C PROGRAMS
1. Write a program to calculate simple interest.
A.
Input: #include <stdio.h>
main( )
{
int p, n ;
float r, si ;
p = 1000 ;
n=3;
r = 8.5 ;
/* formula for simple interest */
si = p * n * r / 100 ;
printf ( "%f" , si ) ;
}
Output: 255.000000
B.
Input: #include <stdio.h>
main( )
{
int p, n ;
float r, si ;
printf ("enter the principal, no. of years, rate of
interest:");
scanf ("%d %d %f", &p, &n, &r);
/* formula for simple interest */
si = p * n * r / 100 ;
printf ( "%f" , si ) ;
}
Output: enter the principal, no. of years, rate of interest:1000
10
2.8
280.000000
2. A fun program
Input: #include <stdio.h>
/* Just for fun*/
main( )
{
int num ;
printf ( "Enter a number" ) ;
scanf ( "%d", &num ) ;
printf ( "Now I am letting you on a secret..." ) ;
printf ( "You have just entered the number %d", num ) ;
}
Output: Enter a number12
Now I am letting you on a secret...You have just entered the
number 12
3. Even or Odd
A. Using if else statement:
Input: #include <stdio.h>
int main( )
{
int n;
printf ("enter the number");
scanf("%i", &n);
if (n%2 == 0)
printf ( "the number you have entered is even");
else
printf ( "the number you have entered is odd");
return 0;
}
Output: enter the number42
the number you have entered is even
B. Switch case statement
Input: #include <stdio.h>
int main( )
{
int n, x;
printf ("enter the number");
scanf("%i", &n);
x = n % 2;
switch (x)
{
case 0:
printf ("The number is even");
break;
default:
printf ("The number is odd");
break;
}
}
Output: enter the number33
The number is odd
4. Which is maximum
Input: #include <stdio.h>
int main( )
{
int x, y;
printf ("enter the number x");
scanf("%i", &x);
printf ("enter the number y");
scanf("%i", &y);
if (x>y)
printf ("%d is maximum", x);
else
printf ("%d is maximum", y);
return 0;
}
Output: enter the number x10
enter the number y7
10 is maximum
5. Maximum of 3 numbers
Input: #include <stdio.h>
int main( )
{
int x, y, z;
printf ("enter the number x");
scanf("%i", &x);
printf ("enter the number y");
scanf("%i", &y);
printf ("enter the number z");
scanf("%i", &z);
if (x>y)
{
if (z>x)
printf ("%d is maximum", z);
else
printf ("%d is maximum", x);
}
else
{
if (z>y)
printf ("%d is maximum", z);
else
printf ("%d is maximum", y);
}
return 0;
}
Output: enter the number x5
enter the number y9
enter the number z6
9 is maximum
* It can also be done by bitwise operator
If (z>y && z>x)
Printf (z is maximum) similarly.
6. Second maximum against 3 numbers
Input:
#include <stdio.h>
int main( )
{
int x, y, z;
printf ("enter the number x");
scanf("%i", &x);
printf ("enter the number y");
scanf("%i", &y);
printf ("enter the number z");
scanf("%i", &z);
if (x>y)
{
if (z>x)
printf ("%d is second maximum", x);
else if (y>z)
printf ("%d is second maximum", y);
else
printf ("%d is second maximum", z);
}
else
{
if (z>y)
printf ("%d is second maximum", y);
else
printf ("%d is second maximum", z);
}
return 0;
}
Output:
enter the number x756
enter the number y754
enter the number z755
755 is second maximum
7. Trenary operator
#include <stdio.h>
#include <math.h>
int main( )
{
int x, y;
int max;
printf ( "Enter the number x");
scanf ("%i", &x);
printf ( "Enter the number y");
scanf ("%i", &y);
max = x>y?x:y;
printf ( "The maximum is %d", max);
return 0 ;
}
Enter the number x6
Enter the number y9
The maximum is 9
8. Maximum of three numbers using ternary operator
#include <stdio.h>
#include <math.h>
int main( )
{
int x, y, z;
int max;
printf ( "Enter the number x");
scanf ("%i", &x);
printf ( "Enter the number y");
scanf ("%i", &y);
printf ( "Enter the number z");
scanf ("%i", &z);
max = x>y && x>z ? x : y>x && y>z ? y : z;
printf ( "The maximum is %d", max);
return 0 ;
}
Enter the number x3
Enter the number y5
Enter the number z4
The maximum is 5
9. Second maximum using ternary operator
#include <stdio.h>
#include <math.h>
int main( )
{
int x, y, z;
int max;
printf ( "Enter the number x");
scanf ("%i", &x);
printf ( "Enter the number y");
scanf ("%i", &y);
printf ( "Enter the number z");
scanf ("%i", &z);
max = x>y && z>x ? x : x>y && y>z ? y : x>y ? z : y>x && z>y ? y : z;
printf ( "The second maximum is %d", max);
return 0 ;
}
Enter the number x3
Enter the number y6
Enter the number z4
The second maximum is 4
10. Demonstration of if Statement
/* Demonstration of if statement */
# include <stdio.h>
int main( )
{
int num ;
printf ( "Enter a number less than 10 " ) ;
scanf ( "%d", &num ) ;
if ( num < 10 )
printf ( "What an obedient servant you are !\n" ) ;
return 0 ;
}
Enter a number less than 10 3
What an obedient servant you are !
11. Example 3.1: While purchasing certain items, a discount of 10% is
offered if the quantity purchased is more than 1000. If quantity and
price per item are input through the keyboard, write a program to calculate the total
expenses.
/* Demonstration of if statement */
# include <stdio.h>
int main( )
{
int quan;
float rate, dis, total ;
printf ( "Enter the quantity " ) ;
scanf ( "%d", &quan ) ;
printf ( "Enter the price per item " ) ;
scanf ( "%f", &rate ) ;
if ( quan > 1000 )
{
printf ( "you have got a discount of 10%\n" ) ;
dis = 10*rate*quan/100;
}
else
dis = 0;
total = rate*quan - dis;
printf ( "Your total expense is %f", total ) ;
return 0 ;
}
Enter the quantity 100
Enter the price per item 250
Your total expense is 25000.000000
Or
Enter the quantity 1002
Enter the price per item 25
you have got a discount of 10
Your total expense is 22545.000000
12. Example 3.2: The current year and the year in which the employee
joined the organization are entered through the keyboard. If the
number of years for which the employee has served the organization is
greater than 3, then a bonus of Rs. 2500/- is given to the employee. If
the years of service are not greater than 3, then the program should do
nothing.
# include <stdio.h>
int main( )
{
int yojoin, cy, bonus, noy;
printf ( "Enter the year of joining " ) ;
scanf ( "%d", &yojoin ) ;
printf ( "Enter the current year " ) ;
scanf ( "%d", &cy ) ;
noy = cy - yojoin;
if ( noy > 3 )
{
bonus = 2500;
printf ( "you have got a bonus of %d\n", bonus ) ;
}
return 0 ;
}
Enter the year of joining 2012
Enter the current year 2021
you have got a bonus of 2500
Or
Enter the year of joining 2020
Enter the current year 2021
13. Example 3.3: In a company an employee is paid as under:
If his basic salary is less than Rs. 1500, then HRA = 10% of basic salary
and DA = 90% of basic salary. If his salary is either equal to or above Rs.
1500, then HRA = Rs. 500 and DA = 98% of basic salary. If the employee's
salary is input through the keyboard write a program to find his gross
salary.
# include <stdio.h>
int main( )
{
float bs, hra, da, gs;
printf ( "Enter your basic salary " ) ;
scanf ( "%f", &bs ) ;
if ( bs < 1500 )
{
hra = 10*bs/100;
da = 90*bs/100;
gs = bs+da+hra;
printf ( "your gross salary is %f\n", gs ) ;
}
else
{
hra = 500;
da = 98*bs/100;
gs = bs+da+hra;
printf ( "your gross salary is %f\n", gs ) ;
}
return 0 ;
}
Enter your basic salary 1200
your gross salary is 2400.000000
14. Example 4.1: The marks obtained by a student in 5 different subjects are input
through the keyboard. The student gets a division as per the following rules:
Percentage above or equal to 60 - First division
Percentage between 50 and 59 - Second division
Percentage between 40 and 49 - Third division
Percentage less than 40 - Fail
Write a program to calculate the division obtained by the student.
# include <stdio.h>
# include <math.h>
int main( )
{
float m1, m2, m3, m4, m5, per;
printf ("Enter the marks in 5 different subjects ");
scanf ("%f%f%f%f%f", &m1, &m2, &m3, &m4, &m5);
per = (m1+m2+m3+m4+m5)*100/500;
if (per >= 60)
printf("First division");
else
{if (per >= 50 && per <= 59)
printf("Second division");
else
{
if (per >= 40 && per <= 49)
printf("First division");
else
printf("fail");
}
}
return 0;
}
Enter the marks in 5 different subjects 56 67 78 89 90
First division
15. Example 4.2: A company insures its drivers in the following cases:
If the driver is married.
If the driver is unmarried, male & above 30 years of age.
If the driver is unmarried, female & above 25 years of age.
In all other cases, the driver is not insured. If the marital status, sex and age of the driver
are the inputs, write a program to determine whether the driver should be insured or not
# include <stdio.h>
# include <math.h>
int main( )
{
int age;
char s, ms;
printf ("Enter the age ");
scanf ("%d", &age);
printf ("Enter the sex ");
scanf (" %c", &s);
printf ("Enter the marital status ");
scanf (" %c", &ms);
if (ms == 'M' )
printf ("The driver will be insured");
else
{
if (s == 'M')
{
if (age>=30)
printf ("The driver will be insured");
else
printf ("The driver will not be insured");
}
else
{
if (age>=25)
printf("The driver will be insured");
else
printf("The driver will not be insured");
}
}
return 0 ;
}
Or
/* Insurance of driver - using logical operators */
# include <stdio.h>
int main( )
{
char sex, ms ;
int age ;
printf ( "Enter age, sex, marital status " ) ;
scanf ( "%d %c %c", &age, &sex, &ms ) ;
if ( ( ms == 'M') || ( ms == 'U' && sex == 'M' && age > 30 ) || ( ms == 'U' && sex == 'F'
&& age > 25 ) )
printf ( "Driver should be insured\n" ) ;
else
printf ( "Driver should not be insured\n" ) ;
return 0 ;
}
Enter the age 20
Enter the sex M
Enter the marital status M
The driver will be insured
Or
Enter the age 30
Enter the sex M
Enter the marital status U
The driver will be insured
Or
Enter the age 20
Enter the sex F
Enter the marital status U
The driver will not be insured
16. Example 4.3: Write a program to calculate the salary as per the
following table:
# include <stdio.h>
# include <math.h>
int main( )
{
int yos, salary, qual;
char s;
printf ("Enter the years of service ");
scanf ("%d", &yos);
printf ("Enter the sex ");
scanf (" %c", &s);
printf ("Enter the qualification (for PG=1, G=0)");
scanf (" %d", &qual);
if ((s=='M' && yos>=10 && qual==1))
printf ("The salary is 15000");
else if ((s=='M' && yos>=10 && qual==0))
printf ("The salary is 10000");
else if ((s=='M' && yos<10 && qual==1))
printf ("The salary is 10000");
else if ((s=='M' && yos<10 && qual==0))
printf ("The salary is 7000");
else if ((s=='F' && yos>=10 && qual==1))
printf ("The salary is 12000");
else if ((s=='F' && yos>=10 && qual==0))
printf ("The salary is 9000");
else if ((s=='F' && yos<10 && qual==1))
printf ("The salary is 10000");
else if ((s=='F' && yos<10 && qual==0))
printf ("The salary is 6000");
return 0 ;
}
Enter the years of service 11
Enter the sex F
Enter the qualification (for PG=1, G=0)1
The salary is 12000
17. Example 6.1: Write a program to determine whether a number is prime
or not. A prime number is said to be prime if it is divisible only by 1 or
itself.
# include <stdio.h>
int main( )
{
int num, i;
printf("Enter the number ");
scanf("%d", &num);
if (num==1)
printf("%d is a special number", num);
for (i=2; i<=(num-1); i++)
{
if (num%i==0)
break;
}
if (i==num)
printf("%d is a prime number", num);
else
printf("%d is not a prime number", num);
return 0;
}
Enter the number 7
7 is a prime number