Vous êtes sur la page 1sur 72

OOPs Practical File

Assignment 1

Q1: WAP to display the following output using a single cout statement.
Maths=90
Physics=77
Chemistry=69

Sol: //print output in a single cout statement


#include<iostream.h>
#include<conio.h>
Void main ()
{
Clrscr ();
Cout<<"maths=90"<<"\n"<<"physics=77"<<"\n"<<"chemistry=69";
Getch ();
}

Output: maths=90
Physics =77
Chemistry =69

Page no: 1
Prepared by: SALMAN
OOPs Practical File
Assignment 1

Q2: WAP to display on the screen:


a. character ‘z’ b. the name ‘Mohan’ c. the number 1977
using (i) single cout statement (ii) multiple cout statement.

Sol: SINGLE COUT STATEMENT


//display characters using single cout statement
#include<iostream.h>
#include<conio.h>
Void main ()
{
Clrscr ();
Cout<<"z"<<"\t"<<"mohan"<<"\t"<<"1977";
Getch ();
}
MULTIPLE COUT STATEMENT
//display characters using multiple cout statement
#include<iostream.h>
#include<conio.h>
Void main ()
{
Clrscr();
Cout<<"z"<<"\n";
Cout<<"mohan"<<"\n";
Cout<<"1977";
Getch ();
}

Output: z mohan 1977

Page no: 2
Prepared by: SALMAN
OOPs Practical File
Assignment 1

Q3: WAP to generate the following table:


1992 17421
1993 29210
1994 100523
1995 106802
1996 127000
(Use a single cout statement for output).

Sol: //To generate table using single cout


#include<iostream.h>
#include<conio.h>
Void main ()
{
Clrscr();
Cout<<"1992 \t 17421" "\n"<<"1993 \t 29210" "\n"<<"1994 \t 100523"
"\n"<<"1995 \t 106802" "\n"<<"1996 \t 127000";
Getch ();
}

Output: 1992 17421


1993 29210
1994 100523
1995 106802
1996 127000

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.

Sol: //calculate the values


#include<iostream.h>
#include<conio.h>
Void main ()
{
Clrscr();
Float a, b, c, x;
Cout<<"enter values for a, b, c";
Cin>>a>>b>>c;
x=a/b-c;
Cout<<"value of x is =" "\t" <<x;
Getch ();
}

Output: (a)
Enter values for a, b, c
250
85
25
Value of x is = -22.058823

(b) Enter values for a, b, c


300
70
70
Value of x is = -65.714287

Page no: 4
Prepared by: SALMAN
OOPs Practical File
Assignment 1

Q5: WAP to perform the following:


(i) Area of a circle
(ii) Circumference of a circle
(iii) Area of a triangle
(iv) Area of a rectangle

Sol: //calculate area and circumference


#include<iostream.h>
#include<conio.h>
Void main ()
{
Clrscr();
Float r, l, b, h, ar, cir, tri, rect, pie=3.14;
Cout<<"enter radius, length, breadth, height";
cin>>r>>l>>b>>h;
ar = pie * r * r;
cir = 2 * pie * r;
tri = .5 * b * h;
rect = l * b * h;
Cout<<"the area of circle is" "\t" <<ar <<"\n";
Cout<<"the circumference of circle is" "\t" <<cir <<"\n";
Cout<<"the area of triangle is" "\t" <<tri <<"\n";
Cout<<"the area of rectangle is" "\t" <<rect <<"\n";
Getch ();
}

Output: enter radius, length, breadth, height


12
23
34
53
the area of circle is 452.160004
the circumference of circle is 75.360001
the area of triangle is 901
the area of rectangle is 41446

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.

Sol: //simple interest by user input


#include<iostream.h>
#include<conio.h>
Void main ()
{
Clrscr();
Float si, p, r, t;
Cout<<"enter value of principle, rate, time";
Cin>>p>>r>>t;
si= p*r*t/100;
Cout<<"the value of simple interest is" "\t" <<si;
Getch ();
}

