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

Programs assignment 1

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Programs assignment 1

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 19

Programs

4.3
Write a program to calculate and print the area of square with given
height and width
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int height, width, area;
height = 5;
width = 4;
area= height width;
cout<<”Area of Square = “<<area;
getch();
}
4.9
Write a program that inputs name, age and address from the user
and then displays these values on the screen.
#include<iostream.h>
#include<conio.h>
void main()
{
char name[25],city[30];
int age;
clrscr();
cout<<”Enter your age:”;
cin>>age;
cout<<”Enter your first name:”;
cin>>name;
cout<<”Enter your city:”;
cin>>city;
cout<<”\n Your first name is “<<name<<endl;
cout<<”Your city is “<<city<<endl;
cout<<”Your age is “<<age<<endl;
getch();
}
4.10
Write a program to calculate the simple interest. It inputs principal
amount, rate of interest and the number of years and displays the
simple interest.
INPUT
#include<iostream.h>
#include<conio.h>
void main ()
{
double p, r, t, I;
clrscr();
cout<< “Enter principal amount, rate, time:”;
cin>> p >> r>> t;
I=(p*r*t)/100;
cout<<”\n Principal Amt = Rs. “ << p;
cout<<”\n Rate=”<<<<”%”;
cout << “\n Time = “ << t << “yrs.”;
cout<<”\n Simple Interest Amt = Rs.” << I;
getch();
}
4.11
Write a program that inputs a character and displays its ASCII code.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
char charac;
cout << “Enter the character:”;
cin>>charac;
int num1= charac;
cout << “The ASCII code for “ << charac<<” is “ << num1 << “,” << endl;
getch();
}
4.12
Write a program that inputs dividend and divisor. It then calculates
and displays the quotient and remainder .
#include<iostream.h>
#include<conio.h>
void main()
{
int div, dis, q,r;
clrscr();
cout<< “Enter dividend & divisor”;
cin >> div >> dis;
q=div/dis;
r=div% dis;
cout<<” Quotient=” << q<<endl;
cout << “Remainder =”<<r;
getch();
}
4.13
Write a program that inputs two numbers, swaps the values and
then displays them.
#include<iostream.h>
#include<conio.h>
void main()
{
int a, b, temp;
clrscr;
cout<<”Enter the first number:”;
cin>>a;
cout<<”Enter the second number:”;
cin>>b;
cout<<”You input the numbers as “<<a<<” and “<<b<<endl;
temp = a;
a=b;
b = temp;
cout<<”The values after swapping are “<< a <<” and “<<b<<”endl;
getch();
}
4.14
Write a program that inputs two numbers, swaps these values
without using third variable and displays them.
#include<iostream.h>
#include<conio.h>
void main()
{
int x, y;
clscr();
cout<<”\n Enter 2 integers respectively:”;
cin>>x>>y;
cout<<”\n The original value in x =” <<x<<” and y =” <<y;
x=x+y;
y=x–y;
x = x – y;
cout<<”\n The swapped value in x =”<<x<<” and y =”<<y;
getch();
}
4.15
Write a program that inputs the distance traveled and the speed of
vehicle. It calculates the time required to reach the destination and
displays it.
#include<iostream.h>
#include<conio.h>
void main ()
{
clrscr();
double distance, time, speed;
cout<<”Enter the distance traveled in miles:”;
cin>>distance;
cout<<”Enter the speed of vehicle (mph):”;
cin>>speed;
time =distance/speed;
cout<<”Time required to reach destination: “<<time<<” hours.”<<endl;
getch();
}
4.17
Write a program that inputs time in seconds and converts it into hh-
mm-ss format.
#include<iostream.h>
#include<conio.h>
void main()
{
int sec, s, m, h;
clrscr();
cout<<”\n Enter time in seconds :”;
cin >> sec;
h = sec/3600;
sec =sec % 3600;
m = sec/60;
s = sec % 60;
cout << “\n HH-MM-SS=” <<h<<”:”<< m <<”:”<< s;
getch();
}
4.21
Write a program that calculates the final velocity of an object by
taking following inputs from the user: vi = Initial velocity, a =
acceleration, t = time span. Formula vf = vi +at .
INPUT
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int vi, vf, a, t;
cout << “Enter the initial velocity: “;
cin>>vi;
cout << “Enter the acceleration: “;
cin>>a;
cout << “Enter the time: “;
cin>>t;
vf =vi+a* t;
cout<<”The final velocity is “<< vf << endl;
getch();
}
4.22
Write a program that inputs a three-digit number from the user and
displays it in reverse order. For example if the user enter 123, it
displays 321
#include<iostream.h>
#include<conio.h>
void main()
{
int n, a, b;
clrscr();
cout<<”Enter 3-digit number:”;
cin>>n;
a= n / 100;
n= n% 100;
b=n/10;
n= n % 10 ;
cout<<”Number in reverse order is “<<n<<b<<a;
getch();
}
4.26
Write a program that will prompt the user to enter number of hours.
It computes and displays the number of weeks, days and hours
within the input number of hours.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int hrs , w , d ;
cout << “Enter number of hours: “;
cin>>hrs;
w= hrs/168;
hrs= hrs % 168;
d= hrs / 24 ;
hrs = hrs % 24;
cout<<”Weeks: “<<w<<endl;
cout<<”Days: “<<d<<endl;
cout<<”Hours: “<<hrs;
getch();
}
Program 5.3
Write a program that inputs two numbers and finds if second
number is square of first.
#include<iostream.h>
#include<conio.h>
void main()
int a, b;
clrscr();
cout<<"Enter a number:";
cin>>a;
cout<<"Enter a number: ";
cin>>b;
if(a*a==b)
cout<<"2nd number is square of 1st number.";
getch();
Program 5.4
Write a program that inputs marks of three subjects. If the average
of marks is more than 80, it displays two messages "You are above
standard!" and "Admission granted!"
#include<iostream.h>
#include<conio.h>
void main()
int sub1, sub2, sub3;
float avg;
cout<<"Enter marks of first subject: ";
cin>>sub1;
cout<<"Enter marks of second subject: ": cin>>sub2;
cout<<"Enter marks of third subject: ";
cin>>sub3;
avg (sub1+sub2+sub3)/3.0,
if(avg>80) {
cout<<"You are above standard! \n";
cout<<"Admission granted!";
getch();
}
Program 5.5
Write a program that inputs three numbers and displays the
maximum number.
#include<iostream.h>
#include<conio.h>
void main()
int a, b, c, max;
cout<<"Enter first number: ";
cin>>a;
cout<<"Enter second number: ";
cin>>b;
cout<<"Enter third number:";
cin>>c;
max = a;
if(b> max)
max = b;
if(c>max)
max = c;
cout<<"The maximum number is "<<max;
getch();
}
Program 5.9
Write a program that inputs a year and finds whether it is a leap
year or not using if-else structure
#include<iostream.h>
#include<conio.h>
void main()
{
int y;
clrscr();
cout<<"Enter a year: ";
cin>>y;
if(y % 4 == 0)
cout<<y<<" is a leap year.";
else
cout<<y<<" is not a leap year.";
getch();
}
Program 5.15
Write a program that inputs salary. If the salary is 20000 or more, it
deducts 7% of salary. If the salary is 10000 or more but less than
20000, it deducts 1000 from the salary. If salary is less than 10000,
it deducts nothing. It finally displays the net salary.
#include<iostream.h>
#include<conio.h>
void main()
int salary;
float net;
clrscr();
cout<<"Enter salary: ";
cin>>salary,;
if(salary > 20000)
net salary (salary 7.0/100);
else if(salary>= 10000)
net salary-1000;
else
net = salary;
cout<<"Your net salary is "<<net;
getch();
}
Program 5.17
Write a program that inputs three numbers and displays whether all
numbers are equal or not by using nested if condition.
#include<iostream.h>
#include<conio.h>
void main()
int a,b,c;
cout<<"Enter three number:";
cin>>a>>b>>c;
if(a==b)
if(a==c)
cout<<"All numbers are equal.";
else
cout<<"Numbers are different.";
else
cout<<"Numbers are different.";
getch();
}
Program 5.23
Write a program that inputs number of week's day and displays the
name of the day. For example if user enters 1, it displays "Friday"
and so on.
#include<iostream.h>
#include<conio.h>
void main()
{
int n;
clrscr();
cout<<"Enter number of a weekday: ";
cin>>n;
switch(n)
{
case 1:
cout<<"Friday";
break;
case 2:
cout<<"Saturday";
break;
cout<<"Sunday";
case 3:
break;
case 4:
cout<<"Monday";
break;
case 5:
cout<<"Tuesday";
break;
case 6:
cout<<"Wednesday";
break;
case 7:
cout<<"Thursday":
break;
default:
cout<<"Invalid number";
}
getch();
}
Program 5.25
Write a program that inputs a floating point number, an operator
and another floating point number. It displays the result by
performing the operation on the given numbers. If the operator is a
division, it should check to make sure that the divisor is not equal to
zero. If the operator is not a +, or/then the program should print an
error message.
#include<iostream.h>
#include<conio.h>
void main()
{
float a, b;
char op;
clrscr();
cout<<" Enter floating point number:";
cin>>a;
cout<<"Enter an operator: ";
cin>>op;
cout<<"Enter a floating point number:";
cin>>b;
switch(op)
{
case '+':
cout<<a+b<<endl;
break;
cout<<a-b<<endl;
break;
case’-‘:
cout<<a-b<<endl;
break;
case’*’:
cout<<”a*b<<endl;
break;
case ‘/’
if (b== 0)
cout<<"Division by zero!"<<endl;
else
cout<<a/b<<endl;
break;
default:
cout<<"Invalid operator!"<<endl;
}
getch();
}

