C PROGRAMS Code
C PROGRAMS Code
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 ) ;
}
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;
}
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;
}
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;
}
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 ;
}
Or
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 ;
}
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 ;
}
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;
}
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
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 ;
}