OUTPUT: enter value of principle, rate, time


1000
10
2
the value of simple interest is 200

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.

Sol: //print age, height, weight by user input


#include<iostream.h>
#include<conio.h>
Void main ()
{
Clrscr();
Float a, h, w;
Cout<<"enter your age, height, weight";
Cin>>a>>h>>w;
Cout<<"age=" "\t" <<a <<"\n";
Cout<<"\n";
Cout<<"height=" "\t" <<h <<"\n";
Cout<<"\n";
Cout<<"weight=" "\t" <<w <<"\n";
Cout<<"\n";
Getch ();
}

Output: enter your age, height, weight


18
5.7
67
age= 18

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.

Sol: //sum and average of five numbers


#include<iostream.h>
#include<conio.h>
Void main()
{
Clrscr();
Float n1, n2, n3, n4, n5, sum, avg;
Cout<<"enter the numbers";
Cin>>n1>>n2>>n3>>n4>>n5;
Sum=n1+n2+n3+n4+n5;
avg= sum/5;
Cout<<"the sum is" "\t" <<sum <<"\n";
Cout<<"\n";
Cout<<"the average is" "\t" <<avg <<"\n";
Getch ();
}

Output: enter the numbers


2
3
4
5
6
the sum is 20
the average is 4

Page no: 8
Prepared by: SALMAN
OOPs Practical File
Assignment 1

Q9: WAP to that will ask a temperature in Fahrenheit and display it in


Celsius.

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).

Sol: //sum of 5 digits


#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int no, sum, 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;
Sum=first+second+third+fourth+fifth;
cout<<"sum of 5 digits are" "\t" <<sum;
getch();
}

Output: enter 5 digit no23456


sum of 5 digits are 20

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).

Sol: For 2 numbers:


//find greater between two numbers
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a, b;
cout<<"enter value for a, enter value for b";
cin>>a>>b;
if(a>b)
cout<<"a is greater";
else cout<<"b is greater";
getch();
}

Output: enter value for a, enter value for b


45
12
a is greater
For 3 numbers:
//find greater between three numbers
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a, b, c;
cout<<"enter value for a, for b, for c";
cin>>a>>b>>c;
if(a>b&&a>c)
cout<<"a is greater";
Page no:
Prepared12by: SALMAN
OOPs Practical File
Assignment 1

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();
}

Output: enter the week no2


Monday

Page no:
Prepared15by: SALMAN
OOPs Practical File
Assignment 1

Q14: WAP to display numbers from 0 to 10 using for loop.

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

Q15: WAP to find the sum and average of given numbers.

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();
}

Output: enter the first number2


enter the second number3
sum is:5
average is:22

Page no:
Prepared17by: SALMAN
OOPs Practical File
Assignment 1

Q16: WAP to display the numbers between 0 to 9 as well as 9 to 0


simultaneously one by one.

Sol: //To print 0-9 and 9-0 simultaneously


#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i, j;
for(i=0, j=9; i<=9, j>=0; i++, j--)
{
cout<<"\n"<<i<<"\t";
cout<<j<<"\n";
}
getch();
}

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

Q17: WAP to find the sum of first 100 natural numbers.


Sum=1+2+3+…………100

Sol: //print sum of first 100 natural numbers


#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int sum=0,i=1;
for (;i<=100; ++i)
sum=sum+i;
cout<<s;
getch();
}

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.

Sol: //to display whether the no entered is even or odd


#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int num;
cout<<"enter the no";
cin>>num;
if (num%2==0)
cout<<"even no";
else cout<<"odd no";
getch();
}

Output: enter the no 2


even no
enter the no 3
odd no

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();
}

Output: enter the no 50


even no

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.

Sol://print first 100 odd numbers


#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i=1;
while(i<=100)
{
cout<<i<<"\n";
++++i;
}
getch();
}

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

Sol: Using for loop structure:


//square & cube of 10 no's
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
long i, s, c;
cout<<"num\tsquare\tcube";
for(i=1; i<=10; i++)

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();
}

