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

Assignment # 1 C++

This document contains 16 programming problems and their solutions in C++. The problems cover a range of basic programming concepts like input/output, arithmetic operations, conditional statements, loops, functions, data types, and conversions. Each problem is numbered and includes the problem statement, sample code to solve it, and a brief description of the code. The problems get progressively more complex and cover finding sums, products, remainders, reversing digits, calculating areas, temperature conversions, swapping values, unit conversions, and working with characters and ASCII codes.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
220 views

Assignment # 1 C++

This document contains 16 programming problems and their solutions in C++. The problems cover a range of basic programming concepts like input/output, arithmetic operations, conditional statements, loops, functions, data types, and conversions. Each problem is numbered and includes the problem statement, sample code to solve it, and a brief description of the code. The problems get progressively more complex and cover finding sums, products, remainders, reversing digits, calculating areas, temperature conversions, swapping values, unit conversions, and working with characters and ASCII codes.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

Assignment # 1 Programming With C++

Muhammad Irshad Khan


Contact #:03041427789

(1) Write a program that takes two values as input and print Sum, Product,
Difference, Quotient and Remainder.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a,b,sum=0,product,q,r;
cout<<"Enter Two Number:";
cin>>a>>b;
sum=a+b;
product=a*b;
q=a/b;
r=a%b;
cout<<"Sum of "<<a<<" and "<<b<<" is "<<sum<<endl;
cout<<"Product of "<<a<<" and "<<b<<" is "<<product<<endl;
cout<<"Quotient of "<<a<<" and "<<b<<" is "<<q<<endl;
cout<<"Remainder of "<<a<<" and "<<b<<" is "<<r<<endl;
getch();
}

1
Assignment # 1 Programming With C++
Muhammad Irshad Khan
Contact #:03041427789

(2) Write a program that input 4-digit integer number and display the
number in reverse order. Compute and print sum, product and average of
its digits.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int n,a,b,c,sum=0,product;
float avg;
cout<<"Enter 4-Digit Number:";
cin>>n;
a=n/1000;
n=n%1000;
b=n/100;
n=n%100;
c=n/10;
n=n%10;
sum=a+b+c+n;
product=a*b*c*n;
avg=sum/4;
cout<<"Number in Reverse Order:"<<n<<c<<b<<a<<endl;
cout<<"Sum of its digit is="<<sum<<endl;
cout<<"Product of its digits is ="<<product<<endl;
cout<<"Average of its digits is ="<<avg;
getch();
}

2
Assignment # 1 Programming With C++
Muhammad Irshad Khan
Contact #:03041427789

(3) Write a program that inputs a 3-digit integer and number and display its
first and last digit and their sum.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int n,a,b,sum=0;
cout<<"Enter 3-Digit Number:";
cin>>n;
a=n/100;
n=n%10;
sum=a+b+n;
cout<<"First digit is="<<a<<endl;
cout<<"Last digit is="<<n<<endl;
cout<<"Sum of its digit is="<<sum<<endl;
getch();
}

3
Assignment # 1 Programming With C++
Muhammad Irshad Khan
Contact #:03041427789

(4) Write a program that read a 3-digit integer and display ASCII character of its middle digit.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int n,a,b,sum=0;
cout<<"Enter 3-Digit Number:";
cin>>n;
a=n/100;
n=n%100;
b=n/10;
cout<<"ASCII Character of "<<b<<" = "<<char(b)<<endl; // Explicit Type Casting
getch();
}
(5) Write a program that inputs an integer and display its square, cube and square root.

#include<iostream.h>
#include<conio.h>
#include<math.h>
void main()
{
clrscr();
int n,sum=0;
cout<<"Enter an integer Number:";
cin>>n;
cout<<"Square of "<<n<<" is "<<n*n<<endl;
cout<<"Cube of "<<n<<" is "<<n*n*n<<endl;
cout<<"Square Root of "<<n<<" is "<<sqrt(n);
getch();
}

