C programming
1. WAP to calculate sum of two numbers
#include <stdio.h>
int main()
{
int a,b,sum;
printf("Enter two numbers:”);
scanf("%d %d", &a,&b);
sum=a+b;
printf("Sum= %d", sum);
return 0;
}
output
Enter two numbers:2
3
Sum= 5
#include <stdio.h>
int main()
{
int a,b,sum;
printf("Enter two numbers:”);
scanf("%d %d", &a,&b);
sum=a+b;
printf("Sum of %d and %d = %d", a,b,sum);
return 0;
}
output
Enter two numbers:2
3
Sum of 2 and 3 = 5
2. WAP to calculate the area of a rectangle in c. [Hints: a = l x b]
#include <stdio.h>
int main()
{
int b,l,area;
printf("Enter length and breadth:”);
scanf("%d %d", &l,&b);
area=l*b;
printf("area of rectangle = %d", area);
return 0;
}
output
Enter length and breadth:3
4
area of rectangle = 12
3. WAP to calculate the simple interest in c. [Hints: i= (ptr)/100]
#include <stdio.h>
int main()
{
float p,t,r,si;
printf("Enter principle,time,rate:”);
scanf("%f %f %f", &p,&t,&r);
si=(p*t*r)/100;
printf("simple interest = %f", si);
return 0;
}
Output
Enter principle,time,rate:1000
2
12
simple interest = 240.000000
4. WAP to calculate the average of three number in c. [Hints: avg= (a+b+c)/3]
#include <stdio.h>
int main()
{
float a,b,c,avg;
printf("Enter three numbers:”);
scanf("%f %f %f", &a,&b,&c);
avg=(a+b+c)/3;
printf("average of three numbers = %f", avg);
return 0;
}
Output
Enter three numbers:2
8
7
average of three numbers = 5.666667
5. WAP to calculate the volume of cylinder in c. [Hints: v= pi*r*r*h]
#include <stdio.h>
int main()
{
float pi=3.14,r,h,v;
printf("Enter radius and height:”);
scanf("%f %f", &r,&h);
v= pi*r*r*h;
printf("volume of cylinder = %f", v);
return 0;
}
Output
Enter radius and height:2
4
volume of cylinder = 50.240002
6. WAP to calculate the area of circle in c. [Hints: a= pi*r*r]
7. WAP to calculate the circumference of circle in c. [Hints: c= 2pi*r]
8. WAP To convert degree Celsius and convert it into degree Fahrenheit where f=(9*c/5)+32.
9. WAP To find square of given number.
#include <stdio.h>
#include <math.h>
int main()
{
int n,s;
printf("Enter a number”);
scanf("%d", &n);
s=pow(n,2);
printf("square of %d = %d", n,s);
return 0;
}
Output
Enter a number2
square of 2 = 4
10. WAP To find square root of given number.
#include <stdio.h>
#include <math.h>
int main()
{
int n,s;
printf("Enter a number”);
scanf("%d", &n);
s=sqrt(n);
printf("squareroot of %d = %d", n,s);
return 0;
}
Note cuberoot: cbrt(n)
Conditional
Program
1. TO
CHECK WHETHER GIVEN NUMBER IS POSITIVE OR NEGATIVE
#include <stdio.h>
int main()
{
int n;
printf("ENTER ANY NUMBER");
scanf("%d",&n);
if
(n>0)
{
printf("%d is positive number",n);
}
else {
printf("%d is negative number",n);
}
return 0;
}
2. TO
CHECK WHETHER GIVEN NUMBER IS POSITIVE, NEGATIVE OR ZERO.
#include <stdio.h>
int main()
{
int n;
printf("ENTER ANY NUMBER");
scanf("%d",&n);
if
(n>0)
{
printf("%d is positive number",n);
}
else
if (n<0)
{
printf("%d is negative number",n);
}
else
{
printf("%d is zero",n);
}
return 0;
}
3.TO FIND
THE SMALLEST NUMBER AMONG ANY TWO DIFFERENT NUMBERS
#include <stdio.h>
int main()
{
int a,b;
printf("Enter two numbers");
scanf("%d %d",&a,&b);
if
(a<b)
{
printf("%d is smaller than %d",a,b);
}
else
{
printf ("%d is smaller than
%d",b,a);
}
return 0;
}
4. TO FIND
THE GREATEST NUMBER AMONG ANY THREE DIFFERENT NUMBERS
#include <stdio.h>
int main()
{
int a,b,c;
printf("Enter three numbers");
scanf("%d %d %d",&a,&b,&c);
if (a>b
&& a>c )
{
printf("%d is greatest",a);
}
else if
(b>a && b>c)
{
printf
("%d is greatest",b);
}
else
{
printf
("%d is greatest",c);
}
return 0;
}
5. TO FIND THE MIDDLE NUMBER AMONG ANY THREE DIFFERENT NUMBERS
#include <stdio.h>
int main()
{
int a,b,c;
printf("Enter three numbers");
scanf("%d
%d %d",&a,&b,&c);
if
((a>b && a<c)||(a<b && a>c))
{
printf("%d is middle number",a);
}
else if
((b>a && b<c)||(b<a && b>c))
{
printf
("%d is middle number",b);
}
else
{
printf
("%d is middle number",c);
}
return 0;
}
6. TO TEST
GIVEN NUMBER IS EXACTLY DIVISIBLE BY 7 OR NOT.
#include <stdio.h>
int main()
{
int n, r;
printf("Enter a numbers");
scanf("%d",&n);
r=n%7;
if
(r==0)
{
printf("%d is exactly divisible by 7",n);
}
else
{
printf("%d is not exactly divisible by 7",n);
}
return 0;
}
7. TO TEST
GIVEN NUMBER IS ODD OR EVEN.
#include <stdio.h>
int main()
{
int n, r;
printf("Enter a numbers");
scanf("%d",&n);
r=n%2;
if
(r==0)
{
printf("%d is even number",n);
}
else
{
printf("%d is odd number",n);
}
return 0;
}
8. TO CHECK WHETHER CITIZEN CAN VOTE OR NOT(IF
VOTING AGE IS 18)
9. TO CHECK WHETHER STUDENT IS PASS OR FAIL IN
SCIENCE(IF PASS MARKS IS 40)
10.TO FIND WHETHER A GIVEN YEAR IS A LEAP YEAR OR NOT.
Looping programs
1. 1,2,3,……10th term
#include <stdio.h>
int main()
{
int i;
for(i=1;i<=10;i++)
{
printf("%d\t",i);
}
return 0;
}
2. 10,9,8…….10th term
#include <stdio.h>
int main()
{
int i;
for(i=10;i>=1;i=i-1)
{
printf("%d\t",i);
}
return 0;
}
3. 1,3,5…….10th term
#include <stdio.h>
int main()
{
int i;
for(i=1;i<=10;i+=2)
{
printf("%d\t",i);
}
}
4. 2,4,6…….10th term
#include <stdio.h>
int main()
{
int i;
for(i=2;i<=10;i+=2)
{
printf("%d\t",i);
}
}
5. 1,2,4,7…….10th term
#include <stdio.h>
int main()
{
int i,a=1;
for(i=1;i<=10;i++)
{
printf("%d\t",a);
a=a+i;
}
}
6. 1,8,27…….10th term
#include <stdio.h>
#include <math.h>
int main()
{
int i, a;
for(i=1;i<=10;i=i+1)
{
a=pow(i,3);
printf("%d\t",a);
}
}
7. 5,10,15…….10th term
#include <stdio.h>
int main()
{
int i, a=5;
for(i=1;i<=10;i=i+1)
{
printf("%d\t",a);
a=a+5;
}
}
8. 5,25,125…….10th term
#include <stdio.h>
#include <math.h>
int main()
{
int i, a;
for(i=1;i<=10;i=i+1)
{
a=pow(5,i);
printf("%d\t",a);
}
}
9. 0,1,1,2…….10th term
#include <stdio.h>
int main()
{
int i, a=0,b=1,c;
printf("%d\t",a);
printf("%d\t",b);
for(i=1;i<=10;i=i+1)
{
c=a+b;
a=b;
b=c;
printf("%d\t",c);
}
return 0;
}
10. 7,22,11,34…….10th term
#include <stdio.h>
int main()
{
int i, n=7;
for (i=1;i<=10;i=i+1)
{
printf("%d\t", n);
if (n % 2 == 0)
n = n / 2;
else
n = 3 * n + 1;
}
return 0;
}
Prime or composite
#include <stdio.h>
int main()
{
int n, i, c = 0;
printf("Enter a number:");
scanf("%d", &n);
for (i =1;i<=n; i++)
{
if (n % i == 0)
{
c = c+1;
}
}
if (c==2)
{
printf("%d is a prime number.", n);
}
else
{
printf("%d is a composite number.", n);
}
return 0;
}
Palindrome number or not
#include <stdio.h>
int main()
{
int n,z,s,r;
printf("ENTER ANY NUMBER: ");
scanf("%d", &n);
z = n;
s = 0;
while (n != 0)
{
r = n % 10;
s = s * 10 + r;
n = n / 10;
}
if (z == s)
printf("%d IS PALINDROME\n", z);
else
printf("%d IS NOT PALINDROME\n", z);
return 0;
}
Armstrong number or not
#include <stdio.h>
int main()
{
int n, z, s = 0, r;
printf("ENTER ANY NUMBER: ");
scanf("%d", &n);
z = n;
while (n != 0)
{
r = n % 10;
s = s + r * r * r;
n = n / 10;
}
if (z == s)
printf("%d IS AN ARMSTRONG NUMBER\n", z);
else
printf("%d IS NOT AN ARMSTRONG NUMBER\n", z);
return 0;
}
8