Using while-loop structure:


//square & cube of 10 no's using while loop
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
long i=1, s, c;
cout<<"num\tsquare\tcube";
while (i<=10)
{
s=i*i;
c=i*i*i;
{
cout<<"\n"<<i<<"\t";
{
cout<<s<<"\t";
{
cout<<c<<"\n";
}
}
}
i++;
}

getch();
}

Page no:
Prepared24by: SALMAN
OOPs Practical File
Assignment 1

Using do-while loop structure:


//square & cube of 10 no's using do-while loop
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
long i=1, s, c;
cout<<"num\tsquare\tcube";
do
{
s=i*i;
c=i*i*i;
{
cout<<"\n"<<i<<"\t";
{
cout<<s<<"\t";
{
cout<<c<<"\n";
}
}
}
i++;
}
while (i<=10);
getch();
}

Page no:
Prepared25by: SALMAN
OOPs Practical File
Assignment 1

Output:

num square cube


1 1 1

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.

Sol: //square & cube of odd no's upto 10


#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int so=1, co=1;
cout<<"number\tsquare\tcube";
for(int j=1;j<=10;j+=2)
{
so=j*j;
co=j*j*j;
cout<<"\n"<<j<<"\t"<<so<<"\t"<<co<<"\t"<<"\n";
}
getch();
}

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

Q23: WAP to find the sum of the following series using:


(i) For loop (ii) while loop (iii) do-while loop

Sol: (a) Sum =1+2+3+……….+n


Using for-loop structure:
//program to display the sum till n
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i, l, sum=0;
cout<<"enter the number =";
cin>>l;
for(i=1; i<=l; i++)
{
sum =sum + i;
cout<<sum;
}
getch();
}

Using while-loop structure


//program to display sum till n
#include<iostream.h>
#include<conio.h>
void main()
{clrscr();
int sum=0,num,i;
cout<<"enter the number =";
cin>>num;
i=1;
while(i<=num)
{
sum=sum+i;
cout<<sum<<"\t";
i++;
}
getch();
Page no:
Prepared28by: 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();
}

Output: enter the number =10


1 3 6 10 15 21 28 36 45 55

(b) Sum =1+3+5+……….+n

Using for-loop structure:


//program to display the sum till n
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int sum=0,num;
cout<<"enter the number =";
cin>>num;
Page no:
Prepared29by: SALMAN
OOPs Practical File
Assignment 1

for(int i=1;i<=num;i++)
{
if(i%2==1)
{
sum=sum+i;
cout<<sum<<"\t";
}
}
while(i<=num);
getch();
}

Using while loop structure

//program to display the sum till n


#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int sum=0,num,i;
cout<<"enter the number =";
cin>>num;
i=1;
while(i<=num)
{
if(i%2==1)
{
sum=sum+i;
cout<<sum<<"\t";
}
i++;
}
getch();
}

Page no:
Prepared30by: SALMAN
OOPs Practical File
Assignment 1

Using do-while loop structure

//program to display the sum till n


#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int sum=0,num,i;
cout<<"enter the number =";
cin>>num;
i=1;
do
{
if(i%2==1)
{
sum=sum+i;
cout<<sum<<"\t";
}
i++;
}
while(i<=num);
getch();
}

Output: enter the number =10


1 4 9 16 25

(c) Sum =1+2+4+………..+n

Using for loop structure


//program to display the sum till n
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int sum=1,num;
Page no:
Prepared31by: SALMAN
OOPs Practical File
Assignment 1

cout<<"enter the number =";


cin>>num;
for(int i=1;i<=num;i++)
{
if(i%2==0)
{
sum=sum+i;
cout<<sum<<"\t";
}
}
getch();
}

Using while-loop structure

//program to display the sum till n


#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int sum=1,num,i;
cout<<"enter the number =";
cin>>num;
i=1;
while(i<=num)
{
if(i%2==0)
{
sum=sum+i;
cout<<sum<<"\t";
}
i++;
}
getch();
}

