2.4.1 – C procedural programs to practice part-3

by subbu on August 14, 2013

Specification:
Accept the principle amount, no of years, rate of interest and print the simple interest, the total amount must be paid to the financier.

Logic:  si=p*n*r/100

#include<stdio.h>
int main()
{
int p,n;
float r,si,tot;
printf("Principle Amount:");
scanf("%d",&p);
printf("Number of years:");
scanf("%d",&n);
printf("Rate of interest:");
scanf("%f",&r);
si=p*n*r/100;
tot=p+si;
printf("Principle amount %d",p);
printf("\nSimple interest %f",si);
printf("\nTotal amount %f",tot);
return 0;
}

Execution:
Principle Amount:5200
Number of years:3
Rate of interest:8.5
Principle amount 5200
Simple interest 1326.000000
Total amount 6526.000000

Here I am going to introduce two functions pow() and sqrt() to calculate x to the power of y and square root of x. Add #include<math.h> to the program to use these functions.

#include<stdio.h>
#include<math.h>
int main()
{
double a=pow(4,5);
double b=sqrt(81);
printf("4 to the power of 5 is %.0lf",a);
printf("\nSquare root of 81 is %.0lf",b);
return 0;
}

Output:
4 to the power of 5 is 1024
Square root of 81 is 9

Specification:
Accept the base and height of a triangle and print the area

Formula:  ½ * b * h

#include<stdio.h>
int main()
{
int b,h;
float area;
printf("Breadth:");
scanf("%d",&b);
printf("Height:");
scanf("%d",&h);
area=(float)1/2*b*h;
printf("Area of triangle %f",area);
return 0;
}

Execution:
Breadth:12
Height:23
Area of triangle 138.000000

Specification:
Accept single side of equilateral triangle and print its area.

Logic: A triangle with sides of equivalent length is called equilateral triangle

Formula: √3/4 *  (a2)

#include<stdio.h>
#include<math.h>
int main()
{
int a;
float area;
printf("Enter the length of any side:");
scanf("%d",&a);
area=sqrt((float)3/4)*pow(a,2);
printf("Area of triangle %f",area);
return 0;
}

Execution:
Enter the length of any side:15
Area of triangle 194.855713

Specification:
Accept three sides of a triangle and print its area.

Formula: √ s*(s-a)*(s-b)*(s-c) where s is (a+b+c)/2 [Heron’s formula]

#include<stdio.h>
#include<math.h>
int main()
{
int a,b,c;
float s,area;
printf("Enter three sides of a triangle:\n");
scanf("%d%d%d",&a,&b,&c);
s=(a+b+c)/2.0;
area=sqrt(s*(s-a)*(s-b)*(s-c));
printf("Area of triangle %f",area);
return 0;
}

Execution:
Enter three sides of a triangle:
9 12 7
Area of triangle 31.304953

Specification:
A cashier has currency notes of 10, 50 and 100. If the amount is input through the keyboard, print denomination of currency notes the cashier has to give.

#include<stdio.h>
int main()
{
int amt;
int hs=0,ffs=0,tns=0;
printf("Enter the amount:");
scanf("%d",&amt);
hs=amt/100;/* finding 100's in amount*/
amt=amt%100;/* removing 100's in amount*/
ffs=amt/50;/*finding 50's in amount*/
amt=amt%50;/*removing 50's in amount*/
tns=amt/10;/*finding 10's in amount*/
printf("Denomination:\n");
printf("100 x %d\n50 x %d\n10 x %d",hs,ffs,tns);
return 0;
}

Execution:
Enter the amount:5460
Denomination:
100 x 54
50 x 1
10 x 1

Specification:
Accept selling price of 20 items, total profit gained by the transactions and print the purchase price of each item.

Logic:  
selling price – purchase price = profit
purchase price=selling price – profit
purchase price of each unit=purchase price/20 (no of units)

#include<stdio.h>
int main()
{
float s_price,p_price,profit,unit_price;
printf("Enter the selling price:");
scanf("%f",&s_price);
printf("Enter the profit:");
scanf("%f",&profit);
unit_price=(s_price-profit)/20;
printf("Purchase cost of each unit %.2f",unit_price);
return 0;
}

Execution:
Enter the selling price:5000
Enter the profit:1000
Purchase cost of each unit 200.00

Specification:
The population of town is 50,000. The percentage of literacy in the town is 42%. If the percentage of literate man in the town is 34% then print the total number of literate man and women.

#include<stdio.h>
int main()
{
long int population,no_literates,no_men_literates,no_wmen_literates;
population=50000;
no_literates=population*42/100;
no_men_literates=population*34/100;
no_wmen_literates=no_literates-no_men_literates;
printf("Number of men literates %ld",no_men_literates);
printf("\nNumber of women literates %ld",no_wmen_literates);
return 0;
}

Output:
Number of men literates 17000
Number of women literates 4000

Previous post:

Next post: