Module II Programming in C – EST 102
1. Write a C program to display “Hello World”
Program
#include<stdio.h>
void main()
{
printf("Hello World");
}
Output
Hello World
2. Write a C program to read two numbers, add them and display their sum
Program
#include<stdio.h>
void main()
{
int a,b,sum;
printf("Enter two numbers: ");
scanf("%d%d",&a,&b);
sum=a+b;
printf("%d + %d = %d",a,b,sum);
}
Output
Enter two numbers: 10 20
10 + 20 = 30
3. Write a C program to read the radius of a circle, calculate its area and display it
Program
#include<stdio.h>
void main()
{
float radius,area;
printf("Enter the radius of the circle: ");
scanf("%f",&radius);
area=3.14 * radius * radius;
printf("Area of the circle = %f",area);
}
Output
Enter the radius of the circle: 10
Area of the circle = 314.000000
1 VJCET
Module II Programming in C – EST 102
4. Write a C program to Evaluate the arithmetic expression ((a -b / c * d + e) * (f +g)) and display its
solution.
Program
//((a -b / c * d + e) * (f + g))
//((20-10/ 2 * 3 + 4) * (1 + 2))
#include<stdio.h>
void main()
{
int a,b,c,d,e,f,g,result;
printf("Enter a b c d e f g values: ");
scanf("%d%d%d%d%d%d%d",&a,&b,&c,&d,&e,&f,&g);
result=((a - b / c * d + e) * (f + g));
printf("((%d - %d / %d * %d + %d) * (%d + %d)) = %d",a,b,c,d,e,f,g,result);
}
Output
Enter a b c d e f g values: 20 10 2 3 4 1 2
((20 - 10 / 2 * 3 + 4) * (1 + 2)) = 27
5. Write a C program to swap two numbers using temporary variable
Program
//swap two numbers using temporary variable
#include<stdio.h>
void main()
{
int a,b,temp;
printf("Enter two numbers: ");
scanf("%d%d",&a,&b);
printf("Before swapping a=%d b=%d",a,b);
temp=b;
b=a;
a=temp;
printf("\nAfter swapping a=%d b=%d",a,b);
}
Output
Enter two numbers: 10 20
Before swapping a=10 b=20
After swapping a=20 b=10
6. Write a C program to swap two numbers without using temporary variable
Program
//swap two numbers without using temporary variable
#include<stdio.h>
void main()
{
int a,b;
printf("Enter two numbers: ");
scanf("%d%d",&a,&b);
printf("Before swapping a=%d b=%d",a,b);
2 VJCET
Module II Programming in C – EST 102
a=a+b;
b=a-b;
a=a-b;
printf("\nAfter swapping a=%d b=%d",a,b);
}
Output
Enter two numbers: 10 20
Before swapping a=10 b=20
After swapping a=20 b=10
7. Write a C program to read 2 integer values and find the largest among them.
Program
//find the largest of two numbers
#include<stdio.h>
void main()
{
int n1,n2;
printf("Enter two number: ");
scanf("%d%d",&n1,&n2);
if(n1>n2)
{
printf("largest number=%d",n1);
}
else
{
printf("largest number=%d",n2);
}
}
Output
Enter two number: 20 10
largest number=20
8. Write a C program to find the largest among two numbers using conditional operator.
Program
//largest among two numbers using conditional operator
#include<stdio.h>
void main()
{
int n1,n2,largest;
printf("Enter two numbers: ");
scanf("%d%d",&n1,&n2);
largest=(n1>n2)?n1:n2;
printf("Largest among %d and %d is %d",n1,n2,largest);
}
Output
Enter two numbers: 10 20
Largest among 10 and 20 is 20
3 VJCET
Module II Programming in C – EST 102
9. Write a C program to read 3 integer values and find the largest among them.
Program
//Largest among 3 numbers
#include<stdio.h>
void main()
{
int num1,num2,num3,largest;
printf("Enter three numbers: ");
scanf("%d%d%d",&num1,&num2,&num3);
if(num1>num2)
{
if(num1>num3)
largest=num1;
else
largest=num3;
}
else //num2>=num1
{
if(num2>num3)
largest=num2;
else
largest=num3;
}
printf("The largest number is %d",largest);
}
Output
Enter three numbers: 2 1 3
The largest number is 3
10. Write a C program to read 3 integer values and find the largest among them using conditional
operator.
Program
//Largest among three numbers using conditional operator
#include<stdio.h>
void main()
{
int n1,n2,n3,largest;
printf("Enter three numbers: ");
scanf("%d%d%d",&n1,&n2,&n3);
largest=(n1>n2)?((n1>n3)?n1:n3):((n2>n3)?n2:n3);
printf("Largest=%d",largest);
}
Output
Enter three numbers: 20 10 30
Largest=30
4 VJCET
Module II Programming in C – EST 102
11. Write a C Program to check whether the given number is positive or negative.
Program
//check whether the given number is +ve or -ve
#include<stdio.h>
void main()
{
int n;
printf("Enter the number: ");
scanf("%d",&n);
if(n>=0)
printf("%d is a positive number",n);
else
printf("%d is a negative number",n);
}
Output
Enter the number: 10
10 is a positive number
12. Write a C Program to check whether the given number is odd or even.
Program
//check whether the given number is odd or even
#include<stdio.h>
void main()
{
int n;
printf("Enter the number: ");
scanf("%d",&n);
if(n%2==0)
printf("%d is an even number",n);
else
printf("%d is an odd number",n);
}
Output
Enter the number: 99
99 is an odd number
13. Write a C program to find the given number is odd or even using goto statement.
Program
//Find odd or even using goto
#include<stdio.h>
void main()
{
int num;
printf("Enter the number: ");
scanf("%d",&num);
if(num%2==0)
5 VJCET
Module II Programming in C – EST 102
{
printf("%d is an even number",num);
goto END;
}
printf("%d is an odd number",num);
END: printf("\nProgram terminated!!!!");
}
Output
Enter the number: 10
10 is an even number
Program terminated!!!!
14. Write a C program to perform the arithmetic operations (+,-,*,/) using else-if ladder.
Program
//Perform arithmetic operation using else-if ladder
#include<stdio.h>
void main()
{
int n1,n2,op;
printf("Enter two operands: ");
scanf("%d%d",&n1,&n2);
printf("Enter 1:Addition 2:Subtraction 3:Multiplication 4:Division \n");
scanf("%d",&op);
if(op==1)
{
printf("%d + %d = %d",n1,n2,(n1+n2));
}
else if(op==2)
{
printf("%d - %d = %d",n1,n2,(n1-n2));
}
else if(op==3)
{
printf("%d * %d = %d",n1,n2,(n1*n2));
}
else if(op==4)
{
printf("%d / %d = %d",n1,n2,(n1/n2));
}
else
{
printf("Invalid operator");
}
}
Output
Enter two operands: 20 10
Enter 1:Addition 2:Subtraction 3:Multiplication 4:Division
1
20 + 10 = 30
6 VJCET
Module II Programming in C – EST 102
15. Write a C program to perform the arithmetic operations(+,-,*,/) using switch.
Program
//Perform arithmetic operation using switch
#include<stdio.h>
void main()
{
int n1,n2,op;
printf("Enter two operands: ");
scanf("%d%d",&n1,&n2);
printf("Enter 1:Addition 2:Subtraction 3:Multiplication 4:Division \n");
scanf("%d",&op);
switch(op)
{
case 1: printf("%d + %d = %d",n1,n2,(n1+n2));
break;
case 2: printf("%d - %d = %d",n1,n2,(n1-n2));
break;
case 3: printf("%d * %d = %d",n1,n2,(n1*n2));
break;
case 4: printf("%d / %d = %d",n1,n2,(n1/n2));
break;
default:printf("Invalid operator");
}
}
Output
Enter two operands: 20 10
Enter 1:Addition 2:Subtraction 3:Multiplication 4:Division
3
20 * 10 = 200
16. Write a C program to display a word starting with the given alphabet using else if ladder.
Program
//Display a word starting with the given alphabet
#include<stdio.h>
void main()
{
char alphabet;
printf("Enter an alphabet in the range(A-D or a-d): ");
scanf("%c",&alphabet);
if(alphabet=='A' || alphabet=='a')
printf("%c for Apple",alphabet);
else if(alphabet=='B' || alphabet=='b')
printf("%c for Ball",alphabet);
else if(alphabet=='C' || alphabet=='c')
printf("%c for Cat",alphabet);
else if(alphabet=='D' || alphabet=='d')
printf("%c for Dog",alphabet);
7 VJCET
Module II Programming in C – EST 102
else
printf("Invalid ALPHABET");
}
Output
Enter an alphabet in the range(A-D or a-d): c
c for Cat
17. Write a C program to display a word starting with the given alphabet using switch.
Program
//Display a word starting with the given alphabet using switch
#include<stdio.h>
void main()
{
char alphabet;
printf("Enter an alphabet in the range(A-D or a-d): ");
scanf("%c",&alphabet);
switch(alphabet)
{
case 'A':
case 'a': printf("%c for Apple",alphabet);
break;
case 'B':
case 'b': printf("%c for Ball",alphabet);
break;
case 'C':
case 'c': printf("%c for Cat",alphabet);
break;
case 'D':
case 'd': printf("%c for Dog",alphabet);
break;
default :printf("Invalid ALPHABET");
}
}
Output
Enter an alphabet in the range(A-D or a-d): D
D for Dog
18. Write a C program to find Roots of Quadratic Equation ax2+bx+c=0
Program
//Root of a quadratic equation
#include<stdio.h>
#include<math.h>
void main()
{
float a,b,c,p,root1,root2,real_part,im_part;
printf("Enter three Coefficients: ");
scanf("%f%f%f",&a,&b,&c);
p=(b*b)-(4*a*c);
if(p>0)
{
8 VJCET
Module II Programming in C – EST 102
root1=(-b+sqrt(p))/(2*a);
root2=(-b-sqrt(p))/(2*a);
printf("Root1=%f Root2=%f",root1,root2);
}
else if(p==0)
{
root1=(-b)/(2*a);
printf("Root=%f",root1);
}
else
{
real_part=(-b)/(2*a);
im_part=sqrt(-p)/(2*a);
printf("Roots are imaginary\n");
printf("Root1=%.2f+%.2fi Root2=%.2f-%.2fi",real_part,im_part,real_part,im_part);
}
}
Output1
Enter three Coefficients: 8 -4 -2
Root1=0.809017 Root2=-0.309017
Output2
Enter three Coefficients: 1 2 1
Root=-1.000000
Output3
Enter three Coefficients: 2.3 4 5.6
Roots are imaginary
Root1=-0.87+1.30i Root2=-0.87-1.30i
19. Write a C program to find the factorial of a number
Program
//factorial of a number
#include<stdio.h>
void main()
{
int n,fact=1,i;
printf("Enter the number: ");
scanf("%d",&n); 5
for(i=1;i<=n;i++)
{
fact=fact*i;
}
printf("%d!=%d",n,fact);
}
Output
Enter the number: 4
4!=24
9 VJCET
Module II Programming in C – EST 102
20. Write a C program to read a natural number and check whether the number is prime or not
Program
//Prime or not
#include<stdio.h>
void main()
{
int num,i,count=0;
printf("Enter the number: ");
scanf("%d",&num);
for(i=1;i<=num;i++)
{
if( num%i ==0 )
{
count++;
}
}
if(count==2)
printf("%d is a prime number",num);
else
printf("%d is not a prime number",num);
}
Output
Enter the number: 11
11 is a prime number
21. Write a C program to find the sum of digits of a number
Program
//Sum of digits of a number
#include<stdio.h>
void main()
{
int n,num,rem,sum=0;
printf("Enter the number: ");
scanf("%d",&num);
n=num;
while(n!=0)
{
rem=n%10;
sum=sum+rem;
n=n/10;
}
printf("sum of digits of %d is %d",num,sum);
}
Output
Enter the number: 123
sum of digits of 123 is 6
10 VJCET
Module II Programming in C – EST 102
22. Write a C program to check whether the number is palindrome or not.
Program
//Given number is palindrome or not
#include<stdio.h>
void main()
{
int n,num,rem,reverse=0;
printf("Enter the number: ");
scanf("%d",&num);
n=num;
while(n!=0)
{
rem=n%10;
reverse=reverse*10+rem;
n=n/10;
}
if(num==reverse)
printf("%d is a palindrome number",num);
else
printf("%d is not a palindrome number",num);
}
Output
Enter the number: 12321
12321 is a palindrome number
23. Write a C program to read a natural number and check whether the number is Armstrong or not
Program
//Armstrong number or not
#include<stdio.h>
void main()
{
int num,n,m=0,rem;
printf("Enter the number: ");
scanf("%d",&num);
n=num;
while(n!=0)
{
rem=n%10;
m=m+(rem*rem*rem);
n=n/10;
}
if(num==m)
printf("%d is an Armstrong number", num);
else
printf("%d is not an Armstrong number", num);
}
Output
11 VJCET
Module II Programming in C – EST 102
Enter the number: 153
153 is an Armstrong number
24. Write a C program to find the sum of first n numbers using for loop
Program
//Sum of n numbers using for loop
#include<stdio.h>
void main()
{
int n,data,sum=0,i;
printf("Enter the value of n: ");
scanf("%d",&n);
printf("Enter %d numbers: ",n);
for(i=1;i<=n;i++)
{
scanf("%d",&data);
sum=sum+data;
}
printf("Sum=%d",sum);
}
Output
Enter the value of n: 3
Enter 3 numbers: 20 10 30
Sum=60
25. Write a C program to find the sum of first n numbers using while loop
Program
//Sum of n numbers using while loop
#include<stdio.h>
void main()
{
int n,data,sum=0,i=1;
printf("Enter the value of n: ");
scanf("%d",&n);
printf("Enter %d numbers: ",n);
while(i<=n)
{
scanf("%d",&data);
sum=sum+data;
i++;
}
printf("Sum=%d",sum);
}
Output
Enter the value of n: 3
Enter 3 numbers: 10 20 30
Sum=60
12 VJCET
Module II Programming in C – EST 102
26. Write a C program to find the sum of first n numbers using do-while loop
Program
//Sum of n numbers using do-while loop
#include<stdio.h>
void main()
{
int n,data,sum=0,i=1;
printf("Enter the value of n: ");
scanf("%d",&n);
printf("Enter %d numbers: ",n);
do
{ scanf("%d",&data);
sum=sum+data;
i++;
}while(i<=n);
printf("Sum=%d",sum);
}
Output
Enter the value of n: 3
Enter 3 numbers: 30 20 10
Sum=60
27. Write a C program to print multiplication Table of a number
Program
//Multiplication table
#include<stdio.h>
void main()
{ int n,i=1;
printf("Enter the number: ");
scanf("%d",&n);
printf("Multiplication Table of %d\n",n);
for(i=1;i<=10;i++)
{
printf("%d x %d = %d\n",i,n,i*n);
}
}
Output
Enter the number: 3
Multiplication Table of 3
1x3=3
2x3=6
3x3=9
4 x 3 = 12
5 x 3 = 15
6 x 3 = 18
7 x 3 = 21
13 VJCET
Module II Programming in C – EST 102
8 x 3 = 24
9 x 3 = 27
10 x 3 = 30
28. Write a C program to generate first n Fibonacci numbers.
Program
//Generate first n Fibonacci numbers
#include<stdio.h>
void main()
{
int n,n1=0,n2=1,n3,i;
printf("Enter the value of n: ");
scanf("%d",&n);
if(n==1)
{
printf("%d",n1);
}
else if(n==2)
{
printf("%d\t%d",n1,n2);
}
else
{
printf("%d\t%d",n1,n2);
for(i=3;i<=n;i++)
{
n3=n1+n2;
printf("\t%d",n3);
n1=n2;
n2=n3;
}
}
}
Output
Enter the value of n: 10
0 1 1 2 3 5 8 13 21 34
29. Write a C program to find the sum of series 1–x + x2 – x3 …. xn
Program
//Generate the sum of 1-x + x^2 - x^3 . . . x^n
#include<stdio.h>
#include<math.h>
void main()
{
int x,n,i,sum=1,sign=-1;
printf("Enter the value of x and n: ");
scanf("%d%d",&x,&n);
for(i=1;i<=n;i++)
{
if(sign==-1)
{
14 VJCET
Module II Programming in C – EST 102
sum=sum-pow(x,i);
sign=+1;
}
else
{
sum=sum+pow(x,i);
sign=-1;
}
}
printf("Sum=%d",sum);
}
Output
Enter the value of x and n: 2 3
Sum=-5
30. Write a C program to print the following pattern
1 2 3
2 3 4
3 4 5
4 5 6
Program
#include<stdio.h>
void main()
{
int i,j;
for(i=1;i<=4;i++)
{ for(j=i;j<=i+2;j++)
{ printf("%d\t",j);
}
printf("\n");
}
}
Output
1 2 3
2 3 4
3 4 5
4 5 6
31. Write a C program to print the following pattern
1
1 2
1 2 3
1 2 3 4
Program
#include<stdio.h>
void main()
{
int i,j;
for(i=1;i<=4;i++)
{ for(j=1;j<=i;j++)
{ printf("%d\t",j);
15 VJCET
Module II Programming in C – EST 102
}
printf("\n");
}
}
Output
1
1 2
1 2 3
1 2 3 4
32. Write a C program to print the following pattern
1
2 3
4 5 6
7 8 9 10
Program
#include<stdio.h>
void main()
{ int i,j,k=1;
for(i=1;i<=4;i++)
{ for(j=1;j<=i;j++)
{ printf("%d\t",k);
k++;
}
printf("\n");
}
}
Output
1
2 3
4 5 6
7 8 9 10
33. Write a C program to print the following pattern
1
2 3
4 5 6
7 8 9 10
Program
#include<stdio.h>
void main()
{ int i,j,k=1,start_tab=3;
for(i=1;i<=4;i++)
{ for(j=1;j<=start_tab;j++)
{ printf("\t");
}
start_tab--;
for(j=1;j<=i;j++)
{ printf("%d\t\t",k);
k++;
}
16 VJCET
Module II Programming in C – EST 102
printf("\n");
}
}
Output
1
2 3
4 5 6
7 8 9 10
34. Read the year of birth of the user. Then print the year in which he celebrate his 50 th birthday.
35. Write a program to calculate the value of resistance using ohm’s law
36. Write a program to calculate the BMI of a person by reading Mass and Height(in Meters)
37. Read the time in seconds. Then convert it into day: hrs: mints: seconds:
38. An employee’s total weekly pay equals the hourly wage multiplied by the total number of regular
hours plus any overtime pay. Overtime pay equals the total overtime hours multiplied by 1.5 times the
hourly wage. Write a program that takes as inputs the hourly wage, total regular hours and total
overtime hours and displays an employee’s weekly pay
17 VJCET