Page no:
Prepared32by: SALMAN
OOPs Practical File
Assignment 1

Using do-while loop structure

//program to display the sum till n


#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int sum=1,num,i;
cout<<"enter the number =";
cin>>num;
i=1;
do
{
if(i%2==0)
{
sum=sum+i;
cout<<sum<<"\t";
}
i++;
}
while(i<=num);
getch();
}

Output: enter the number =10


3 7 13 21 31

(d) Sum = 1-1/1!+2/2!-3/3!+4/4!-………r/n!

Using for loop structure


//program to display the sum till n
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int sum=1,num,fact=1,a;
Page no:
Prepared33by: SALMAN
OOPs Practical File
Assignment 1

cout<<"enter the number =";


cin>>num;
for(int i=1;i<=num;i++)
{
fact=fact*i;
a=i/fact;
if(i%10==0)
{
sum=sum-a;
}
else
if(i%10==1)
{
sum=sum+a;
}
cout<<sum<<"\t";
}
getch();
}

Using while loop structure


//program to display the sum till n
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int sum=1,num,fact=1,a,i=1;
cout<<"enter the number =";
cin>>num;
while(i<=num)
{
fact=fact*i;
a=i/fact;
if(i%10==0)
{
sum=sum-a;
}
else
Page no:
Prepared34by: SALMAN
OOPs Practical File
Assignment 1

if(i%10==1)
{
sum=sum+a;
}
cout<<sum<<"\t";
++i;
}
getch();
}

Using do-while loop structure

//program to display the sum till n


#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int sum=1,num,fact=1,a,i=1;
cout<<"enter the number =";
cin>>num;
do
{
fact=fact*i;
a=i/fact;
if(i%10==0)
{
sum=sum-a;
}
else
if(i%10==1)
{
sum=sum+a;
}
cout<<sum<<"\t";
++i;
}
while(i<=num);
getch();
}
Page no:
Prepared35by: SALMAN
OOPs Practical File
Assignment 1

Output: enter the number =10


2 2 2 2 2 2 2 2 2 2

(g) Sum= 12+22+32+42+………+n2

Using for loop structure


//program to display the sum till n
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int sum=0,num,i,square;
cout<<"enter the number =";
cin>>num;
i=1;
for (int i=1;i<=num;i++)
{
square=i*i;
sum=sum+square;
cout<<sum<<"\t";
}
getch();
}

Using while loop structure


//program to display the sum till n
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int sum=0,num,i,square;
cout<<"enter the number =";
cin>>num;
i=1;
while(i<=num)
{
square=i*i;
Page no:
Prepared36by: SALMAN
OOPs Practical File
Assignment 1

sum=sum+square;
cout<<sum<<"\t";
i++;
}
getch();
}

Using do-while loop structure


//program to display the sum till n
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int sum=0,num,i,square;
cout<<"enter the number =";
cin>>num;
i=1;
do
{
square=i*i;
sum=sum+square;
cout<<sum<<"\t";
i++;
}
while(i<=num);
getch();
}

Output: enter the number =10


1 5 14 30 55 91 140 204 285 385

(h) Sum=13-23+33-43+……….+n3

Using for loop structure


//program to display the sum till n
#include<iostream.h>
Page no:
Prepared37by: SALMAN
OOPs Practical File
Assignment 1

#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();
}

Using while loop structure

//program to display the sum till n


#include<iostream.h>
#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)
Page no:
Prepared38by: SALMAN
OOPs Practical File
Assignment 1

{
sum=sum-cube;
}
else
if(i%10==1)
{
sum=sum+cube;
}
cout<<sum<<"\t";
}
getch();
}

Using do-while loop structure

//program to display the sum till n


#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int sum=0,num,cube,i=1;
cout<<"enter the number =";
cin>>num;
do
{
cube=i*i*i;
if(i%10==0)
{
sum=sum-cube;
}
else
if(i%10==1)
{
sum=sum+cube;
}
cout<<sum<<"\t";
++i;
}
while(i<=num);
Page no:
Prepared39by: SALMAN
OOPs Practical File
Assignment 1

