Assignment 4
a. Write a program to exchange content of 2 variable
Ans.
1. using arithmatic operator
code :
main() {
int a = 5;
int b = 6;
printf("\n value of a is %d and value of b is %d", a, b);
a = a + b;
b = a - b;
a = a - b;
printf("\n after swapping \n value of a is %d and value of b is %d \n", a,
b);
}
output :
value of a is 5 and value of b is 6
after swapping
value of a is 6 and value of b is 5
2. Without using arithmatic operator
code :
main(){
int temp;
int a = 45;
int b = 33;
printf("\n value of a is %d and value of b is %d",a,b);
temp = a;
a = b;
b =temp;
printf("\n after swapping \n value of a is %d and value of b is %d
\n",a,b);
}
output :
value of a is 45 and value of b is 33
after swapping
value of a is 33 and value of b is 45
b. Write a program that takes input of distance between two cities in
Kilometer from keyboard and display this distance in meter.
Ans.
code :
main() {
float distance = 0.000;
printf("\n enter distance between the two cities : ");
scanf("%f", &distance);
printf("\n distance in meter will be %f m", distance * 1000);
}
output :
run1.
enter distance between the two cities in Km : 45
distance in meter will be 45000.000000 m
run2.
enter distance between the two cities in Km : 350.432
distance in meter will be 350432.000000 m
c. Read temprature of a city in Fahrenheit . Write a program to convert
the tremprature to celsius and kelvin.
code :
main(){
float deg_far,deg_cel,kel;
printf("enter temperature of city in degFahrenheit : ");
scanf("%f",°_far);
deg_cel = ((deg_far-32)*5)/9;
kel = deg_cel + 273.15;
printf("temprature of city in \n Fahrenheit : %.1f degF\n Celsius : %.1f
degC\n Kelvin : %.1f K\n",deg_far,deg_cel,kel);
}
output :
enter temperature of city in degFahrenheit : 54.2
temprature of city in
Fahrenheit : 54.2 degF
Celsius : 12.3 degC
Kelvin : 285.5 K
d. write a program to read marks of a student in 5 different subjects
and display the aggregate mark.
code :
main(){
int c,cpp,python,java,javascript,aggregate;
printf(" marks in c : ");
scanf("%d",&c);
printf("\n marks in cpp : ");
scanf("%d",&cpp);
printf("\n marks in python : ");
scanf("%d",&python);
printf("\n marks in java : ");
scanf("%d",&java);
printf("\n marks in javascript : ");
scanf("%d",&javascript);
aggregate = (c+cpp+python+java+javascript)/5;
printf("\n aggregate marks : %d\n",aggregate);
}
output :
marks in c : 43
marks in cpp : 76
marks in python : 26
marks in java : 34
marks in javascript : 09
aggregate marks : 37
e. Write a program to read the basic salary of employee from keyboard
where dearness allowance is 70% of base salary, house rent is 20% of
base salary, medical allowance 10% of base salary,for provident fund
7% of base salary, for professional task rs200 will be deducted. Find
out the employee's gross salary.
code :
main(){
int base_salary, dearnesa_allowance, house_rent ,medical_allowance,
provident_fund, gross_salary , professional_task = 200;
printf(" Enter base salary of the employee \n");
scanf("%d",&base_salary);
dearnesa_allowance = base_salary * 0.70;
house_rent = base_salary * 0.20;
medical_allowance = base_salary * 0.10;
provident_fund = base_salary * 0.07;
gross_salary = base_salary+ dearnesa_allowance + house_rent +
medical_allowance - provident_fund -professional_task;
printf(" salary intimation \n base salary %d \n dearness allowence %d \n
house rent %d \n medical allownce %d\n provident fund %d \n professional task
%d \n\n gross salary %d
\n",base_salary,dearnesa_allowance,house_rent,medical_allowance,provident_fund,
professional_task,gross_salary);
}
output :
Enter base salary of the employee
45000
salary intimation
base salary 45000
dearness allowence 31499
house rent 9000
medical allownce 4500
provident fund 3150
professional task 200
gross salary 86649
f. Read a 4 digit number from keyboard write a program that adds 1st
and 4th digit.
code :
#include<stdio.h>
int main(){
int number;
int digit_1st, digit_4th;
printf(" Enter the 4-digit number : ");
scanf("%d",&number);
digit_1st = number / 1000;
digit_4th = number % 10;
printf(" First number is %d and fourth number is %d and their sum is
%d",digit_1st,digit_4th,digit_1st+digit_4th);
}
output :
Enter the 4-digit number : 4326
First number is 4 and fourth number is 6 and their sum is 10