ASSIGNMENT # 02
Programming fundamental
NAME : Muhammad Hussain Cheema
Roll no : 24014156_117
Section : IT 24 B
Submitted to :
Mr . Zulfiqar ALI.
PROGRAM: 1(Display size of data types )
#include<iostream>
using namespace std;
int main()
cout<<" Size of Fundamental Data types : "<<endl;
cout << " Sizeof Character is : " << sizeof(char) << " Bytes" << endl;
cout << " Sizeof short int is : " << sizeof(short) << " Bytes" << endl;
cout << " Sizeof integer : " << sizeof(int) << " Bytes" << endl;
cout << " Sizeof long : " << sizeof(long) << " Bytes" << endl;
cout << " Sizeof long long is : " << sizeof(long long) << " Bytes" << endl;
cout << " Sizeof float : " << sizeof(float) << " Bytes" << endl;
cout << " Sizeof Double : " << sizeof(double) << " Bytes" << endl;
cout << " Size of long double : " << sizeof(long double) << " Bytes" << endl;
cout << " Sizeof Bool : " << sizeof(bool) << " Bytes" ; return 0;}
PROGRAM: 02 ( Swamping two numbers)
#include<iostream>
using namespace std;
int main(){
int a,b;
cout<<"Enter X = ";
cin>>a;
cout<<"Enter Y = ";
cin>>b;
cout<<" Before Swamping \t X = "<<a<<" \t Y = "<<b<<endl;
a=a-b;
b=a+b;
a=b-a;
cout<<"After Swamping \t X = "<<a<< "\t Y = "<<b<<endl;
return 0;
Program : 03 ( calcutate volume of sphere )
#include<iostream>
#include<cmath>
using namespace std;
int main(){
const float pi=3.142;
int r;
cout<<"Enter radius of sphere : ";
cin>>r;
float vol=(4.0/3)*pi*pow(r,3); // 4.0 to avoid integer division
cout<<"\n Volume of sphee is : "<<vol;
return 0;
}
Program : 04 (calcutale area and perimeter of
rectangle)
:
#include<iostream>
using namespace std;
int main(){
int l,w;
cout<<"Enter width of rectangle : "; cin>>w;
cout<<"Enter Length of rectangle : "; cin>>l;
int area , peri;
area=l*w;
peri=2*(l+w);
cout<<"Area = "<<area<<endl;
cout<<"perimeter = "<<peri<<endl;
return 0;}
Program 05: (Calculate area and circumferane of
circle)
#include<iostream>
#include<cmath>
using namespace std;
int main(){
float const pi=3.142;
int r=0;
cout<<"Enter radius of circle : ";
cin>>r;
float area , circum;
area=pi*pow(r,2);
circum=2*pi*r;
cout<<"Area = "<<area<<endl;
cout<<"Circumferance = "<<circum;
return 0;}
program 06 : (convet tem from Celsius to farenheight)
#include<iostream>
using namespace std;
int main(){
float c;
cout<<"Enter Tempetraure in Celcius : "; cin>>c;
float f;
f=(9.0/5*c)+32; // evalute from left to right
cout<<"Tempetature in celcius : "<<c<<endl;
cout<<"Temperature in Farenheight "<<f;
return 0;}
Output :
Program 07(convert temperature from farnheight to Celsius)
#include<iostream>
using namespace std;
int main(){
float f;
cout<<"Enter Tempetraure in farenheight : "; cin>>f;
float c;
c=(f-32)*(5.0/9);// evalute from left to right
cout<<"Tempetature in farenheight : : "<<f<<endl;
cout<<"Temperature in celcius "<<c;
return 0;}
Program 08:(convet speed from km/h to mile/h)
#include<iostream>
using namespace std;
int main(){
float k;
cout<<"Enter Speed in Km/h :"; cin>>k;
float m;
m=k*0.621371;
cout<<k<< " Km/h means "<<m<<" Miles/h "; return 0;
}
Program 09( write program find quotient and remainder )
#include <iostream>
using namespace std;
int main() {
float dividend, divisor, quotient, remainder;
cout << "Enter the dividend: ";
cin >> dividend;
cout << "Enter the divisor: ";
cin >> divisor;
quotient = dividend / divisor;
remainder = int (dividend) % int(divisor);
cout << "Quotient: " << quotient << endl;
cout << "Remainder: " << remainder << endl;
return 0;
}
Program 10: ( display time)
#include <iostream>
#include <ctime>
using namespace std;
int main() {
// Get current time
time_t now = time(0);
tm *ltm = localtime(&now);
// Display the current date and time
cout << "Display the Current Date and Time : " << endl;
cout << "----------------------------------------" << endl;
cout << "seconds = " << ltm->tm_sec << endl;
cout << "minutes = " << ltm->tm_min << endl;
cout << "hours = " << ltm->tm_hour << endl;
cout << "day of month = " << ltm->tm_mday << endl;
cout << "month of year = " << 1 + ltm->tm_mon << endl; // tm_mon
is 0-11
cout << "year = " << 1900 + ltm->tm_year << endl; // tm_year is
years since 1900
cout << "weekday = " << ltm->tm_wday << endl; // 0 is Sunday
cout << "day of year = " << ltm->tm_yday << endl;
cout << "daylight savings = " << ltm->tm_isdst << endl;
// Display current date
cout << "Current Date: " << ltm->tm_mday << "/" << 1 + ltm->tm_mon
<< "/" << 1900 + ltm->tm_year << endl;
// Display current time
cout << "Current Time: " << ltm->tm_hour << ":" << ltm->tm_min <<
":" << ltm->tm_sec << endl;
return 0;
}
Program 11 (area of polygone )
#include <iostream>
#include <cmath>
using namespace std;
int main() {
const float pi =3.142;
int n; // Number of sides
double s; // Length of each side
cout << "Print the area of a polygon:" << endl;
cout << "Input the number of sides of the polygon: ";
cin >> n;
cout << "Input the length of each side of the polygon: ";
cin >> s;
double area = (n * s*s) / (4 * tan(pi / n));
cout << "The area of the polygon is: " << area << endl;return 0;
}
Program 12(calculate time to reach destination)
#include<iostream>
using namespace std;
int main() {
float distance, speed, time;
cout << "Enter the distance traveled (in km): ";cin >> distance;
cout << "Enter the speed of vehicle (km/h): ";cin >> speed;
time = distance / speed;
cout<<"Time to reach destination: "<< time <<" hour" << endl;
return 0;}
Program 13 : (revese digits)
#include<iostream>
using namespace std;
int main(){
int n;
cout<<"Enter a three digit number : ";
cin>>n;
cout<<"Original order is : "<<n<<endl;
int a,b;
a=n%10;
n=n/10;
b=n%10;
n=n/10;
cout<<"Reverse rorder is : "<<a<<b<<n;
return 0;}
Program 14: (convert hours in weeks , days and remaining hours)
#include<iostream>
using namespace std;
int main()
{
int n;
cout<<"Enter Hours : ";cin>>n;
int w,d;
w=n/168;
n=n%168;
d=n/24;
n=n%24;
cout<<"WEEk(s) : "<<w<<endl;
cout<<"DAYS : "<<d<<endl;
cout<<"Hours : "<<n;
return 0;}
Program 15:
#include<iostream>
using namespace std;
int main()
{
int n;
cout<<"Enter A number : "; cin>>n;
if(n>0)
cout<<"number is positive "<<endl;
else if (n<0)
cout<<"number is Negative : "<<endl;
else
cout<<"Number is Zero "<<endl;
return 0;
Program 16:
#include<iostream>
using namespace std;
int main()
{
int n,k;
cout<<"Enter first number : "; cin>>n;
cout<<"Enter second number : "; cin>>k;
if(n*n==k)
cout<<"Second number is square of first "<<endl;
else
cout<<"second number is not square of first number "<<endl;
return 0;}
Program 17:
#include<iostream>
using namespace std;
int main()
{
float a,b,c,d;
cout<<"Enter total marks in Matric : "; cin>>a;
cout<<"\nenter Obtained marks in Matric : "; cin>>b;
cout<<"\nEnter total marks in Inter : "; cin>>c;
cout<<"\nEnter obtained marks in Inter : "; cin>>d;
float w_matric=(30.0/100); // stores weightage
float w_inter=(70.0/100); // store weightage
if (b<=a && d<=c){ // ensure that obtained marks are less
than or equal to obtained marks
float x,y;
x=((b/a)*100)*w_matric; // computes percentage according to
weightage.
y=((d/c)*100)*w_inter;
cout<<"\n Merit score : "<<x+y; // adds weightages
}
else
cout<<"obtained marks cannot be greater than total marks : ";
return 0;
Program 18:(Find maximum among three numbers )
#include<iostream>
using namespace std;
int main()
{
int a,b,c;
cout<<"Enter three numbers : "; cin>>a>>b>>c;
if(a>b)
if(a>c)
cout<<"Maximum : "<<a<<endl;
else
cout<<"Maximum : "<<c<<endl;
else
if(b>c)
cout<<"Maximum :"<<b<<endl;
else
cout<<"Maximum :"<<c<<endl;
return 8;
}
Program 19 : (Even odd)
#include<iostream>
using namespace std;
int main()
{
int a;
cout<<"Enter a number : "; cin>>a;
(a%2==0)?cout<<" Even ": cout<<" Odd ";
return 9;}
Program 20 :(leap year)
#include<iostream>
using namespace std;
int main()
{
int y;
cout<<"Enter year : ";cin>>y;
(y%4==0)?cout<<"Leap year ":cout<<"Not leap year ";
return 3;}
Program 21 (Salary )
#include<iostream>
using namespace std;
int main()
{
float s;
int g;
cout<<"Enter Basic salary : "; cin>>s;
cout<<endl<<"Enter grade : "; cin>>g;
if(g<=15)
s=s+(s*0.25);
else
s=s+(s*0.50);
cout<<"Total salary : "<<s;
return 8;}
Program 22 : (calculate total marks According to weightage )
#include<iostream>
using namespace std;
int main()
{
float a1,a2,a3,a4,q1,q2,q3,q4;
float w_asign=10/100.0; //weightage of assignments
float w_quiz=5.0/100; //weightage of Quizes
float ta,tq;
float x,y;
cout<<"Enter Marks od assingment 1 : "; cin>>a1;
cout<<endl<<"Enter Marks od assingment 2 : "; cin>>a2;
cout<<endl<<"Enter Marks od assingment 3 : "; cin>>a3;
cout<<endl<<"Enter Marks od assingment 4 : "; cin>>a4;
ta=a1+a2+a3+a4;
x=( (ta/40.0) *100) * w_asign ; // since weightage of assignments is
10 %
cout<<endl<<"Enter Marks of Quiz 1 : "; cin>>q1;
cout<<endl<<"Enter Marks of Quia 2 : "; cin>>q2;
cout<<endl<<"Enter Marks of Qiuz 3: "; cin>>q3;
cout<<endl<<"Enter Marks of Quiz 4: "; cin>>q4;
tq=q1+q2+q3+q4;
y=( (tq/40.0) *100) * w_quiz; // since weightage of Quiz is 5%
float p,m,f;
cout<<"Enter Obtained marks of project : "; cin>>p;
cout<<endl<<"Enter marks of mid term : "; cin>>m;
cout<<endl<<"Enter marks of final : "; cin >>f;
float total = x+y+p+m+f;
cout<<"\nTotal marks : "<<total;
return 0;}