getch();
}

Output: enter the number =10


1 5 14 30 55 91 140 204 285 385

(i) Sum=12+22+42+62+……….+n2

Using for loop structure


//program to display the sum till n
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int sum=1,num,square;
cout<<"enter the number =";
cin>>num;
for(int i=1;i<=num;i++)
{
if(i%2==0)
{
square=i*i;
sum=sum+square;
cout<<sum<<"\t";
}
}
getch();
}

Using while loop structure


//program to display the sum till n
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int sum=1,num,i,square;
cout<<"enter the number =";
Page no:
Prepared40by: SALMAN
OOPs Practical File
Assignment 1

cin>>num;
while(i<=num)
{
if(i%2==0)
{
square=i*i;
sum=sum+square;
cout<<sum<<"\t";
}
i++;
}
getch();
}

Using do-while loop structure


//program to display the sum till n
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int sum=1,num,i,square;
cout<<"enter the number =";
cin>>num;
i=1;
do
{
if(i%2==0)
{
square=i*i;
sum=sum+square;
cout<<sum<<"\t";
}
i++;
}
while(i<=num);
getch();
}

Page no:
Prepared41by: SALMAN
OOPs Practical File
Assignment 1

Output : enter the number =10


5 21 57 121 221

(j) Sum=12-32+52-72+………..+n2

Using for loop structure


//program to display the sum of odd numbers till n
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int sum=1,num,square;
cout<<"enter the number =";
cin>>num;
for(int i=1;i<=num;i++)
{
if(i%2==0)
{
i=i+1;
square=i*i;
sum=sum-square;
}
else
{
square=i*i;
sum=sum+square;
}
cout<<sum<<"\t";
}
getch();
}

Page no:
Prepared42by: SALMAN
OOPs Practical File
Assignment 1

Using while loop structure


//program to display the sum of odd numbers till n
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int sum=1,num,square,i=1;
cout<<"enter the number =";
cin>>num;
while(i<=num)
{
if(i%2==0)
{
i=i+1;
square=i*i;
sum=sum-square;
}
else
{
square=i*i;
sum=sum+square;
}
cout<<sum<<"\t";
++i;
}
getch();
}

Using do-while loop structure


//program to display the sum of odd numbers till n
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int sum=1,num,square,i=1;
cout<<"enter the number =";
cin>>num;
do
Page no:
Prepared43by: 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();
}

Output: enter the number =10


2 -7 -32 -81 -162 -283

Q24: WAP to generate the following series of numbers:


(a)1 (b)1 (c)5 4 3 2 1 (d)1 2 3 4 5
21 12 4321 1234
321 123 321 123
4321 1234 21 12
54321 12345 1 1

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

Q25: WAP to print factorial of a given number.

Sol: //print factorial upto limit entered by user


#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int no, f=1;
cout<<"enter the no";
cin>>no;
for(int i=1; i<=no; i++)
f=f*i;
cout<<f;
getch();
}

Output: enter the no134


0

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.

Sol:(a) Using for-loop structure


//print no entered is prime or not
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int n, flag=0;
cout<<"enter the no";
cin>>n;
for(int i=2; i<=n-1; i++)
{
if (n%i==0)
Page no:
Prepared48by: SALMAN
OOPs Practical File
Assignment 1

{
flag=1;
break;
}
}
if (flag==0)
cout<<"it is a prime no";
else
cout<<"it is not a prime";
getch();
}

Using while loop structure


//print no entered is prime or not
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int n, flag=0,i;
cout<<"enter the no";
cin>>n;
i=2
while( i<=n-1)
{
if (n%i==0)
{
flag=1;
break;
}
}
if (flag==0)
cout<<"it is a prime no";
else
cout<<"it is not a prime";
i++;
getch();
}

Page no:
Prepared49by: SALMAN
OOPs Practical File
Assignment 1

