0% found this document useful (0 votes)
49 views

C++ Assignment PDF

The document contains 13 programming problems involving calculations using various formulas. Each problem includes an algorithm describing the steps, a C program implementing the algorithm, and sample output. The problems cover topics like calculating velocity, distance, conversions between units, taxes, discounts, batting averages, and calculating totals on bills.

Uploaded by

Anantha Vijay
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)
49 views

C++ Assignment PDF

The document contains 13 programming problems involving calculations using various formulas. Each problem includes an algorithm describing the steps, a C program implementing the algorithm, and sample output. The problems cover topics like calculating velocity, distance, conversions between units, taxes, discounts, batting averages, and calculating totals on bills.

Uploaded by

Anantha Vijay
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/ 42

C Problem Solving Assignment 1

Problem 1:

Algorithm:

Step 1 − START
Step 2 − declare five integers u, v, a, t, s
Step 3 − take input for u,a,t (initial velocity,acceleration,time)
Step 4 − using u,a,t find v,s by using the respective formula
v = u+at (final velocity)
s = ut+1/2at^2 (distance)
Step 5 − print v and s
Step 6 − STOP

Program:

#include <stdio.h>

int main(void) {

// Variable declaration
float v, u, a, t, s;
// Input for variables
printf("Input");
printf("\n\nInitial Velocity(u) : ");
scanf("%f", &u);
printf("Acceleration(a) : ");
scanf("%f", &a);
printf("Time(t) : ");
scanf("%f", &t);

// Calculation
s = u*t + 0.5*a*t*t;
v = u + a*t;

// Output
printf("\nOnput");
printf("\n\nFinal Velocity(v) : %f",v);
printf("\nDistance travelled(s) : %f",s);

return 0;
}
Output:
Problem 2:

Algorithm:

Step 1 − START
Step 2 − declare 4 variables feet, inch ,length1 ,length2
Step 3 − take input for feet and inch
Step 4 − convert feet and inch to cm and store it in length 1 and 2
respectively
Step 5 − print length 1 and 2
Step 6 − STOP

Program:

#include <stdio.h>

int main(void) {
// Variable declaration
float feet, inch, length1, length2;

// Input for variables


printf("Input");
printf("\n\nLength in Feet : ");
scanf("%f", &feet);
printf("Length in Inch : ");
scanf("%f", &inch);

/* 1 inch = 2.54 cm
1 feet = 30.48 cm */

// Conversion
length1 = feet*30.48;
length2 = inch*2.54;

// Output
printf("\nOnput");
printf("\n\nFeet to Cm : %f",length1);
printf("\nInch to Cm : %f",length2);
return 0;
}

Output:

Problem 3:

Algorithm:

Step 1 − START
Step 2 − declare 5 variables start, end, distance , mile, reimbursement
Step 3 − take input for start and end (beginning odometer and ending
odometer reading)
Step 4 − calculate distance: distance (distance travelled) = end-start
Step 5 − distance calculated in the previous step will be in km
So, km should be converted to mile
mile = distance*0.621371 (distance travelled in miles)
Step 6 − Calculate reimbursement cost
reimbursement = mile*0.35
Step 7 - print reimbursement
Step 8 - STOP

Program:

#include <stdio.h>

int main(void) {

// Variable declaration
float start, end, distance, mile, reimbursement;

// Input for variables


printf("MILEAGE REIMBURSEMENT CALCULATOR");
printf("\n\nEnter the beginning odometer reading(km) : ");
scanf("%f", &start);
printf("Enter the ending odometer reading(km) : ");
scanf("%f", &end);

// Calculation
distance = end-start; // distance is in km
mile = distance*0.621371; // km to miles
reimbursement = mile*0.35;
// Output
printf("\nYou traveled %f miles, at Rs. 0.35 per mile. ",mile);
printf("\nYour reimbursement is Rs. %f",reimbursement);

return 0;
}

Output:

Problem 4:

Algorithm:

Step 1 − START
Step 2 − Calculate the number of acres in 389767 square feet (divide by
43,560)
Step 3 − print the result
Step 4 − STOP

