0% found this document useful (0 votes)
6 views18 pages

2

The document outlines various C programming exercises aimed at demonstrating different programming concepts such as arithmetic operations, salary calculations, temperature conversions, simple interest calculations, distance covered in a circular ground, price separation into rupees and paise, swapping values of two numbers, and finding the greatest of three numbers using a ternary operator. Each exercise includes a code snippet and a brief explanation of the theoretical principles used. The practical exercises serve as a hands-on approach to learning fundamental programming skills.

Uploaded by

nidhibangar2245
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views18 pages

2

The document outlines various C programming exercises aimed at demonstrating different programming concepts such as arithmetic operations, salary calculations, temperature conversions, simple interest calculations, distance covered in a circular ground, price separation into rupees and paise, swapping values of two numbers, and finding the greatest of three numbers using a ternary operator. Each exercise includes a code snippet and a brief explanation of the theoretical principles used. The practical exercises serve as a hands-on approach to learning fundamental programming skills.

Uploaded by

nidhibangar2245
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

DATE : 13 /10/2022

ROLL NO. : 22CMP075

COURSE CODE AND NAME : 1CS501 COMPUTER


PROGRAMMING

PRACTICAL NO. : 2

AIM : C program to demonstrate various operators.


a. To scan two numbers and display result of different
arithmetic operations ( + , - , * , / , % )
#include<stdio.h>
#include<stdlib.h>
int main()
{
int a,b;
printf("Enter the first number\n");
scanf("%d",&a);
printf("Enter the second number\n");
scanf("%d",&b);
printf("a/b=%d\n",a/b)
return 0;
}
b.) 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.


#include<stdio.h>

int main ()

float basic, da, hra, medical, gross, pf, insurance, deduction, netsalary ;

printf("enter value of basic");

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;

printf("enter temperature of ahmedabad in C and mumbai in F");

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.

You might also like