2
2
PRACTICAL NO. : 2
int main ()
float basic, da, hra, medical, gross, pf, insurance, deduction, netsalary ;
scanf("%f”, &basic);
da=0.5*basic;
hra=0.1*basic;
medical+0.04*basic;
gross=basic+da+hra+medical;
pf=0.05*gross;
insurance=0.07*gross;
deduction=insurance+pf;
netsalary=gross-deduction;
printf("%f",netsalary);
return 0;
}
c.) The driver is driving a car from city Ahmedabad to city Mumbai, in
Ahmedabad temperature displays in Celsius while in Mumbai the
temperature displayed in Fahrenheit, a driver wants to find the
difference between the temperatures of two cities in Celsius.
#include<stdio.h>
int main()
float a,b,c,d,e;
scanf("%f",&a);
scanf("%f",&b);
c=b-32;
d=c/1.8;
e=a-d;
printf("%f",e);
return 0;
}
n
Theoretical Principles used : In this program we use float data type because during
Fahrenheit to Celsius conversion we can get decimal values and then by using scanf function
we took values from user and by the appropriate formula we get the desired results.
d.) To calculate simple interest.
#include<stdio.h>
int main()
{
float p, r, t, si;
printf("enter principal amount");
scanf("%f" ,&p);
printf("enter rate");
scanf("%f" ,&r);
printf("enter time");
scanf("%f" ,&t);
si=p*r*t/100;
printf("simple interest=");
printf("%f”, si);
return 0;
}
Theoretical Principles used : By using printf and scanf function take values
from user and then apply it to the simple interest formula to get results.
e.) A boy was punished and asked to cover 5 rounds of the
circular ground. Area of the ground is 32000 sqmtr. Calculate
how many e the boy has covered.
#include<stdio.h>
#include<math.h>
int main ()
{
float area, radsq, rad, circum, dist;
area=32000;
radsq=area/3.14;
rad=sqrt(radsq);
circum=2*3.14*rad;
dist=circum*5;
printf("distance covered=");
printf("%f",dist);
return 0;
}
Theoretical Principles used : First define and put value of are then find radius
by mathematical formula the by using printf function display the desired
results.
F.) Read the price of item in decimal form. For example, 12.52 and
separate rupee and paise from the given value. For example, 12
rupees and 52 paise.
#include<stdio.h>
int main()
int a;
float x,b,p;
printf("enter a value");
scanf("%f",&x);
a=x;
printf("rupees");
printf("%d",a);
b=x-a;
p=b*100;
printf("%f",p);
printf("paisa");
return 0;
}
Theoretical Principles used: Tale a value from user and then store it in a
variable ‘a’. Then print ‘a’ since a is an integer type of variable so it will display
integer value which is in rupees and then take another variable ‘b’ and subtract x
from a since x is in float and a is integer it will give decimal value then multiply that
value with 100 you will get paise value.
g.) To swap the value of two numbers
➢ using a temporary variable
#include<stdio.h>
int main()
{
int
a=20,b=30,temp;
temp=a;
a=b;
b=temp;
printf("The value of a is %d\n",a);
printf("The value of b is %d\n",b);
return 0;
}
Theoretical Principles used: Enter the values of a and b and initialize a
temporary variable . Firstly store value of a in temporary variable then equal
it to b and then again equal it to temporary variable .By this we swap values
using temporary variable.
➢ Without using a temporary variable
#include<stdio.h>
int main()
{
int a=10;
int b=30;
a=a+b;
b=a-b;
a=a-b;
printf("b is %d\n",b);
printf("a is %d",a);
return 0;
}
Theoretical Principles used: First int a and b then with the help of
arithmetic operators perform some operations on two numbers and store
them in each other, you will get that the two numbers are swapped.
h.) To find greatest of two numbers using the ternary operator.
#include<stdio.h>
int main ()
{
int a, b, c;
a=20;
b=30;
c=40;
a>b?(a>c?printf("a is greatest"):(printf("c is greatest"))):()b>c?printf("b is
greatest"):(printf("c is greatest"))));
return 0;
}
Theoretical Principles used: Int 3 values a , b , c then by using ternary
operator assign two equations that is given condition is true then print this or
false then print that by using a semi colon.