Вы находитесь на странице: 1из 13

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.
/* Calculation of gross salary */
#include <stdio.h>
#include <math.h>
#include <conio.h>
Void main( )
{
float bs, gs, da, hra ;
printf ( "Enter basic salary " ) ;
scanf ( "%f", &bs ) ;
if ( bs < 1500 )
{
hra = bs * 10 / 100 ;
da = bs * 90 / 100 ;
}
else
{
hra = 500 ;
da = bs * 98 / 100 ;
}
gs = bs + hra + da ;
printf ( "gross salary = Rs. %f", gs ) ;
}
If Three sides of a triangle are given, then calculate the area of a triangle.
#include <stdio.h>
#include <math.h>
#include <conio.h>
void main()
{
float a,b,c,area,s,x;
clrscr();
L : printf ("\n Give Three sides of a triangle : ");
scanf ("%f %f %f", &a,&b,&c);
printf("\n The sides are %f %f %f", a,b,c);
if((a+b)<=c || (a+c)<=b || (b+c)<=a)
{
printf ("\n Triangle can't be formed");
printf ("\n Give appropriate sides that a triangle can be formed");
goto L;
}
else
{
s=(a+b+c)/2.0;

}
getch();
}

x=s*(s-a)*(s-b)*(s-c);
area =pow(x,0.5);
printf("\n The area of the triangle : %f square unit", area);

Write a C-Program to test whether a given number is a perfect square or not.


#include <stdio.h>
#include <math.h>
#include <conio.h>
void main()
{
int x,y;
clrscr();
printf ("\n Enter any digit : ");
scanf ("%d", &x);
y = sqrt(x);
if (y*y==x)
printf ("\n %d is perfect square",x);
else
printf ("\n %d is not perfect square",x);
getch();
}
Write a C-Program to calculate the factorial of a given integer.
#include <stdio.h>
#include <conio.h>
void main()
{
clrscr();
int a,i,f=1;
l:printf ("\n get the digit from user :");
scanf ("%d", &a);
if (a<0)
{
printf ("\n Give positive integer ");
goto l;
}
if (a==0 || a==1)
printf ("\n %d is the factorial of %d ",f,a);
else
for (i=1;i<=a;i++)
{
f=f*i;
}
printf ("\n %d is the factorial of %d ",f,a);
getch();
}

NOTE :

[ if calculate the factorial of long integer (ex. 85,99,100 etc) then


write long double a, f=1 instead of int a,f=1 and use in printf
function %Lf instead of %d]

Write a C-Program to test whether a given year is a leap year or not.


#include <stdio.h>
#include <conio.h>
void main()
{
clrscr();
int a;
printf ("get the year from user :");
scanf ("%d", &a);
if (a%4==0)
{
printf ("this year is leapyear ");
}
else
{
printf ("this year is not leapyear ");
}
getch();
}
Write a program to accept a character and that whether it is in upper case or in
lower case.
#include
#include
#include
#include

<stdio.h>
<conio.h>
<stdlib.h>
<ctype.h>

