Sunday, August 13, 2023

Write a program to find the factorial of a given number using while loop.


 

Write a program to find the factorial of a given number using while loop.


Program :

#include<stdio.h>

void main()

{

    int n;

    long int fact;  

    scanf("%d",&n);

int i=1;

fact = 1;

while(i<=n)

    {

        fact*=i;

        i++;

    }

    printf("The Factorial of %d is : %ld",n,fact);

}



InputOutput
Test Case 1
5
The Factorial of 5 is : 120
Test Case 2
10
The Factorial of 10 is : 3628800

The length of three sides are taken as input. Write a C program to find whether a triangle can be formed or not. If not display “This Triangle is NOT possible.” If the triangle can be formed then check whether the triangle formed is equilateral, isosceles, scalene or a right-angled triangle. (If it is a right-angled triangle then only print Right-angle triangle do not print it as Scalene Triangle).


 

The length of three sides are taken as input. Write a C program to find whether a triangle can be formed or not. If not display “This Triangle is NOT possible.” If the triangle can be formed then check whether the triangle formed is equilateral, isosceles, scalene or a right-angled triangle. (If it is a right-angled triangle then only print Right-angle triangle do not print it as Scalene Triangle).


Program :

#include<stdio.h>

int main()

{

    int a,b,c; 

    scanf("%d %d %d",&a, &b, &c);

if(a<(b+c)&&b<(a+c)&&c<(a+b))

    {

        if(a==b&&a==c&&b==c)

        printf("Equilateral Triangle");

          else if(a==b||a==c||b==c)

          printf("Isosceles Triangle");

          else   

    if((a*a)==(b*b)+(c*c)||(b*b)==(a*a)+(c*c)||(c*c)==(a*a)+(b*b))

        printf("Right-angle Triangle");

        else if(a!=b&&a!=c&&b!=c)

        printf("Scalene Triangle");

    }

    else

    printf("Triangle is not possible");

}



InputOutput
Test Case 1
9 9 9
Equilateral Triangle
Test Case 2
5 12 13
Right-angle Triangle

Write a C Program to Find the Smallest Number among Three Numbers (integer values) using Nested IF-Else statement.


 Write a C Program to Find the Smallest Number among Three Numbers (integer values) using Nested IF-Else statement.


Program  :


#include <stdio.h>

int main()

{

    int n1, n2, n3; 

    scanf("%d %d %d", &n1, &n2, &n3);

if (n1<n2)

    {

        if(n1<n3)

            printf("%d is the smallest number.", n1);

        else

            printf("%d is the smallest number.", n3);

    }

    else

    {

        if(n2<n3)

            printf("%d is the smallest number.", n2);

        else

            printf("%d is the smallest number.", n3);

    }

}



InputOutput
Test Case 1
80 40 90
40 is the smallest number.
Test Case 2
77 88 -99
-99 is the smallest number.

NPTEL » Problem Solving Through Programming In C Week-4 Quiz Solution

 


Week 4 : Assignment 4


1 point
What is the purpose of the "if-else" statement in C?
 
 
 
 
1 point
What is the correct syntax for an "if-else" statement in C?
 
 
 
 
1 point
Which of the following is true about nested "if-else" statements?
 
 
 
 
1 point
What happens if there is no "else" part in an "if-else" statement?
 
 
 
 
1 point
Which of the following operators can be used to combine multiple conditions in an "if" statement?
 
 
 
 
1 point
 
 
 
 
1 point
If multiple conditions are used in a single "if" statement then the testing of those conditions are done
 
 
 
 
1 point
 
 
 
 
1 point
 
 
 
 
1 point
 
 
 
 
Sunday, August 6, 2023

Complete the code segment to debug / complete the program which is intended to print 'NPTEL JAVA'.


 

Complete the code segment to debug / complete the program which is intended to print 'NPTEL JAVA'.


Program:-

public class Question215{ 

    public static void main(String[] args) {

 //Declare variable with name 'nptel', 'space' and 'java' and proper datatype

    String nptel, space, java;


  //Initialize the variables with proper input

    nptel = "NPTEL";

    space = " ";

    java = "JAVA";

System.out.print(nptel+space+java);

   }

}



InputOutput
Test Case 1
NPTEL JAVA

Complete the code segment to call default constructor first and then any other constructor in the class.


 

Complete the code segment to call default constructor first and then any other constructor in the class.


Program :-


// This is the main class Question

public class Question214{

public static void main(String[] args){

Answer a = new Answer(10,"MCQ");

}

}

class Answer{

// This is the default constructor of the class Answer

Answer(){

System.out.println("You got nothing.");

}

// This is a parameterized constructor of the class Answer

Answer(int marks, String type){

//The 'this()' referene variable is able to call the default constructor of the class.

this();

//Print marks and type of the question

System.out.print("You got "+marks+" for an "+ type);

}

}



InputOutput
Test Case 1
You got nothing.
You got 10 for an MCQ

Complete the code segment to call print() method of class Question by creating a method named ‘studentMethod()’.



 Complete the code segment to call print() method of class Question by creating a method named ‘studentMethod()’.


Program:-


// This is the main class Question

public class Question23{ 

    public static void main(String[] args) { 

// Object of the main class is created

Question23 q = new Question23();

// Print method on object of Question class is called

q.studentMethod();

    }

// 'print()' method is defined in class Question

void print(Question23 object){

System.out.print("Well Done!");

}

// 'studentMethod()' method is defined in class Question23

void studentMethod(){

// Calling a method named 'print()' in class Question23

print(this);

    }

}



InputOutput
Test Case 1
Well Done!

Complete the code segment to call the method print() of class given class Printer to print the following.


 

Complete the code segment to call the method  print() of class given class Printer to print the following.


--------------------------------
Hi! I am class STUDENT
Hi! I class SCHOOL.
--------------------------------

Program :

// This is the class named Printer
class Printer { 
    // This are the methods in class Printer
    public void print() { 
System.out.println("Hi! I class SCHOOL."); 
    } 
    public void print(String s) { 
System.out.println(s); 
    } 

public class Question22{ 
    public static void main(String[] args) {
// Create an object of class Printer

Printer p = new Printer();

// Call 'print()' methods for desired output
p.print("Hi! I am class STUDENT");

p.print();
}
}


InputOutput
Test Case 1
Hi! I am class STUDENT
Hi! I class SCHOOL.

Complete the code segment to call the method print() of class Student first and then call print() method of class School.



Complete the code segment to call the method  print() of class Student first and then call print() method of class School.


 // This is the class named School

class School { 

    // This is a method in class School

    public void print() { 

System.out.println("Hi! I class SCHOOL."); 

    } 

// This is the class named Student

class Student { 

// This is a method in class Student

    public void print() { 

System.out.println("Hi! I am class STUDENT"); 

    } 

}


public class Question21{ 

    public static void main(String args[]){

// Creating object of class Student

Student student = new Student();


// Creating object of class School

School school = new School();



 // Call 'print()' method of class Student 

student.print();

// Call 'print()' method of class School

school.print();

}


InputOutput
Test Case 1
Hi! I am class STUDENT
Hi! I class SCHOOL.

     

NPTEL » Programming In Java Week-2 Quiz Assignment Solution

 


Week 2:Assignment 2


1 point
 
 
 
 
1 point
 
 
 
 
1 point
 
 
 
 
1 point
 
 
 
 
1 point
 
 
 
 
1 point
 
 
 
 
1 point
 
 
 
 
1 point
 
 
 
 
1 point
 
 
 
 
1 point