22CLK038 LPW2-1

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

LPW-2

Computer Programming
Roll no.: 22CLK038
Name: DEV PATEL

Q2: C programs to illustrate working of various operators

(a). Scan two numbers and display result of different arithmetic operations
(+, - , *, / and %).
• Code
#include <stdio.h>

int main()

int a,b,sum,diff,pro,divi,mod;

printf("enter integer a\n");

scanf("%d",&a);

printf("enter integer b\n");

scanf("%d",&b);

sum=a+b;

diff=a-b;

pro=a*b;

divi=a/b;

mod=a%b;
printf("\nthe sum of %d and %d is %d",a,b,sum);

printf("\nthe difference of %d and %d is %d",a,b,diff);

printf("\nthe product of %d and %d is %d",a,b,pro);

printf("\nthe division of %d and %d is %d",a,b,divi);

printf("\nthe reminder on dividing %d and %d is %d",a,b,mod);

return 0;

• Run Program
(b). 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. (Celsius = (F-32) *(5/9))

• Code
#include <stdio.h>
int main()
{
float b,mf,mc,diff;
printf("enter ahmedabad temp in celsius\n");
scanf("%f",&b);
printf("enter mumbai temp in fahrenheit\n");
scanf("%f",&mf);
mc=(5*mf-160)/9;
diff=b-mc;
printf("\ndifference in temp of ahmedabad and mumbai in celsius is
%f",diff);
return 0;
}

• Run Program


(d). A company has following scheme for payment to their staff.


• Net salary = Gross salary – Deduction
• Gross salary = Basic + DA + HRA + Medical
• Deduction = Insurance + PF
• DA (Dearness allowance) = 50% of Basic
• HRA (House rent allowance) = 10% of Basic
• Medical = 4% of Basic
• PF (Provident Fund) = 5% of Gross
• Insurance = 7% of Gross
Calculate the net payment to any employee.

• Code

#include <stdio.h>
int main()
{
float a,da,hra,medical,pf,insu,deduc,gross,net;
printf("enter basic salary\n");
scanf("%f",&a);
da=a*0.5;
hra=a*0.1;
medical=a*0.04;
gross=a+da+hra+medical;
pf=gross*0.05;
insu=gross*0.07;
deduc=insu+pf;
net=gross-deduc;
printf("\n net salary is %f",net);
printf("\n gross salary is %f",gross);
printf("\n total deduction in salary is %f",deduc);
printf("\n pf is %f",pf);
printf("\n da is %f",da);
printf("\n hrais %f",hra);
printf("\n insu is %f",insu);
printf("\n medical is %f",net);
return 0;
}

• Run Program
(e). 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.

• Code
#include <stdio.h>
#include <math.h>
int main()
{float raw;
int rup,pai;
printf("“enter price\n");
scanf("%f",&raw);
rup= raw;
pai=(raw-rup)*100;
printf("%d””rupees and “”%d””paise", rup,pai);
return 0;
}

• Run program
(f). To swap the value of two numbers (i) using and (ii) without using a
temporary variable.

• Code using temporary variabl


#include<stdio.h>
int main()
{
int x, y, temp;
printf("Enter the value of x and y: ");
scanf("%d %d", &x, &y);
printf("Before swapping x=%d, y=%d ", x, y);
temp = x;
x = y;
y = temp;
printf("After swapping x=%d, b=%d", x, y);
return 0;
}

• Run Program
• Code without using temporary variabl

#include <stdio.h>
int main()
{
int a=10,b=20,temp;
printf("\n before swap a=%d,b=%d ",a,b);
//swap steps using arithmetic operation;
a=a+b;
b=a-b;
a=a-b;
printf("\n after swap a=%d,b=%d",a,b);
return 0:
}
Run Program
(g). To find greatest of two and three numbers using the ternary operator.

• Code
#include <stdio.h>
int main()
{
int a,b,c,g;
printf("enter the value of a");
scanf ("%d",&a);
printf("enter the value of b");
scanf ("%d",&b);
printf("enter the value of c");
scanf ("%d",&c);
g = ("(a>b)?(a>c)?(a>c)(b>c)?(b>c)?");
printf("the value of g is %d",g);
return 0;
}
Run Program

You might also like