Académique Documents
Professionnel Documents
Culture Documents
Assignment 1
Q1: WAP to display the following output using a single cout statement.
Maths=90
Physics=77
Chemistry=69
Output: maths=90
Physics =77
Chemistry =69
Page no: 1
Prepared by: SALMAN
OOPs Practical File
Assignment 1
Page no: 2
Prepared by: SALMAN
OOPs Practical File
Assignment 1
Page no: 3
Prepared by: SALMAN
OOPs Practical File
Assignment 1
Q4: WAP to read the values a, b and c and display the value of x, where
X=a/b-c
Test your program for the following values:
(a) a=250, b=85, c=25.
(b) a=300, b=70, c=70.
Output: (a)
Enter values for a, b, c
250
85
25
Value of x is = -22.058823
Page no: 4
Prepared by: SALMAN
OOPs Practical File
Assignment 1
Page no: 5
Prepared by: SALMAN
OOPs Practical File
Assignment 1
Q6: WAP to find the simple interest for a given principle, rate of interest and
number of years.
Page no: 6
Prepared by: SALMAN
OOPs Practical File
Assignment 1
Q7: WAP to read name, age, sex, height and weight of a student display with
proper heading for each variable.
height= 5.7
weight= 67
Page no: 7
Prepared by: SALMAN
OOPs Practical File
Assignment 1
Q8: WAP to read a set of five numbers from the keyboard and find out their
sum and average.
Page no: 8
Prepared by: SALMAN
OOPs Practical File
Assignment 1
Sol: #include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
float temp,countemp;
cout<<"\n"<<"Enter temperature in Fahrenheit:";
cin>>temp;
countemp=(temp-32)/1.8;
cout<<"The temperature in Celsius is :"<<countemp<<"\n";
getch();
}
Output:
Enter temperature in fahrenheit:100
The temperature in Celsius is : 37.777779
Page no: 9
Prepared by: SALMAN
OOPs Practical File
Assignment 1
Q10: WAP to find the sum of 5 digit number entered by user (without
conditions).
Page no:
Prepared10by: SALMAN
OOPs Practical File
Assignment 1
Q11. WAP to find the reverse of a 5 digit number entered by user (without
conditions).
Sol:
//print in reverse order
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int no, reverse, first, second, third, fourth, fifth;
cout<<"enter 5 digit no";
cin>>no;
first=no%10;
no=no/10;
second=no%10;
no=no/10;
third=no%10;
no=no/10;
fourth=no%10;
no=no/10;
fifth=no%10;
no=no/10;
cout<<"reverse=" <<first<<second<<third<<fourth<<fifth;
getch();
}
Output:
enter 5 digit no12345
reverse=54321
Page no:
Prepared11by: SALMAN
OOPs Practical File
Assignment 1
Q12: WAP to read two numbers from the keyboard and display the largest
value on the screen (for 2 numbers and 3numbers).
else if(b>c&&b>a)
cout<<"b is greater";
else
cout<<"c is greater";
getch();
}
Output:
enter value for a, for b, for c
23
45
12
b is greater
Page no:
Prepared13by: SALMAN
OOPs Practical File
Assignment 1
Q13: WAP to display the name of the day in a week, depending upon the
number which is entered by the keyboard using (else-if structure, switch-case
statement).
Sol: Switch-case
//program to display days for the no entered
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int ch;
cin>>ch;
switch (ch)
{
case 1 : cout<<"sunday";
break;
case 2 : cout<<"monday";
break;
case 3 : cout<<"tuesday";
break;
case 4 : cout<<"wednesday";
break;
case 5 : cout<<"thursday";
break;
case 6 : cout<<"friday";
break;
case 7 : cout<<"saturday";
break;
default : cout<<"wrong no";
}
getch();
}
Output: 1
Sunday
Page no:
Prepared14by: SALMAN
OOPs Practical File
Assignment 1
Else-if structure
//program to display day for the no entered
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
char w;
cout<<"enter the week no";
cin>>w;
if(w=='1')
cout<<"sunday";
else if(w=='2')
cout<<"monday";
else if(w=='3')
cout<<"tuesday";
else if(w=='4')
cout<<"wednesday";
else if(w=='5')
cout<<"thursday";
else if(w=='6')
cout<<"friday";
else if(w=='7')
cout<<"saturday";
else cout<<"wrong number";
getch();
}
Page no:
Prepared15by: SALMAN
OOPs Practical File
Assignment 1
Sol:
//to print 0 to 10
#include<iostream.h>
#include<conio.h>
void main()
{clrscr();
int i;
for(i=0;i<=10; i++)
{
cout<<"\n"<<i;
}
getch();
}
Output:
0
1
2
3
4
5
6
7
8
9
10
Page no:
Prepared16by: SALMAN
OOPs Practical File
Assignment 1
Sol:
//print sum and average of the numbers entered by user
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a, b, sum, avg;
cout<<"enter the first number";
cin>>a;
cout<<"enter the second number";
cin>>b;
sum=(a+b);
avg=sum/2;
cout<<"sum is:"<<sum<<"\n"<<"average is:"<<avg;
cout<<avg;
getch();
}
Page no:
Prepared17by: SALMAN
OOPs Practical File
Assignment 1
Output:
0 9
1 8
2 7
3 6
4 5
5 4
6 3
7 2
8 1
9 0
Page no:
Prepared18by: SALMAN
OOPs Practical File
Assignment 1
Output: 5050
Page no:
Prepared19by: SALMAN
OOPs Practical File
Assignment 1
Q18: WAP to display whether a number entered by the user is even or odd.
Page no:
Prepared20by: SALMAN
OOPs Practical File
Assignment 1
Q19: WAP to display whether a number entered by a user is even or odd. For
first 50 numbers.
Sol:
//to display whether the no entered is even or odd for first 50 numbers
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int num;
cout<<"enter the no ";
cin>>num;
if (num%2==0&&num<=50)
cout<<"even no";
else if(num>50)
cout<<"it is more than the limit";
else
cout<<"odd no";
getch();
}
enter the no 51
it is more than the limit
enter the no 49
odd no
Page no:
Prepared21by: SALMAN
OOPs Practical File
Assignment 1
Q20: WAP to display first 100 odd numbers using the while loop.
Output: 3
5
7
9
11
13
15
17
19
21
23
25
27
29
31
33
35
37
39
41
43
45
47
49
Page no:
Prepared22by: SALMAN
OOPs Practical File
Assignment 1
51
53
55
57
59
61
63
65
67
69
71
73
75
77
79
81
83
85
87
89
91
93
95
97
99
Q21: WAP to find the square and cube of the numbers from 1 to 10 using
(i) for loop (ii) while-loop (iii) do-while loop
the output should be as follows:
numbers squares cubes
------------ ----------- ---------
Page no:
Prepared23by: SALMAN
OOPs Practical File
Assignment 1
{
s=i*i;
c=i*i*i;
cout<<"\n"<<i<<"\t";
cout<<s<<"\t";
cout<<c<<"\n";
}
getch();
}
getch();
}
Page no:
Prepared24by: SALMAN
OOPs Practical File
Assignment 1
Page no:
Prepared25by: SALMAN
OOPs Practical File
Assignment 1
Output:
2 4 8
3 9 27
4 16 64
5 25 125
6 36 216
7 49 343
8 64 512
9 81 729
10 100 1000
Page no:
Prepared26by: SALMAN
OOPs Practical File
Assignment 1
Q22: Modify Q21 so that it prints the square and cubes of odd numbers only.
Output:
number square cube
1 1 1
3 9 27
5 25 125
7 49 343
9 81 729
Page no:
Prepared27by: SALMAN
OOPs Practical File
Assignment 1
}
Using do-while structure
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int sum=0,num,i;
cout<<"enter the number =";
cin>>num;
i=1;
do
{
sum=sum+i;
cout<<sum<<"\t";
i++;
}
while(i<=num);
getch();
}
for(int i=1;i<=num;i++)
{
if(i%2==1)
{
sum=sum+i;
cout<<sum<<"\t";
}
}
while(i<=num);
getch();
}
Page no:
Prepared30by: SALMAN
OOPs Practical File
Assignment 1
Page no:
Prepared32by: SALMAN
OOPs Practical File
Assignment 1
if(i%10==1)
{
sum=sum+a;
}
cout<<sum<<"\t";
++i;
}
getch();
}
sum=sum+square;
cout<<sum<<"\t";
i++;
}
getch();
}
(h) Sum=13-23+33-43+……….+n3
#include<conio.h>
void main()
{
clrscr();
int sum=0,num,cube;
cout<<"enter the number =";
cin>>num;
for(int i=1;i<=num;i++)
{
cube=i*i*i;
if(i%10==0)
{
sum=sum-cube;
}
else
if(i%10==1)
{
sum=sum+cube;
}
cout<<sum<<"\t";
}
getch();
}
{
sum=sum-cube;
}
else
if(i%10==1)
{
sum=sum+cube;
}
cout<<sum<<"\t";
}
getch();
}
getch();
}
(i) Sum=12+22+42+62+……….+n2
cin>>num;
while(i<=num)
{
if(i%2==0)
{
square=i*i;
sum=sum+square;
cout<<sum<<"\t";
}
i++;
}
getch();
}
Page no:
Prepared41by: SALMAN
OOPs Practical File
Assignment 1
(j) Sum=12-32+52-72+………..+n2
Page no:
Prepared42by: SALMAN
OOPs Practical File
Assignment 1
{
if(i%2==0)
{
i=i+1;
square=i*i;
sum=sum-square;
}
else
{
square=i*i;
sum=sum+square;
}
cout<<sum<<"\t";
++i;
}
while(i<=num);
getch();
}
Page no:
Prepared44by: SALMAN
OOPs Practical File
Assignment 1
Sol:(a)
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
for(int i=1; i<=5; i++)
{
cout<<"\n";
for(int j=i; j>=1; j--)
cout<<j;
}
getch();
}
Output:
1
21
321
4321
54321
(b)
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
for(int i=1; i<=5; i++)
{
cout<<"\n";
{
for(int j=1; j<=i; j++)
{
cout<<j;}}}
getch();
}
Page no:
Prepared45by: SALMAN
OOPs Practical File
Assignment 1
Output:
1
12
123
1234
12345
(c) #include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
for(int i=5; i>=1; i--)
{
cout<<"\n";
{
for(int j=i; j>=1; j--)
{
cout<<j;
}
}
}
getch();
}
Output:
54321
4321
321
21
1
Page no:
Prepared46by: SALMAN
OOPs Practical File
Assignment 1
(d) #include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
for(int i=5;i>=1;i--)
{
cout<<"\n";
{
for(int j=1;j<=i;j++)
cout<<j;
}
}
getch();
}
Output:
12345
1234
123
12
1
Page no:
Prepared47by: SALMAN
OOPs Practical File
Assignment 1
Q26: WAP that prints a given number whether it is a prime number or not,
using:
(a) for-loop (b) while-loop (c) do-while loop.
{
flag=1;
break;
}
}
if (flag==0)
cout<<"it is a prime no";
else
cout<<"it is not a prime";
getch();
}
Page no:
Prepared49by: SALMAN
OOPs Practical File
Assignment 1
Page no:
Prepared50by: SALMAN
OOPs Practical File
Assignment 1
Sol:
Page no:
Prepared51by: SALMAN
OOPs Practical File
Assignment 1
Sol:
//fibonacci series limit entered by user
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int l, a, b, s=0;
cout<<"enter the limit";
cin>>l;
a=0;
b=1;
cout<<a<<b;
for(int i=1; i<=l - 2; i++)
{
s=a+b;
cout<<s;
a=b;
b=s;
}
getch();
}
Q29: WAP to check whether the digit is present in number ‘n’. Also count
how many times it is repeated in the number n and digit exists on which
position.
N =123765
Digit to check 5
The digit is present twice at position 3 and 6.
Page no:
Prepared52by: SALMAN
OOPs Practical File
Assignment 1
Sol:
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int n,digit,a,count=0;
cout<<"N=";
cin>>n;
cout<<"digit to check= ";
cin>>digit;
while(n!=0)
{
a=n%10;
n=n/10;
if(digit==a)
{
count++;
n++;
}
}
cout<<"The digit is present"<<count<<"time(s)";
getch();
}
Output: N=13786533
digit to check= 3
The digit is present 3 time(s)
Q30: WAP to read a number ‘n’ from user and print the sum of its digits.
Page no:
Prepared53by: SALMAN
OOPs Practical File
Assignment 1
Sol:
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int n,l,sum=0,a;
cout<<"enter the limit";
cin>>l;
cout<<"enter the number= ";
cin>>n;
for(int i=1;i<=l;++i)
{
a=n%10;
n=n/10;
sum=sum+a;
}
cout<<"sum="<<sum;
getch();
}
Q31: WAP read a positive integer number ‘n’ and generate the numbers in
the following form:
Enter the number: 5
54321012345
Page no:
Prepared54by: SALMAN
OOPs Practical File
Assignment 1
Sol:
Q33: WAP to print the numbers less than 2000 that are divisible by 10 using
If-then-else and goto statements.
Sol:
}
getch();
}
Output: 0
1
2
3
4
5
6
7
8
9
Page no:
Prepared57by: SALMAN
OOPs Practical File
Assignment 1
Q34: WAP to check whether two numbers are equal or not using ternary
operator.
Sol:
Q35: WAP to enter the two numbers and an operator depending upon the
operator perform the calculations.
clrscr();
char ch;
int n1, n2, result;
cout<<"enter the operator: ";
cin>>ch;
cout<<"enter two numbers: ";
cin>>n1>>n2;
if(ch=='+')
{
result=n1+n2;
cout<<"result is "<<result;
}
else if(ch=='-')
{
result=n1-n2;
cout<<"result is "<<result;
}
else if(ch=='*')
{
result=n1*n2;
cout<<"result is "<<result;
}
else if(ch=='/')
{
result=n1/n2;
cout<<"result is "<<result;
}
else if(ch=='%')
{
result=n1%n2;
cout<<"result is "<<result;
}
getch();
}
Page no:
Prepared59by: SALMAN
OOPs Practical File
Assignment 1
Q36: WAP to calculate sales and commission using the following table:
Sales commission
30000 onwards 15%
22001-30000 10%
12001-22000 7%
5001-12000 3%
0-5000 0%
Sol:
//calculate sales and commission
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
float sales, comm;
cout<<"enter value for sales";
cin>>sales;
if (sales>=30001)
{
comm=sales*15/100;
cout<<comm;
}
else if(sales>=22001&&sales<=30000)
{
comm=sales*10/100;
cout<<comm;
}
else if(sales>12001&&sales<=22000)
{
comm=sales*7/100;
cout<<comm;
}
else if(sales>=5001&&sales<=12000)
{
comm=sales*3/100;
cout<<comm;
}
else if(sales>=0&&sales<=5000)
{
Page no:
Prepared60by: SALMAN
OOPs Practical File
Assignment 1
comm=sales*0/100;
cout<<comm;
}
else cout<<"you have entered wrong sales amount";
getch();
}
Page no:
Prepared61by: SALMAN
OOPs Practical File
Assignment 1
Page no:
Prepared62by: SALMAN
OOPs Practical File
Assignment 1
Page no:
Prepared63by: SALMAN
OOPs Practical File
Assignment 1
Page no:
Prepared64by: SALMAN
OOPs Practical File
Assignment 1
Page no:
Prepared65by: SALMAN
OOPs Practical File
Assignment 1
Page no:
Prepared66by: SALMAN
OOPs Practical File
Assignment 1
Page no:
Prepared67by: SALMAN
OOPs Practical File
Assignment 1
Page no:
Prepared68by: SALMAN
OOPs Practical File
Assignment 1
Page no:
Prepared69by: SALMAN
OOPs Practical File
Assignment 1
Page no:
Prepared70by: SALMAN
OOPs Practical File
Assignment 1
Page no:
Prepared71by: SALMAN
OOPs Practical File
Assignment 1
Page no:
Prepared72by: SALMAN