0% found this document useful (0 votes)
4 views20 pages

C++ Programming Assignments Solutions

This document contains solutions to several programming questions involving C++ code. It includes questions calculating the circumference of a circle, finding the average of numbers, calculating sums and cubes of numbers, finding the sale price of a bag, calculating the area of a triangle using Heron's formula, and finding the average rainfall over four months.

Uploaded by

786aunmuhammad
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)
4 views20 pages

C++ Programming Assignments Solutions

This document contains solutions to several programming questions involving C++ code. It includes questions calculating the circumference of a circle, finding the average of numbers, calculating sums and cubes of numbers, finding the sale price of a bag, calculating the area of a triangle using Heron's formula, and finding the average rainfall over four months.

Uploaded by

786aunmuhammad
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

Assignment NO 2

Name: Aun Muhammad


Registration No: 04062314003
Submited to: Mam Kashia Riaz
Semester: 2nd (Evening)
Department: Chemistry
Question no: 6
Calculate the circumference of a circle
Include<iostream>
using namespace std;
int main()
{
float π,r, circumference ;
π=3.14;
cout<<“Enter value of r “;
cin>>r ;
circumference=2*π*r;
cout<<“Circumference of circle is “<<circumference <<endl;
return 0;
}
Question no:1
Calculate average of the numbers
#include<iostream>
using namespace std;
int main()
{
int num1=20;
int num2=70;
int num3=90;
int sum=num1+num2+num3;
int average=sum/3; \\average=sum of all values/
total no. of values
cout<<“The average is “<<average<<endl;

return 0;
}
Question no:2. Part :1
Find sum of number
#include<iostream>
using namespace std;
int main()
{
int num1,num2,num3,sum;
cout<<“enter a number” ;
cin>>num1;
cout<<“enter a number” ;
cin>>num2;
cout<<“enter a number” ;
cin>>num3;
sum=num1+num2+num3;
cout<<“the sum is”<<sum<<endl;
return 0;
}
Question no:2. Part 2
Find cube of numbers
#include<iostream>
using namespace std;
int main()
{
int num1,num2,num3,cube1,cube2, cube3;
cout<<“enter num1 “ ;
cin>>num1;
cube1=num1*num1*num1;
cout<<“enter num2 “ ;
cin>>num2;
Cube2=num2*num2*num2;
cout<<“enter num3 “ ;
cin>>num3;
cube3=num3*num3*num3;
cout<<“the cube is “<<cube1<<endl;
cout<<“the cube is “<<cube2<<endl;
cout<<“the cube is “<<cube3<<endl;
return 0;
}
Question no:5
Find saleprice of a bag
#include<iostream>
using namespace std;
int main()
{
int originalprice,discount, saleprice;
originalprice = 3000;
double discountpercent = 0.15; //discountpercent=15% or 15/100 or 0.15
discount = originalprice*discountpercent;
cout<<“discount is “<<discount<<endl;
saleprice=originalprice-discount ;
cout<<“saleprice of bag is “<<saleprice <<endl;
return 0;
}
Question no:3
Find area of triangle using herons formula
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
double heronsformula,s,a,b,c;
a=20.864; // a,b,c are three sides of a triangle
b=19.7806;
c=25.14230;
s=(a+b+c)/2;
Cout<<“the value of s will be “<<s<<endl;
heronsformula = sqrt(s*(s-a)*(s-b)*(s-c));
cout<<“The area of triangle will be “<<heronsformula<<endl;

return 0;
}
Question No:4
Write program to find rainfall (using string
headerfile)
#include <iostream>
#include <string>
using namespace std;

int main()
{
string m1;
string m2;
string m3;
string m4;
int r_m1; //rainfall in 1st month
int r_m2; //rainfall in 2nd month
int r_m3; //rainfall in 3rd month
int r_m4; //rainfall in 4th month
Cout<<“Enter 1st month name “<<endl;
cin>>m1;
cout<<“Enter 2nd month name “<<endl;
cin>>m2;
cout<<“Enter 3rd month name “<<endl;
cin>>m3;
cout<<“Enter 4th month name “<<endl;
cin>>m4;

cout<<“Now Enter rainfall in 1st month(in inches) “<<endl;


cin>>r_m1;
cout<<“Now Enter rainfall in 2nd month(in inches) “<<endl;
cin>>r_m2;
cout<<“Now Enter rainfall in 3rd month(in inches) “<<endl;
cin>>r_m3;
cout<<“Now Enter rainfall in 4th month(in inches) “<<endl;
cin>>r_m4;
double averagerainfall=( r_m1+r_m2+r_m3+r_m4 ) /4; //formula to
find average
cout<<“The average rainfall in four months is
“<<averagerainfall<<endl;
return 0;
}

You might also like