void main()
{
clrscr();
char x;
printf ("\n give a character : ");
x=getchar();
if (isupper(x))
printf ("\n %c is in upper case letter ",x);
else
printf ("\n %c is not in upper case letter ",x);
getch();
}
Write a program to accept a character and that whether it is in upper case or in
lower case without using isupper().
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
void main()
{

clrscr();
char x;
printf ("\n give a character : ");
x=getchar();
if (x>='A' && x<='Z')
printf ("\n %c is in upper case letter ",x);
else
printf ("\n %c is not in upper case letter ",x);
getch();

Write a program to accept a character and that whether it is in upper case or in


lower case using user defined function.
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
int upper(char);
void main()
{

clrscr();
char x;
printf ("\n give a character : ");
x=getchar();
if (upper(x))
printf ("\n %c is in upper case letter ",x);
else
printf ("\n %c is not in upper case letter ",x);
getch();

int upper(char c)
{

if (c>='A' && c<='Z')


return(1);
return(0);

Write a program to accept a character and that whether it is in lower case or not
using user defined function.
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
int lower(char);
void main()
{
clrscr();
char x;
printf ("\n give a character : ");
x=getchar();
if (lower(x))

else

printf ("\n %c is in lower case letter ",x);

printf ("\n %c is not in lower case letter ",x);


getch();
}
int lower(char c)
{

if (c>='a' && c<='z')


return(1);
return(0);

}
Write a program to accept a character and test whether it is digit or not using user
defined function.
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
int digit(char);
void main()
{

clrscr();
char x;
printf ("\n give a character : ");
x=getchar();
if (digit(x))
printf ("\n %c is digit ",x);
else
printf ("\n %c is not a digit ",x);
getch();

}
int digit(char c)
{
if (c>='0' && c<='9')
return(1);
return(0);
}
Write a program to accept a character and test whether it is character or not using
user defined function.
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
int alpha(char);
void main()
{
clrscr();
char x;
printf ("\n give a character : ");

x=getchar();
if (alpha(x))
printf ("\n %c is alphabet ",x);
else
printf ("\n %c is not a alphabet ",x);
getch();
}
int alphabet(char c)
{

if (c>='a' && c<='z' || c>='A' && c<='Z')


return(1);
return(0);

}
Write a program to find the number of combinations that can be taking r number
of values from n number of integers using the relation : Number of combinations
= n!/r!(n-r)! (using function)
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
void main()
{
clrscr();
void comb(int x,int y);
int n,r;
printf("\n enter the value of n and r :");
scanf("%d %d",&n,&r);
if(n<=0 || r<=0 || n<=r)
{
printf("\n absured values entered, can't be done");
exit(0);
}
else
comb(n,r);
}
void comb(int x,int y)
{
long long int f1,f2,f3,i,f;
f1=f2=f3=1;
for(i=1;i<=x;i++)
f1*=i;
for(i=1;i<=y;i++)
f2*=i;
for(i=1;i<=x-y;i++)
f3*=i;
f=f1/(f2*f3);
printf("\n number of combinations = %d",f);
getch();
}

Write a program to find the largest and the smallest from the given three integers.
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
void main()
{
clrscr();
int a,b,c,max,min;
printf("\n enter any three integers :");
scanf("%d%d%d",&a,&b,&c);
if (a>b)
{
if(a>c)
max=a;
else
max=c;
}
else
{
if (b>c)
max=b;
else
max=c;
}
if(a<b)
{
if(a<c)
min=a;
else
min=c;
}
else
{
if (b<c)
min=b;
else
min=c;
}
printf("\n maximum = %d", max);
printf("\n minimum = %d", min);
getch();
}

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
Percentage
Percentage
Percentage

above or equal to 60 - First division


between 50 and 59 - Second division
between 40 and 49 - Third division
less than 40 - Fail

Write a program to calculate the division obtained by the student.


There are two ways in which we can write a program for this example. These methods are
given below.
/* Method I */
main( )
{
int m1, m2, m3, m4, m5, per ;
printf ( "Enter marks in five subjects " ) ;
scanf ( "%d %d %d %d %d", &m1, &m2, &m3, &m4, &m5 ) ;
per = ( m1 + m2 + m3 + m4 + m5 ) / 5 ;
if ( per >= 60 )
printf ( "First division ") ;
else
{
if ( per >= 50 )
printf ( "Second division" ) ;
else
{
if ( per >= 40 )
printf ( "Third division" ) ;
else
printf ( "Fail" ) ;
}
}
}
/* Method II */
main( )
{
int m1, m2, m3, m4, m5, per ;
printf ( "Enter marks in five subjects " ) ;
scanf ( "%d %d %d %d %d", &m1, &m2, &m3, &m4, &m5 ) ;
per = ( m1 + m2 + m3 + m4 + m5 ) / 5 ;
if ( per >= 60 )
printf ( "First division" ) ;
if ( ( per >= 50 ) && ( per < 60 ) )
printf ( "Second division" ) ;
if ( ( per >= 40 ) && ( per < 50 ) )
printf ( "Third division" ) ;
if ( per < 40 )
printf ( "Fail" ) ;
}

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.
There are two ways in which we can write a program for this example. One without using
logical operators and another is using logical operators.
/* Insurance of driver - without using logical operators */
main( )
{
char sex, ms ;
int age ;
printf ( "Enter age, sex, marital status " ) ;
scanf ( "%d %c %c", &age, &sex, &ms ) ;
if ( ms == 'M' )
printf ( "Driver is insured" ) ;
else
{
if ( sex == 'M' )
{
If ( age > 30 )
printf ( "Driver is
else
printf ( "Driver is
}
else
{
if ( age > 25 )
printf ( "Driver is
else
printf ( "Driver is
}
}

insured" ) ;
not insured" ) ;

insured" ) ;
not insured" ) ;

}
/* Insurance of driver - using logical operators */
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 is insured") ;
else
printf ("Driver is not insured") ;
}

An employee can apply for a loan at the beginning of every six months, but he will
be sanctioned the amount according to the following company rules :
Rule 1 : An employee cannot enjoy more than two loans at any point of time.
Rule 2 : Max permissible total loan is limited and depends upon the category of the
employee.
#include <stdio.h>
#include <conio.h>
#define MAXLOAN 50000
void main()
{

clrscr();
long int loan1,loan2,loan3,sancloan,sum23;
printf(Enter the values of previous two loans :\n);
scanf(%ld %ld, &loan1,&loan2);
printf(Enter the value of new loan :\n);
scanf(%ld, &loan3);
sum23=loan2+loan3;
sancloan = (loan1>0)? 0 : ((sum23>MAXLOAN)? MAXLOAN-loan2 : loan3);
printf(\n \n);
printf(Previous loans pending :\n%ld %ld\n, loan1, loan2);
printf(loan requested : %ld\n, loan3);
printf(loan sanctioned : %ld\n, sancloan);
getch();

}
In an office there are three grades of employees, namely grade 1, 2 and 3.
Calculate the bonus obtained by them as per the following rules :
For Grade 1 : Bonus = 10% of pay
For Grade 2 : Bonus = 12% of pay, subject to a maximum of Rs. 2500
For Grade 3 :
Pay
Bonus
Upto Rs. 5000
5% of pay
Above Rs. 5000 but upto Rs. 8000
7% of pay
Above Rs. 8000
10% of pay
#include <stdio.h>
#include <conio.h>
void main()
{
int grade;
float pay, bonus;
printf( Enter your pay please in Rs. : );
scanf(%f,&pay);
fflush(stdin);
printf( \nEnter your grade please (1/2/3) : );
scanf(%d,&grade);
switch (grade)
{

case 1 :
bonus=0.1*pay;
break;
case 2 :
bonus=0.05*pay;
if (bonus>2500)
bonus=2500;
break;
case 3 :
if (pay<=5000)
bonus=0.05*pay;
if (pay>5000 && pay<=8000)
bonus=0.07*pay;
if (pay>8000)
bonus=0.1*pay;
break;
default :
printf(\nAbsurd Grade Entered);

}
printf(\nYour Bonus Amount Rs = %f, bonus);
getch();

In a hotel there are three floors and in each floor there are four types of room
available having different room charges given below. Write a program to print the
room charges by entering the floor number and class of room. The room charges
are as follows :
In floor 1
A Class = Rs. 750
B Class = Rs. 600
C Class = Rs. 500
In floor 2
A Class = Rs. 1200
B Class = Rs. 1000
M Class = Rs. 300
In floor 3
1 Class = Rs. 2500
2 Class = Rs. 1800
A Class = Rs. 1200
#include <iostream.h>
#include <conio.h>
#include <stdlib.h>
void main()
{
clrscr();
int floor, charge;
char c1;
cout<<"Enter the Floor Number (1/2/3) : ";
cin>>floor;
cout<<" \nEnter Class of Room (A/B/C/M/1/2) : ";
cin>>c1;
switch (floor)

case 1 :
switch(c1)
{
case 'A' :
case 'a' :
charge=750;
break;
case 'B' :
case 'b' :
charge=600;
break;
case 'C' :
case 'c' :
charge=500;
break;
default :
cout<<"\nAbsurd Class of Room";
getch();
exit(0);
}
break;
case 2 :
switch(c1)
{
case 'A' :
case 'a' :
charge=1200;
break;
case 'B' :
case 'b' :
charge=1000;
break;
case 'M' :
case 'm' :
charge=300;
break;
default :
cout<<"\nAbsurd Class of Room";
getch();
exit(0);
}
break;
case 3 :
switch(c1)
{
case '1' :
charge=2500;
break;
case '2' :
charge=1800;
break;
case 'A' :

}
break;

case'a' :
charge=1200;
break;
default :
cout<<"\nAbsurd Class of Room";
getch();
exit(0);

}
cout<<"\nRoom Charges is Rs = "<<charge;
getch();
}

Вам также может понравиться