Problem Solving Through Programming in C NPTEL Week 4 programs

This article will help you with the answers of Problem Solving Through Programming in C NPTEL Week 4 programs.

Week 4 Program 01 Solution

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

#include <stdio.h>
int main()
{
    int n1, n2, n3; 
    scanf("%d %d %d", &n1, &n2, &n3); /* where three number are read from the test cases and are stored in the variable n1, n2 and n3 */


    if((n1 < n2) && (n1 < n3))
      printf("%d is the smallest number.", n1);
    else if(n2 < n3)
      printf("%d is the smallest number.", n2);
    else
      printf("%d is the smallest number.", n3);
}

Week 4 Program 02 Solution

Write a C program to find power of a number using while loops. The base number (>0) and exponent (>=0) is taken from the test cases.

#include <stdio.h>
int main()
{
int base, exponent;
long int result;
scanf("%d", &base); //The base value is taken from test case
scanf("%d", &exponent);  //The exponent value is taken from test case

result = 1;
while(exponent > 0) {
  result *= base;
  exponent -=1;
}

printf("The result is : %ld\n", result);
return 0;
}

Week 4 Program 03 Solution

Write a C program to calculate the Sum of First and the Last Digit of a given Number. For example if the number is 1234 the result is 1+4 = 5.

#include <stdio.h>
int main()
{

int N, First_digit, Last_digit;

scanf("%d", &N); //The number is accepted from the test case


Last_digit = N % 10;

while(N > 0) {
  First_digit = N;
  N = N / 10;
}

printf("Sum of First and Last digit = %d", First_digit + Last_digit);

return 0;
}

Week 4 Program 04 Solution

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

#include<stdio.h>
void main()
{
    int n;
    long int fact;  /* n is the number whose factorial we have to find and fact is the factorial */
    scanf("%d",&n);  /* The value of n is taken from test cases */

    fact = 1;
    int num = n;
    while(num>0) {
       fact = fact * num;
       num--;
    }
    printf("The Factorial of %d is : %ld",n,fact);
}

For discussion about any question, join the below comment section. And get the solution of your query. Also, try to share your thoughts about the topics covered in this particular quiz.

Leave a Comment

Your email address will not be published. Required fields are marked *