4
Assignment # 1 Programming With C++
Muhammad Irshad Khan
Contact #:03041427789

(6) Write a program that input two numbers x and y and compute XY.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int x,y,p=1,i;
cout<<"Enter Value of X:";
cin>>x;
cout<<"Enter Value of Y:";
cin>>y;
i=1;
while(i<=y)
{
p=p*x;
i++;
}
cout<<"Requried Result is="<<p;
getch();
}

5
Assignment # 1 Programming With C++
Muhammad Irshad Khan
Contact #:03041427789

(7) Write a program to input three sides a, b and c of triangle and compute its area.
Formula: s = (a+b+c)/2;
Area=sqrt(s(s-a)(s-b)(s-c))
#include<iostream.h>
#include<conio.h>
#include<math.h>
void main()
{
clrscr();
float a,b,c,s;
double area;
cout<<"Enter Side a:";
cin>>a;
cout<<"Enter side b:";
cin>>b;
cout<<"Enter side c:";
cin>>c;
s=(a+b+c)/2;
area=sqrt(s*(s-a)*(s-b)*(s-c));
cout<<"Area of a triangle is="<<area;
getch();
}

6
Assignment # 1 Programming With C++
Muhammad Irshad Khan
Contact #:03041427789

(8) Write a program that calculates area of a triangle when base and height entered from
keyboard.
Formula: area=1/2*base*height
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
float b,h,a;
cout<<"Enter base:";
cin>>b;
cout<<"Enter Height:";
cin>>h;
a=0.5*b*h;
cout<<"Area="<<a;
getch();
}
(9) Write a program that convert temperature from Centigrade to Fahrenheit.
Formula: F=9/5*C+32;
#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
void main()
{
clrscr();
float f,c;
cout<<"Enter Temperature in Centigrade:";
cin>>c;
f=9.0/5.0*c+32;
cout<<"Temperature in Fahrenheit is=";
cout<<setprecision(2)<<f;
getch();
}

7
Assignment # 1 Programming With C++
Muhammad Irshad Khan
Contact #:03041427789

(10) Write a program that convert temperature from Fahrenheit to Centigrade.


Formula: C=5/9*(f-32)
#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
void main()
{
clrscr();
float f,c;
cout<<"Enter Temperature in Fahrenheit:";
cin>>f;
c=5.0/9.0*(f-32);
cout<<"Temperature in Centigrade is=";
cout<<setprecision(2)<<c;
getch();
}
(11) Write a program that swap two values of two variables without using third variable.
#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
void main()
{
clrscr();
int a,b;
cout<<"Enter Value of a:";
cin>>a;
cout<<"Enter Value of b:";
cin>>b;
cout<<"Values Before Swap are:"<<endl;
cout<<"Value of a="<<a<<endl;
cout<<"Value of b="<<b<<endl;
a=a+b;
b=a-b;
a=a-b;
cout<<"Values After Swap="<<endl;
cout<<"Value of a="<<a<<endl;
cout<<"Value of b="<<b;
getch();
}

8
Assignment # 1 Programming With C++
Muhammad Irshad Khan
Contact #:03041427789

(12) Write a program that read the distance between two cities in kilometers and change it
into meters, feet, inches, centimeters and millimeters.
#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
void main()
{
clrscr();
long int dkm,meter,cm,mm;
double inch,feet;
cout<<"Enter Distence in Kilometers B/W two city:";
cin>>dkm;
meter=dkm*1000;
cm=dkm*100000;
mm=dkm*1000000;
inch=dkm*39370.1;
feet =dkm*3280.84;
cout<<"Distance in meter:"<<meter<<endl;
cout<<"Distance in centimeter:"<<cm<<endl;
cout<<"Distance in milimeter:"<<mm<<endl;
cout<<"Distance in Inches:"<<setprecision(3)<<inch<<endl;
cout<<"Distence in Feet:"<< setprecision(3)<<feet<<endl;
getch();
}
(13) Write a program that read age in years and convert it into months, weeks, Days and
hours;
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int age_in_year,month,week,day;
long int hour=0;
cout<<"Enter age in year:";
cin>>age_in_year;
month=age_in_year*12;
week=age_in_year*52;

