1.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()
{
int radius;
float area;
/* Enter the radius of a circle */
/* The radius of the circle is taken from the test cases */
scanf("%d", &radius);
area = PI * radius * radius;
printf("Area of a circle = %5.2f\n", area);
}
}
Input | Output | |
42 | Area of a circle = 5538.96 | |
9 | Area of a circle = 254.34 | |
7 | Area of a circle = 153.86 | |
50 | Area of a circle = 7850.00 |
2.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);
if (number <= 0.0)
{
if (number == 0.0)
printf("The number is 0.\n");
else
printf("Negative number.\n");
}
else
printf("Positive number.\n");
}
Input | Output | |
0 | The number is 0. | |
-0.9 | Negative number. | |
14.05 | Positive number. | |
0.45 | Positive number. | |
-50 | Negative number. |
3.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 */
/*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);
if( n2>=n1 && n2>=n3 )
printf("%d is the largest number.", n2);
if( n3>=n1 && n3>=n2 )
printf("%d is the largest number.", n3);
}
Input | Output | |
-9 -4 -20 | -4 is the largest number. | |
45 34 67 | 67 is the largest number. | |
80 -5 3 | 80 is the largest number. |
4.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);
if(number % 2 == 0)
printf("%d is even.", number);
else
printf("%d is odd.", number);
}
Input | Output | |
51 | 51 is odd. | |
116 | 116 is even. | |
24 | 24 is even. | |
-25 | -25 is odd. |
5.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);
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);
}
}
|
6.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 that it is a Right-angle triangle do not print it as scalene triangle).
#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");
}
Input | Output | |
5 12 13 | Right-angle Triangle | |
9 9 9 | Equilateral Triangle | |
10 4 6 | Triangle is not possible | |
7 6 8 | Scalene Triangle | |
3 4 5 | Right-angle Triangle |
7.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);
int i=1;
fact = 1;
while(i<=n)
{
fact*=i;
i++;
}
printf("The Factorial of %d is : %ld",n,fact);
}
Input | Output | |
7 | The Factorial of 7 is : 5040 | |
11 | The Factorial of 11 is : 39916800 | |
5 | The Factorial of 5 is : 120 | |
10 | The Factorial of 10 is : 3628800 |
8.Write a Program to find the sum of all even numbers from 1 to N where the value of N is taken as input. [For example when N is 10 then the sum is 2+4+6+8+10 = 30]
#include <stdio.h>
void main()
{
int N, sum=0;
scanf("%d", &N);
int i;
for (i = 1; i <= N; i++)
{
if (i % 2 == 0)
sum = sum + i;
}
printf("Sum = %d", sum);
}
Input | Output | |
15 | Sum = 56 | |
25 | Sum = 156 | |
10 | Sum = 30 | |
30 | Sum = 240 |
9.Write a program to find whether a given character is a Vowel or consonant. A character is taken as input. The character may be in Upper Case or in Lower Case.
#include <stdio.h>
int main()
{
char ch;
scanf("%c",&ch);
int Lower_case, Upper_case;
Lower_case = (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u');
Upper_case = (ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U');
if (Lower_case|| Upper_case)
printf("%c is a vowel.", ch);
else
printf("%c is a consonant.", ch);
}
Input | Output | |
I | I is a vowel. | |
k | k is a consonant. | |
C | C is a consonant. | |
o | o is a vowel. |
Comments
Post a Comment