6.5
Write a program that inputs a number from the user and displays a
table of that number using while loop.
#include<iostream.h>
#include<conio.h>
void main()
{
int n, c;
clrscr();
c = 1;
cout<<”Enter a number”;
cin>>n;
while(c <= 10 )
{
cout<<n<<” * “<<c<<” = “<<n*c<<endl;
c = c+1;
}
getch();
}
6.6
Write a program that inputs an artener and displays the sum of its
digits. For example the program should display 9 if the user enters
135.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int x, a, r, sum = 0;
cout<<”Enter an integer:”;
cin>>x;
a = x;
while (x != 0)
{
r=x% 10;
if (r == 0)
sum = sum+x;
else
sum = sum+r;
x = x/10;
}
cout<<” The sum of digits of “<<a<<”= “<<sum;
getch();
}
6.15
Write a program that uses a while loop to enter number from the
user and then display. The loop is terminated when the user enters -
1.
#include<iostream.h>
#include<conio.h>
void main()
{
int n;
n = 1;
while(n ! = -1)
{
cout<<”Enter a number: “;
cin>>n;
cout<<”You entered “<<n<<endl;
}
cout<<”End of Program”;
getch();
}
6.19
Write a program that gets two numbers from the user and displays
the result of first number raise to the power of second number using
do-while loop.
#include<iostream.h>
#include<conio.h>
void main()
{
int a, b, c, r;
clrscr();
cout<<”Enter first number”;
cin>>a;
cout<<”Enter second number: “;
cin>>b;
c = 1;
r = 1;
do
{
r = r * a;
c = c + 1;
}
while(c<=b);
cout<<”Result is “<<r;
getch();
}
6.21
Write a program that gets starting and ending point from the user
and displays all odd numbers in the given range using do-while loop.
#include<iostream.h>
#include<conio.h>
void main()
{
int c, s, e;
clrscr();
cout<<”Enter starting number”;
cin>>s;
cout<<” Enter ending number : “;
cin>>e;
c=s;
do
if(c%2!=0)
cout<<c<<endl;
c =c+1;
}
while(c<=e);
getch();
}

You might also like