9
Assignment # 1 Programming With C++
Muhammad Irshad Khan
Contact #:03041427789

day=age_in_year*365;
hour=day*24;
cout<<"Age in month:"<<month<<endl;
cout<<"Age in week:"<<week<<endl;
cout<<"Age in day:"<<day<<endl;
cout<<"Age in hour:"<<hour;
getch();
}
(14) Write a program that inputs a character from keyboard and display its ASCII code.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
char ch;
cout<<"Enter any Character:";
cin>>ch;
cout<<"ASCII code crossponding "<<ch<<" is "<<int(ch);
getch();
}
(15) Write a program that inputs a ASCII code from keyboard and display its ASCII
character.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int n;
cout<<"Enter ASCII code:";
cin>>n;
cout<<"ASCII Character crossponding "<<n<<" is "<<char(n);
getch();
}

10
Assignment # 1 Programming With C++
Muhammad Irshad Khan
Contact #:03041427789

(16) Write a program that input an integer number and find the right most digit.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int n,r;
cout<<"Enter an integer:";
cin>>n;
r=n%10;
cout<<"Right Most Digit in "<<n<<" is "<<r;
getch();
}
(17) Write a program that convert lowercase to uppercase and vice versa

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
char ch;
cout<<"Enter any Character:";
cin>>ch;
if(ch>='a'&&ch<='z')
{
cout<<"You Entered Lowercase Letter:"<<endl;
ch=ch-32;
cout<<"UpperCase Letter is:"<<ch<<endl;
}
else
{
cout<<"You Entered Uppercase Letter:"<<enld;
ch=ch+32;
cout<<"Lowercase Letter is:"<<ch;
}
getch();
}

11
Assignment # 1 Programming With C++
Muhammad Irshad Khan
Contact #:03041427789

(18) Write a program that inputs amount in Rupees and convert it into Dollars.

(1 Dollar=105 Rupees)

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
float rupees,dollar;
cout<<"Enter Amount in Rupees:";
cin>>rupees;
dollar=rupees/105;
cout<<"Amount in Dollars is="<<dollar;
getch();
}
(19) Write a program that inputs marks of a student in four subject and compute total
marks and average.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int cpp,dld,os,dbms,total;
float avg;
cout<<"Enter marks in CPP:";
cin>>cpp;
cout<<"Enter marks in Digital Logic and Design:";
cin>>dld;
cout<<"Enter marks in operating system:";
cin>>os;
cout<<"Enter marks in DBMS:";
cin>>dbms;
total=cpp+dld+os+dbms;
avg=total/4.0;
cout<<"Total Mark="<<total<<endl;
cout<<"Average="<<avg;
getch();
}

12
Assignment # 1 Programming With C++
Muhammad Irshad Khan
Contact #:03041427789

(20) Write a program that input a floating number and separate and display its integer and
fractional part.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
float f,r;
cout<<"Enter a floating point number:";
cin>>f;
int n=f;
r=f-n;
cout<<"Integer Part is:"<<n<<endl;
cout<<"Fractional part is:"<<r;
getch();
}
(21) Write a program that takes 5-digit integer number from user. Then make its three
middle digits 0. E.g. if input number is 42935, then output should be 40005. Store and Print
the result as a single variable.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
long int n,a,b,c,first,last;
a=b=c=0;
cout<<"Enter 5-digit Number:";
cin>>n;
first=n/10000;
n=n%10;
cout<<"Requried output is:"<<first<<a<<b<<c<<n;
getch();
}

13
Assignment # 1 Programming With C++
Muhammad Irshad Khan
Contact #:03041427789