Program:

#include <stdio.h>

int main(void) {

// Variable declaration
float acre;
float given = 389767;

// Calculation
// 1 acre of land is equivalent to 43,560 square feet.

acre = given/43560 ;

// Output
printf("\nNumber of acres in an area of land with 389,767 square feet is
%f",acre);

return 0;
}

Output:
Problem 5:

Algorithm:

Step 1 − START
Step 2 − declare 3 variables selling price, cost price, profit
Step 3 − assign values 40, 1200.67 to profit and cost price respectively
Step 4 − calculate selling price by the formula:
selling price = (100 + profit) * cost price / 100
Step 5 − print selling price
Step 6 − STOP

Program:

#include <stdio.h>

int main(void) {

// Variable declaration
float sellingprice, costprice;
int profit;

// Variable initialization
profit = 40;
costprice = 1200.67;

// Calculation
sellingprice = (100 + profit)* costprice / 100;

// Output
printf("\nSelling price of the ciruit board = %f",sellingprice);

return 0;
}

Output:

Problem 6:

Algorithm:
Step 1 − START
Step 2 − declare and assign values for name,address,telephone number,
college major
Step 3 − print the declared variables
Step 4 − STOP

Program:

#include <stdio.h>

int main(void) {

// Variable declaration and initiation


char name[] = "M.Anantha Vijay";
char address[] = "R.K.Nagar,Hosur,Tamilnadu 635109";
char collegemajor[] = "Msc";
int telephone = 1234567890;

// Output
printf("Name : %s \nAddress : %s \nCollege Major : %s \nTelephone
number : %d ",name,address,collegemajor,telephone);

return 0;
}
Output:

Problem 7:

Algorithm:

Step 1 − START
Step 2 − declare 3 variables selling price, commission, amount
Step 3 − take input for selling price
Step 4 − calculate commission by the formula:
commission = selling price*8/100
Step 5 – calculate amount by the formula:
amount = selling price - commission
Step 6 − print commission and amount
Step 7 − STOP

Program:
#include <stdio.h>

int main(void) {

// Variable declaration
float sellingprice,commission,amount;

// Input for variables


printf("\nSold amount : Rs. ");
scanf("%f", &sellingprice);

// Calculation
commission = sellingprice*0.08;
amount = sellingprice - commission;

// Output
printf("\nSalesperson’s commission is Rs. %f",commission);
printf("\nThe amount the company receives after the commission is Rs.
%f",amount);

return 0;
}

Output:
Problem 8:

Algorithm:

Step 1 − START
Step 2 − declare 4 variables purchase price, state tax, country tax ,total
tax
Step 3 − assign value 1000 to purchase price
Step 4 − calculate state tax by the formula:
State tax = purchase price * 4 / 100
Step 5 − calculate country tax by the formula:
country tax = purchase price * 2 / 100
Step 6 − calculate total tax by the formula:
total tax = state tax + country tax
Step 7 − print state tax, country tax, total tax
Step 8 − STOP

Program:
#include <stdio.h>

int main(void) {

// Variable declaration and variable initialization


int purchaseprice = 1000;
int statetax,countrytax,totaltax;

// Calculation
statetax = purchaseprice*4/100;
countrytax = purchaseprice*2/100;
totaltax = statetax + countrytax;

// Output
printf("Purchase price : %d",purchaseprice);
printf("\nState sales tax : %d",statetax);
printf("\nCountry sales tax : %d",countrytax);
printf("\nTotal sales tax : %d",totaltax);

return 0;
}

Output:
Problem 9:

Algorithm:

Step 1 − START
Step 2 − declare 2 variables fahrenheit and centigrade
Step 3 − take input for centigrade
Step 4 − calculate centrigade to farenheit by the formula:
farenheit = 32 + centigrade*9/5
Step 5 − print farenheit
Step 6 − STOP

Program:

#include <stdio.h>

int main(void) {
// Variable declaration
float centigrade,farenheit;

// Input for variables


printf("Enter temperature reading in centigrade : ");
scanf("%f", &centigrade);

// Calculation
farenheit = 32 + centigrade*9/5; //F=32+C*(180.0/100.0)

// Output
printf("\nTemperature reading in farenheit = %f",farenheit);

return 0;
}

Output:

Problem 10:
Algorithm:

Step 1 − START
Step 2 − declare variables price, discount, no, sales tax, cost, discount
amount, discounted cost, sales tax amount, total cost
Step 3 − take input for price, discount, no, sales tax
Step 4 − calculation each term by applying the formulas given below:
cost = price*no;
discount amount = discount*cost/100;
discounted cost = cost – discount amount;
sales tax amount = sales tax*discounted cost/100;
total cost = discounted cost + sales tax amount;
Step 5 − print cost, discount amount, discounted cost, sales tax amount,
total cost
Step 6 − STOP

Program:

#include <stdio.h>

int main(void) {

// Variable declaration
float price,discount,no,salestax;
float cost,discountamount,discountedcost,salestaxamount,totalcost;
// Input for variables
printf("\nPrice of the item : ");
scanf("%f", &price);
printf("Discount (in percentage) : ");
scanf("%f", &discount);
printf("Quantity : ");
scanf("%f", &no);
printf("Sales tax (in percentage) : ");
scanf("%f", &salestax);

// Calculation
cost = price*no;
discountamount = discount*cost/100;
discountedcost = cost - discountamount;
salestaxamount = salestax*discountedcost/100;
totalcost = discountedcost + salestaxamount;

// Output
printf("\nCost of the order before the discount is applied : %f",cost);
printf("\nAmount of the discount : %f",discountamount);
printf("\nDiscounted cost : %f",discountedcost);
printf("\nAmount of sales tax : %f",salestaxamount);
printf("\nTotal amount : %f",totalcost);
return 0;
}

Output:

Problem 11:

Algorithm:

Step 1 − START
Step 2 − declare 4 variables walk, bat, hits, result
Step 3 − take inputs for walk, bat, hits
Step 4 − calculate result by the formula:
result = (bat - walk) / hits
Step 5 − print result
Step 6 − STOP

Program:
#include <stdio.h>

int main(void) {

// Variable declaration
float walk,bat,hits;
float result;

// Input for variables


printf("\nNumber of times the player walked : ");
scanf("%f", &walk);
printf("Number of times at bat : ");
scanf("%f", &bat);
printf("Number of hits the player made. : ");
scanf("%f", &hits);

// Calculation
result = (bat-walk)/hits;

// Output
printf("\nBatting average is %0.3f",result);

return 0;
}
Output:

Problem 12:

Algorithm:

Step 1 − START
Step 2 − declare 4 variables meal cost, tax, tip, total
Step 3 − assign value 44.50 to meal cost
Step 4 − calculate tax by the formula:
tax = 6.75 * mealcost / 100;
Step 5 − calculate tip by the formula:
tip = 15 * (mealcost + tax) / 100;
Step 6 − calculate total by the formula:
total = tax + tip + mealcost;
Step 7 − print meal cost, tax, tip, total
Step 8 − STOP
Program:

#include <stdio.h>

int main(void) {

// Variable declaration
float mealcost = 44.50;
float tax,tip,total;

// Calculation
tax = 6.75 * mealcost / 100;
tip = 15 * (mealcost + tax) / 100;
total = tax + tip + mealcost;

// Output
printf("\nMeal cost : %f",mealcost);
printf("\nTax amount : %f",tax);
printf("\nTip amount : %f",tip);
printf("\nTotal bill : %f",total);

return 0;
}
Output:

Problem 13:

Algorithm:

Step 1 − START
Step 2 − declare variables feet, inch, height, height1, pound, weight,
BMI
Step 3 − take input for feet, inch, pound
Step 4 − calculate each term by the formulas given below
height1 = feet*12 + inch;
height = height1*0.0254;
weight = pound/2.2;
BMI = weight / (height*height)
Step 5 − print BMI
Step 6 − STOP

Program:
#include <stdio.h>

int main(void) {

// Variable declaration
float feet,inch,height,height1,pound,weight,BMI;

// Input for variables


printf("\nHeight in feet : ");
scanf("%f", &feet);
printf("Height in inch : ");
scanf("%f", &inch);
printf("Weight in pounds : ");
scanf("%f", &pound);

// Conversion

// Height in inches -> Meter


height1 = feet*12 + inch;
height = height1*0.0254;
// Pound -> Kilogram
weight = pound/2.2;
// BMI
BMI = weight/(height*height);

// Output
printf("\nBMI : %f",BMI);

return 0;
}

Output:

Problem 14:

Algorithm:

Step 1 − START
Step 2 − declare 3 variables value, taxable amount, property tax
Step 3 − take input for value
Step 4 − calculate taxable amount by the formula:
taxable amount = 92*value/100;
Step 5 − calculate property tax by the formula:
property tax = taxable amount*1.05/100;
Step 6 − print value, taxable amount, property tax
Step 7 − STOP

Program:

#include <stdio.h>

int main(void) {

// Variable declaration
float value,taxableamount,propertytax;

// Input for variables


printf("\n\nEnter the assessed value of the property : ");
scanf("%f", &value);

// Calculation
taxableamount = 92*value/100;
propertytax = taxableamount*1.05/100;

// Output
printf("\nAssessed Value : %f",value);
printf("\nTaxable amount : %f",taxableamount);
printf("\nTax rate for each Rs.100 : 1.05");
printf("\nProperty tax : %f",propertytax);
return 0;
}

Output:

Problem 15:

Algorithm:

Step 1 − START
Step 2 − declare 5 variables x1, x2, y1, y2, m
Step 3 − take inputs for x1, x2, y1, y2
Step 4 − calculate m by the formula:
m = (y2 – y1)/(x2 – x1)
Step 5 − print m
Step 6 − STOP

Program:

#include <stdio.h>

int main(void) {

// Variable declaration
float x1,x2,y1,y2,m;

// Input for variables


printf("\nEnter the 1st coornidate : ");
printf("\n x coornidate : ");
scanf("%f", &x1);
printf(" y coornidate : ");
scanf("%f", &y1);
printf("\nEnter the 2nd coornidate : ");
printf("\n x coornidate : ");
scanf("%f", &x2);
printf(" y coornidate : ");
scanf("%f", &y2);

// Calculation
m = (y2-y1)/(x2-x1);

// Output
printf("\nSlope of the line is %f ",m);

return 0;
}

Output:

Problem 16:

Algorithm:

Step 1 − START
Step 2 − declare 6 variables r1 , r2, r3, resistance, current, voltage
Step 3 − take inputs for r1, r2, r3, current
Step 4 − calculate resistance by the formula:
resistance = 1/ (1/r1 + 1/r2 + 1/r3)
Step 5 − calculate voltage by the formula:
voltage = current * resistance
Step 6 − print voltage price
Step 7 − STOP

Program:

#include <stdio.h>

int main(void) {

// Variable declaration
float current,voltage,r1,r2,r3,resistance;

// Input for variables


printf("Input");
printf("\n\nCurrent : ");
scanf("%f", &current);
printf("Resitance 1 : ");
scanf("%f", &r1);
printf("Resitance 2 : ");
scanf("%f", &r2);
printf("Resitance 3 : ");
scanf("%f", &r3);

// Calculation
resistance = 1/(1/r1 + 1/r2 + 1/r3);
voltage = current * resistance;

// Output
printf("\nOutput");
printf("\n\nEffective resistance : %f",resistance);
printf("\nVoltage : %f",voltage);

return 0;
}

Output:

Problem 17:
Algorithm:

Step 1 − START
Step 2 − declare 5 variables principle, period, interest, SI, CI
Step 3 − take inputs for principle, period, interest
Step 4 − calculate SI by the formula:
SI = principle * interest * period / 100;
Step 5 − calculate SI by the formula:
CI = principle * pow(( (1+ interest) / 100),period);
Step 6 − print SI, CI
Step 7 − STOP

Program:

#include <stdio.h>
#include <math.h>

int main(void) {

// Variable declaration
float SI,CI,principle,period,interest;

// Input for variables


printf("Interest");
printf("\n\nPrinciple amount : ");
scanf("%f", &principle);
printf("Period (in years) : ");
scanf("%f", &period);
printf("Rate of interest : ");
scanf("%f", &interest);

// Calculation
SI = principle * interest * period / 100;
CI = principle * pow(( (1+ interest) / 100),period);

// Output
printf("\nOutput");
printf("\n\nSimple interest : %f",SI);
printf("\nCompund interest : %f",CI);

return 0;
}

Output:
Problem 18:

Algorithm:

Step 1 − START
Step 2 − declare 5 variables fifty, twenty, ten, five, one, amount
Step 3 − take input for amount
Step 4 − no.of fifty rupee note (fifty) required = amount/fifty
Step 5 - now replace the amount value with the remainder of above
divison
i.e amount = amount%50
Step 6 – repeat the same for twenty, ten, five, one
Step 7 − print fifty, twenty, ten, five, one
Step 8 − STOP

Program:
#include <stdio.h>

int main(void) {

// Variable declaration
int fifty,twenty,ten,five,one,amount;
fifty=twenty=ten=five=one=0;

// Input for variables


printf("Amount : ");
scanf("%d", &amount);

// Calculation
if(amount >= 50)
{
fifty = amount/50;
amount = amount%50;
}
if(amount >= 20)
{
twenty = amount/20;
amount = amount%20;
}
if(amount >= 10)
{
ten = amount/10;
amount = amount%*10;
}
if(amount >= 5)
{
five = amount/5;
amount = amount %5;
}
if(amount >= 1)
one = amount;

// Output
printf("\nNo.of 50rs note = %d",fifty);
printf("\nNo.of 20rs note = %d",twenty);
printf("\nNo.of 10rs note = %d",ten);
printf("\nNo.of 5rs note = %d",five);
printf("\nNo.of 1rs note = %d",one);

return 0;
}

Output:
Problem 19:

Algorithm:

Step 1 − START
Step 2 − declare 5 variables tank, town, highway, town distance,
highway distance
Step 3 − assign values 21.5,26.8,20 to town, highway and tank
respectively
Step 4 − calculate town distance by the formula:
town distance = tank * town
Step 5 − calculate highway distance by the formula:
highway distance = tank * highway
Step 6 − print town distsance, highway distance
Step 7 − STOP

Program:
#include <stdio.h>

int main(void) {

// Variable declaration
float town = 21.5;
float highway = 26.8;
int tank = 20;
int towndistance,highwaydistance;

// Calculation
towndistance = tank*town;
highwaydistance = tank*highway;

// Output
printf("\nDistance the car can travel on one tank of gas when driven in
town is %d miles",towndistance);
printf("\nDistance the car can travel on one tank of gas when driven in
highway is %d miles",highwaydistance);

return 0;
}

Output:
Problem 20:

Algorithm:

Step 1 − START
Step 2 − declare 4 variables inch, foot, cm, m
Step 3 − take input for inch
Step 4 − convert inch to respective term using the formula given below:
foot = inch/12;
yard = inch/36;
cm = inch*2.54;
m = inch/39.37;
Step 5 − print foot, yard, cm, m
Step 6 − STOP

Program:

#include <stdio.h>

int main(void) {
// Variable declaration
float inch,foot,yard,cm,m;
// Input for variables
printf("\nMeasurement in inches : ");
scanf("%f", &inch);

// Conversion
foot = inch/12;
yard = inch/36;
cm = inch*2.54;
m = inch/39.37;

// Output
printf("\nMeasurement in foot : %f",foot);
printf("\nMeasurement in yard : %f",yard);
printf("\nMeasurement in centimeter : %f",cm);
printf("\nMeasurement in meter : %f",m);
return 0;

Output:

You might also like