Problem Solving Through Programming in C NPTEL Week 3 programs

Week 3 Program 01 Solution

Write a C Program to calculates the area (floating point number with two decimal places) of a Circle given itâ€™s radius (integer value). The value of Pi is 3.14.

``````#include <stdio.h>
#define PI 3.14
void main()
{
float area;
/* Enter the radius of a circle */

printf("Area of a circle = %5.2f\n", area);
}``````

Week 3 Program 02 Solution

Write a C program to check if a given Number is zero or Positive or Negative Using if…else statement.

``````#include <stdio.h>
int main()
{
double number;
scanf("%lf", &number);
/* The number is entered automatically from the test cases and executed */
/* Write the rest of the code in the box below
As the output should exactly match with the output mentioned in the test cases
so copy and paste the following printf statements wherever and whichever is applicable
printf("The number is 0.");
printf("Negative number.");
printf("Positive number.");
Do not use any other scanf statements */

if(number == 0)
printf("The number is 0.");
else if(number < 0)
printf("Negative number.");
else
printf("Positive number.");

}``````

Week 3 Program 03 Solution

Write a C program to check whether a given number (integer) is Even or Odd.

``````#include <stdio.h>
int main()
{
int number;
scanf("%d", &number); /*An integer number is taken from the test case */

/* Write the rest of the program in the box provided below. As the output
should exactly match with the output provided in the test cases so use exactly the
following printf statement wherever and whichever is applicable.

printf("%d is even.", number);
printf("%d is odd.", number);
*/

if(number%2 == 0)
printf("%d is even.", number);
else
printf("%d is odd.", number);

}``````

Week 3 Program 04 Solution

Write a C Program to find the Largest Number (integer) among Three Numbers (integers) using IF and Logical && operator.

``````#include <stdio.h>
int main()
{
int n1, n2, n3;

scanf("%d %d %d", &n1, &n2, &n3); /*Three numbers are accepted from the test case */

/*  Complete the code in the box provided below. Use printf statement as provided below:
printf("%d is the largest number.", n1);
It may be n1, n2 or n3.
*/

if((n1 > n2) && (n1 > n3))
printf("%d is the largest number.", n1);
else if(n2 > n3)
printf("%d is the largest number.", n2);
else
printf("%d is the largest number.", n3);

}``````

The above question set contains all the correct answers. But in any case, you find any typographical, grammatical or any other error in our site then kindly inform us. Donâ€™t forget to provide the appropriate URL along with error description. So that we can easily correct it.