(22) Write a program that takes 5-digit integer and print its each digit on the separate line.
E.g. if we input number34567 then
3 4 5 6 7
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
long int n,a,b,c,d;
cout<<"Enter 5-digit Number:";
cin>>n;
a=n/10000;
n=n%10000;
b=n/1000;
n=n%1000;
c=n/100;
n=n%100;
d=n/10;
n=n%10;
cout<<"Requried output is:"<<endl;
cout<<a<<endl<<b<<endl<<c<<endl<<d<<endl<<n;
getch();
}
(23) Write a program that inputs radius and compute and print area and circumference.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
float rad,area,cir;
cout<<"Enter radius:";
cin>>rad;
area=3.141*rad*rad;
cir=2.0*3.141*rad;
cout<<"Area of a circle is :"<<area<<endl;
cout<<"Circumference of Circle is:"<<cir<<endl;
getch();
}

14
Assignment # 1 Programming With C++
Muhammad Irshad Khan
Contact #:03041427789

(24) Write a program that inputs Coordinates of two points p(X1,Y1) and Q(X2,Y2) and
print distance between two points.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
float x1,x2,y1,y2,d;
cout<<"Enter Coordinates X1 and Y1 of Point P:";
cin>>x1>>y1;
cout<<"Enter Coordinates X2 and Y2 of Point Q:";
cin>>x2>>y2;
d=sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));
cout<<"Requried Distance is:"<<d;
getch();
}
(25) Write a program that input basic salary of an employee from keyboard and compute
25% house rent, 15% Medical Allowance, 35% Dearness Allowance and print gross salary.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int bsal;
float gross;
cout<<"Enter Basic Salary:";
cin>>bsal;
gross=bsal+(bsal*0.25)+(bsal*0.35);
cout<<"Your Groos Salary is:"<<gross;
getch();
}

15
Assignment # 1 Programming With C++
Muhammad Irshad Khan
Contact #:03041427789

(26) Write a program that input time in hours and calculate number of week,days and
hours within the entered number of hours.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int hour,week,day;
cout<<"Enter Time in Hours:";
cin>>hour;
week=hour/168;
hour=hour%168;
day=hour/24;
hour=hour%24;
cout<<"Number of Weeks="<<week<<endl;
cout<<"Number of Days="<<day<<endl;
cout<<"Number of Hours="<<hour;
getch();
}
(27) Write a program that input time in second and converts into hours,minutes and
seconds.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int sec,hour,min;
cout<<"Enter Time In Second:";
cin>>sec;
hour=sec/3600;
sec=sec%3600;
min=sec/60;
sec=sec%60;
cout<<"Number of Hours="<<hour<<endl;
cout<<"Number of Minutes="<<min<<endl;
cout<<"Number of Second="<<sec;
getch();
}

16
Assignment # 1 Programming With C++
Muhammad Irshad Khan
Contact #:03041427789

(28) Write a program that inputs two floating point numbers, divide them and print the
result rounded to two digits after decimal point.
#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
void main()
{
clrscr();
float a,b,div;
cout<<"Enter First Floating Point Number:";
cin>>a;
cout<<"Enter Second Floating Point Number:";
cin>>b;
div=a/b;
cout<<"Result="<<setprecision(2)<<div;
getch();
}
(29) Write a program that input multi-word strings name, address and city from the user
and print them on three separate lines.

#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
#include<conio.h>
#include<stdio.h>
void main()
{
clrscr();
char name[30],add[50],city[30];
cout<<"Enter Your name:";
gets(name);
cout<<"Enter Your Address:";
gets(add);
cout<<"Enter Your City Name:";
gets(city);
puts(name);

17
Assignment # 1 Programming With C++
Muhammad Irshad Khan
Contact #:03041427789

puts(add);
puts(city);
getch();
}
(30) Write a program that inputs two letters from the user and print next two
letters.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
char ch;
cout<<"Enter any character:";
cin>>ch;
cout<<"Two Letters are:"<<endl;
ch++;
cout<<ch<<endl;
ch++;
cout<<ch;
getch();
}

18

You might also like