Using do-while loop structure


//print no entered is prime or not
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int n, flag=0,i;
cout<<"enter the no";
cin>>n;
i=2;
do
{
if (n%i==0)
{
flag=1;
break;
}
if (flag==0)
cout<<"it is a prime no";
else
cout<<"it is not a prime";
i++;
}
While(i<=num-1);
getch();
}

Output: enter the no 3


it is a prime no

Q27: WAP to solve a general quadratic equation ax2+bx+c=0.

Page no:
Prepared50by: SALMAN
OOPs Practical File
Assignment 1

Sol:

//program to solve quadratic equation


#include<iostream.h>
#include<conio.h>
#include<math.h>
void main()
{
clrscr();
float a, b, c, d, r1, r2;
cout<<"enter value for a";
cin>>a;
cout<<"enter value for b";
cin>>b;
cout<<"enter value for c";
cin>>c;
d=(b*b)-(4*a*c);
if(d>0)
{
r1=-b+sqrt(d)/2*a;
r2=-b-sqrt(d)/2*a;
cout<<r1<<r2;
}
else if(d==0)
{
r1=-b/2*a;
r2=-b/2*a;
cout<<r1<<r2;
}
else cout<<"roots are imaginary";
getch();
}

Output: enter value for a2


enter value for b8
enter value for c4
-2.343146-13.656855

Page no:
Prepared51by: SALMAN
OOPs Practical File
Assignment 1

Q28: WAP to generate a Fibonacci series of ‘n’ numbers, where n is defined


by a programmer.(1 1 2 3 5 8…….)

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();
}

Output: enter the limit10


0112358132134

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();
}

Output: enter the limit= 4


enter the number= 1234
sum= 10

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:

//To generate a series


#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int num;
cout<<"Enter the number";
cin>>num;
for(int i=num;i>=1;i--)
cout<<i;
{
for(int j=0;j<=num;j++)
cout<<j;
}
getch();
}

Output: Enter the number 9


9876543210123456789

Q32: WAP to print largest of two numbers using goto statements.


Sol: Using goto statement
//To print largest of two numbers
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a,b;
do
{
cout<<"Enter 1st no.";
cin>>a;
Page no:
Prepared55by: SALMAN
OOPs Practical File
Assignment 1

cout<<"Enter 2nd no.";


cin>>b;
if(a>b)
{
cout<<"1st is greater";
}
if(a<b)
{
cout<<"2nd is greater";
}
}
while(0);
getch();
}

Output: Enter 1st no.14


Enter 2nd no.21
2nd is greater

Q33: WAP to print the numbers less than 2000 that are divisible by 10 using
If-then-else and goto statements.

Sol:

Using if-then-else statement


//program to print the numbers less than 2000 and are divisible by 10
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int num;
for(num=0;num<=2000;++num)
{
if(num<=2000&&num/10==0)
cout<<num<<"\n";
Page no:
Prepared56by: SALMAN
OOPs Practical File
Assignment 1

}
getch();
}

Using goto statement


//program to print the numbers less than 2000 and are divisible by 10
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int num;
for(num=0;num<=2000;++num)
{
if(num<=2000&&num/10==0)
goto output;
output:
cout<<num<<"\n";
}
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:

//program to check whether the 2 numbers are equal or not


#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a,b,c;
cout<<"Enter a=";
cin>>a;
cout<<"Enter b=";
cin>>b;
c = (a == b ? 'T' : 'F');
cout<<c;
getch();
}

Output: Enter a=2


Enter b=2
116

Q35: WAP to enter the two numbers and an operator depending upon the
operator perform the calculations.

Sol: //accepting 2 no's from the user and performing operations


#include<iostream.h>
#include<conio.h>
void main()
{
Page no:
Prepared58by: SALMAN
OOPs Practical File
Assignment 1

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();
}

Output: enter the operator: +


enter two numbers: 2
3
result is 5

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();
}

Output: enter value for sales: 200000


commission is 30000

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

Vous aimerez peut-être aussi