Practice examples in c language
1. Program to print maximum number of
characters(“width”)
INPUT:-
#include <stdio.h>
int main()
printf(“\n The number is %-6d%d ”, 1234,12);
return 0;
OUTPUT:-
The number is 1234 12
2. Program to perform both “width and presicion
INPUT:-
#include <stdio.h>
int main()
printf(“\n The number is %09.1f”,123.456);
return 0;
OUTPUT:-
The number is 000123.46
3. Program to perform width and precision using flags
INPUT:-
#include<stdio.h>
int main()
printf("\n a=|%-15.2f|b=%0+7.2fc=%-08.2f",1.2,1.2,1.2);
return 0;
OUTPUT:-
a=|1.2 |b=+001.20c=1.20
4. Program to perform width and presicion using flags
in the “Good morning”
INPUT:-
#include<stdio.h>
int main()
char str[] = "Good Morning";
printf("\n %s", str);
printf("\n %20s", str);
printf("\n %20.10s", str);
printf("\n %.7s", str);
printf("\n %-20.10s", str);
printf("\n %7s", str);
printf("\n %15s", str);
return 0;
OUPUT:-
Good Morning
Good Morning
Good Morni
Good Mo
Good Morni
Good Morning
Good Morning
5. Program to perform width in the given number
INPUT:-
#include<stdio.h>
int main()
int a,b;
printf("\n Enter two four digit numbers:");
scanf("%2d %4d",&a,&b) ;
printf("\n the two numbers are :%dand%d",a,b);
return 0;
OUTPUT:-
Enter two four digit numbers:1234 5678
The two numbers are:12 and 34
6. Program to print long integers more than 10 digits
INPUT:-
#include<stdio.h>
int main()
unsigned long long i=10000000000;
printf("%llu",i);
OUTPUT:-
10000000000
7. Program to print different data types
INPUT:-
#include<stdio.h>
int main()
// Declare and initialize variables
int num=7;
float amt=123.45;
char code='A';
double pi=3.1415926536;
unsigned long long a=10000000000;
char msg[]="hi";
printf("\n NUM=%d \t AMT =%f \t CODE= %c \n PI=%e\t POPULATION OF
MESSAGE= %s",num,amt,code,pi,a, msg);
return 0;
}
OUTPUT:-
NUM=7 AMT=123.4499997 CODE=A
PI=3.141593e+000 POPULATION OF INDIA=10000000000
MESSAGE=hi
8. Program to find the distance between to points
INPUT:-
#include<stdio.h>
#include<math.h>
main()
int x1, x2, y1, y2;
float distance;
// To find distance between two points
printf("\n enter the x and y coordinates of the first point:");
scanf("%d %d",&x1,&y1);
printf("\n enter the x and y coordinates at second point:");
scanf("%d %d",&x2,&y2);
//sqrt and pow are mathematical
distance=sqrt(pow((x2-x1),2)+pow((y2-y1),2));
printf("\n Distance=%f",distance);
return 0;
OUTPUT:-
Enter the x and y coordinate of the first point:2
Enter the x and y coordinates at second point:3
7
Distance = 2.236068
9. Program to find the area of the triangle
INTPUT:-
//to caluculate the area of triangle
#include<stdio.h>
#include<math.h>
main()
float a, b, c, area,s;
printf("\n enter the lengths of the three sides of the triangles:");
scanf("%f %f %f",&a,&b,&c);
s=(a+b+c)/2;
area=sqrt(s*(s-a)*(s-b)*(s-c)) ;
printf("\n area =%f",area);
return 0;
OUTPUT:-
Enter the lengths of the three sides of the traingle:12
16
20
Area=96.000000
10. Program to perform all “Arithmetic Operators”
INPUT:-
#include<stdio.h>
#include<conio.h>
int main()
int num1,num2;
int add_res=0, sub_res=0, mul_res=0, idiv_res=0, modiv_res=0;
float fdiv_res=0.0;
printf("\n enter the frst number:");
scanf("%d",&num1);
printf("\n enter the second number:");
scanf("%d",&num2);
add_res=num1+num2;
sub_res=num1-num2;
mul_res=num1*num2;
idiv_res=num1/num2;
modiv_res=num1%num2;
fdiv_res=(float)num1/num2;
printf("\n %d + %d = %d", num1, num2, add_res);
printf("\n %d - %d = %d", num1, num2, sub_res);
printf("\n %d * %d = %d", num1, num2, mul_res);
printf("\n %d / %d = %d (Integer Division)", num1, num2, idiv_res);
printf("\n %d %% %d =%d (Modulo Division)",num1, num2, modiv_res);
printf("\n %d / %d = %.2f (Normal Division)",num1, num2, fdiv_res);
return 0;
}
OUTPUT:-
enter the first number: 9
enter the second number:7
9 + 7 = 16
9 - 7 = 2
9 * 7 = 63
9 / 7 = 1(Integer Division)
9 % 7 = 2(Modulo Division)
9 / 7 = 1.29(Normal Division)
11. Program to perform Arithmetic operator in long
integers
INPUT:-
#include<stdio.h>
#include<conio.h>
Int main()
Long int num1=1234567, num2, diff=0;
print(“\n Enter the number: ”);
scanf(“%ld”, &sum2);
diff= num1 – num2;
printf(“\n Difference= %ld”, diff);
return 0;
OUTPUT:-
Enter the number: 1234
Difference= 1233333
12. Program to perform all “Relational Operators”
INPUT:-
#include<stdio.h>
#include<conio.h>
int main()
int x=10, y=20;
printf("\n %d < %d = %d", x, y, x<y);
printf("\n %d == %d = %d", x, y, x==y);
printf("\n %d != %d = %d", x, y, x!=y);
printf("\n %d > %d = %d", x, y, x>y);
printf("\n %d >= %d = %d", x, y, x>=y);
printf("\n %d <= %d = %d", x, y, x<=y);
return 0;
OUTPUT:-
10 < 20 = 1
10 == 20 = 0
10 != 20 = 1
10 > 20 = 0
10 >= 20 = 0
10 <= 20 = 1
13. Program to perform all Logical Operators
INPUT:-
#include<stdio.h>
#include<conio.h>
int main()
int a=10, b=20, c=30, res1, res2, res3;
res1=(a<b)&&(b>c);
printf("\n (a<b) && (b>c)= %d ", res1);
res2=(a<b) || (b>c);
printf("\n (a<b) || (b>c)= %d ", res2);
res3 =(b=!a);
printf("\n (b=!a)= %d", res3);
return 0;
OUTPUT:-
(a<b)&&(b>c)= 0
(a<b) || (b>c)= 1
(b=!a)= 0
14. Program to perform “Unary Operator”
INPUT:-
#include<stdio.h>
#include<conio.h>
int main()
int num=3;
printf("\n the value of num = %d",num);
printf("\n the value of ++num = %d",++num);
printf("\n the new value of num = %d",num);
printf("\n the value of num++ = %d",num++);
printf("\n the new value of num = %d", num);
printf("\n the value of --num = %d",--num);
printf("\n the new value of num = %d",num);
printf("\n the value of num-- = %d",num--);
printf("\n the new value of num = %d",num);
return 0;
OUTPUT:-
The value of num =3
The value of ++num =4
The new value of num =4
The value of num++ =4
The new value of num =5
The value of –num =4
The new value of num =4
The value of num-- =4
The new value of num =3
15. Program to find the largest number from the given
three numbers using “Conditional/Ternary Operator”
INPUT:-
#include<stdio.h>
int main()
int num1,num2,num3,large;
printf("\n enter the first number:");
scanf("%d",&num1);
printf("\n enter the second number:");
scanf("%d",&num2);
printf("\n enter the third number:");
scanf("%d",&num3);
large =num1>num2?(num1>num3?num1:num3):(num2>num3?num2:num3);
printf("\n the large number is:%d",large);
return 0;
OUTPUT:-
enter the first number: 15
enter the second number: 35
enter the third number: 45
the large number is: 45
16. Program to find the smallest number from the
given three numbers using Conditional/Ternary
Operator”
INPUT:-
#include<stdio.h>
int main()
int num1,num2,num3,large;
printf("\n enter the first number:");
scanf("%d",&num1);
printf("\n enter the second number:");
scanf("%d",&num2);
printf("\n enter the third number:");
scanf("%d",&num3);
large =num1<num2?(num1<num3?num1:num3):(num2<num3?num2:num3);
printf("\n the smallest number is :%d",large);
return 0;
OUTPUT:-
enter first number: 15
enter second number: 8
enter third number: 9
the smallest number is : 8
17. Program to perform all Bitwise operator(B.AND,
B.OR, B.XOR, B.NOT)
INPUT:-
#include<stdio.h>
int main()
int a=10 , b=20, c, d, e;
c=a&b;
d=a|b;
e=a^b;
printf("%d",c);
printf("\n %d",d);
printf("\n %d",e);
return 0;
OUTPUT:-
30
30
18. Program to perform all ASSIGNMENT OPERATORs
INPUT:-
#include<stdio.h>
int main()
int a=5, c;
c=a;
printf("\nc=%d",c);
c+=a;
printf("\nc=%d",c);
c-=a;
printf("\nc=%d",c);
c*=a;
printf("\nc=%d",c);
c/=a;
printf("\nc=%d",c);
c%=a;
printf("\nc=%d",c);
c&=a;
printf("\nc=%d",c);
c^=a;
printf("\nc=%d",c);
c>>=a;
printf("\nc=%d",c);
c<<=a;
printf("\nc=%d",c);
OUTPUT:-
c=5
c=10
c=5
c=25
c=5
c=0
c=0
c=5
c=0
c=0
19. Program to perform “sizeof()” operator in
different data types
INPUT:-
#include<stdio.h>
main()
char a='s', res;
res=sizeof(a);
printf("%d",res);
int b=2003, c;
c=sizeof(b);
printf("\n%d",c);
float d=15.42, e;
e=sizeof(d);
printf("\n%f",e);
double f=1547896535635, g;
g=sizeof(f);
printf("\n%lf",g);
return 0;
OUTPUT:-
4.000000
8.000000
20. Program to demonstrate the use of “Assignment
Operators”
INPUT:-
#include<stdio.h>
Int main()
Int num1= 3, num2= 5;
printf(“\n Initial value of num1= %d and num2= %d”, num1, num2);
num1+= num2 * 4 - 7;
printf(“\n After the Evaluation of the expression num1 = %d and
num2= %d “,num1, num2);
return 0;
OUTPUT:-
Initial value of num1 = 3 and num2 = 5
After the evaluation of the expression num1 = 16 and num2 = 5
21. Program to calculate the area and circumference
of the circle
INPUT:-
#include<stdio.h>
main()
float radius;
double area, circumference;
printf("\n Enter the radius of the circle: ");
scanf("%f",&radius);
area= 3.14 * radius * radius;
circumference = 2 * 3.14 * radius;
printf("Area = %.2f",area);
printf("\n CIRMFERENCE= %.2f",circumference);
return 0;
OUTPUT:-
Enter the radius of the circle: 6
Area = 113.04
CIRCUMFERENCE = 37.68
22. Program to print the ACSII value of the character
INPUT:-
#include <stdio.h>
#include <conio.h>
int main()
char ch;
printf("\n enter any character:") ;
scanf("%c",&ch);
printf("\n the ascii value of %c is:%d",ch,ch);
return 0;s
OUTPUT:-
Enter any character: s
The ascii value of s is :115
23. Program to read a character in upper case and
then print it in lower case
INPUT:-
#include <stdio.h>
#include <conio.h>
int main()
char ch;
printf("\n Enter any character in upper case:") ;
scanf("%c", &ch);
printf("\n the character in lower case is:%c",ch+32);
return 0;
}
OUTPUT:-
Enter any character in upper case : A
The character in lower case is: a
24. Program to read the character in lower case and
the print it in upper case
INPUT:-
#include <stdio.h>
#include <conio.h>
int main()
char ch;
printf("\n Enter any character in lower case:") ;
scanf("%c", &ch);
printf("\n the character in upper case is:%c",ch-32);
return 0;
OUTPUT:-
Enter any character in lower case :s
The character I upper case is : S
25. Program to print the digit at ones place of a
number
INPUT:-
#include <stdio.h>
int main()
{
int num,digit_at_ones_place;
printf("\n Enter any number: ");
scanf("%d",&num);
digit_at_ones_place=num/1%10;
printf("\n the digit at ones place of %d is%d",num,digit_at_ones_place);
return 0;
OUTPUT:-
Enter any number : 589
The digit at ones place of 589 is 9
26. Program to print the digit at tens place of a
number
INPUT:-
#include <stdio.h>
int main()
int num,digit_at_tens_place;
printf("\n Enter any number: ");
scanf("%d",&num);
digit_at_tens_place=num/10%10;
printf("\n the digit at tens place of %d is%d",num,digit_at_tens_place);
return 0;
OUTPUT:-
Enter any number : 654
The digit at tens place: 5
27. Program to print the digit at hundreds place of a
number
INPUT:-
#include <stdio.h>
int main()
int num,digit_at_hundreds_place;
printf("\n Enter any number: ");
scanf("%d",&num);
digit_at_hundreds_place=num/100%10;
printf("\n the digit at hundreds place of %d is
%d",num,digit_at_hundreds_place);
return 0;
OUTPUT:-
Enter any number: 2022
The digit at hundreds place of 2022 is: 0
28. Program to swap the two numbers using
temporary operator
INPUT:-
#include <stdio.h>
#include <conio.h>
int main ()
{
int num1,num2,temp;
printf("\n Enter the first number: ");
scanf("%d",&num1);
printf("\n enter the second number:");
scanf("%d",&num2);
temp=num1;
num1=num2;
num2=temp;
printf("\n the first number is%d",num1);
printf("\n The second number is%d",num2);
return 0;
OUTPUT:-
Enter the first number: 9
Enter the second number: 11
The first number is 11
The second number is 9
29. Program to swap the two numbers without using
temporary variable
INPUT:-
#include <stdio.h>
#include <conio.h>
int main ()
int num1,num2;
printf("\n Enter the first number: ");
scanf("%d",&num1);
printf("\n enter the second number:");
scanf("%d",&num2);
num1=num1+num2;
num2=num1-num2;
num1=num1-num2;
printf("\n the first number is%d",num1);
printf("\n The second number is%d",num2);
return 0;
OUTPUT:-
Enter the first number: 9
Enter the second number: 11
The first number is 11
The second number is 9
30. Program to calculate the average of two
numbers .Also print their deviation
INPUT:-
#include <stdio.h>
#include <conio.h>
int main ()
int num1,num2;
float avg,dev1,dev2;
printf("\n Enter the two numbers:");
scanf("%d %d",&num1,&num2);
avg=(num1+num2)/2;
dev1=num1-avg;
dev2=num2-avg;
printf("\n average=%.2f",avg);
printf("\n deviation of first number=%.2f",dev1);
printf("\n deviation of second number=%.2f",dev2);
return 0;
OUTPUT:-
Enter the two numbers: 15
20
Average= 17.00
Deviation of first number= -2.00
Deviation of second number= 3.00
31.
INPUT:-
#include<stdio.h>
#include<conio.h>
main()
float fahrenheit, celsius;
printf("\n Enter the temperature in fahrenheit: ");
scanf("%f", &fahrenheit);
celsius = (0.56) * (fahrenheit - 32);
printf("\n Temperature in degrees celsius= %f", celsius);
return 0;
OUTPUT:-
Enter the temperature in Fahrenheit: 98.6
Temperature in degrees Celsius = 37.295998
32.
INPUT:-
#include<stdio.h>
#include<conio.h>
int main()
printf("\n The size of the short integer is: %d", sizeof(short int));
printf("\n The size of the unsigned integer is: %d", sizeof(unsigned int));
printf("\n The size of the signed integer is: %d", sizeof(signed int));
printf("\n The size of the integer is: %d", sizeof(int));
printf("\n The size of the long integer is: %d", sizeof(long int));
printf("\n\n The size of the character is: %d",sizeof(char));
printf("\n The size of the unsigned charaacter is: %d", sizeof(unsigned char));
printf("\n The size of the signed character is: %d", sizeof(signed char));
printf("\n\n The size of the floating point number is: %d", sizeof(float));
printf("\n The size of the double number is: %d", sizeof(double));
return 0;
}
OUTPUT:-
The size of the short integer is: 2
The size of the unsigned integer is: 4
The size of the signed integer is: 4
The size of the integer is: 4
The size of the long integer is
The size of the character is: 1
The size of the unsigned character is: 1
The size of the signed character is: 1
The size of the floating point number is: 4
The size of the double number is: 8