Sem 3‎ > ‎OOPS (C++) LAB‎ > ‎

Q0 First Class. 5 Basic Questions.

posted Jul 19, 2011, 11:05 AM by Neil Mathew   [ updated Nov 10, 2011, 5:59 AM ]

WAP to find multiplication tables using functions.


SOURCE CODE:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream.h> 
// Unlike C, <stdio.h> is insufficient. (acc to DevC++)
#include <conio.h> 
// for getch(), not accepted in gcc (but accepted in DevC++)
 
void Table(int n)
{
int i=1; 
for(; i<=12; i++)
cout<<endl<<i<<" x "<<n<<" = "<<i*n;
}
 
int main() //gcc compiler requires int
{
int num;
printf("\n Enter number: ");
cin>>num;
 
printf("\n Table : \n");
Table(num);
 
getch();
return 0;
}

INPUT/OUTPUT:


 Enter number: 45

 Table :

1 x 45 = 45
2 x 45 = 90
3 x 45 = 135
4 x 45 = 180
5 x 45 = 225
6 x 45 = 270
7 x 45 = 315
8 x 45 = 360
9 x 45 = 405
10 x 45 = 450
11 x 45 = 495
12 x 45 = 540




WAP to make number half-pyramid 1:


1
1 2
1 2 3 
1 2 3 4
1 2 3 4 5

SOURCE CODE:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include<stdio.h>
#include<conio.h>
 
int main()
{
int i,j;
for(i=1; i<=5; i++)
{
for(j=1; j<=i; j++)
{
printf("%d ",j);
}
printf("\n");
}
 
getch();
return 1; 
}
 

INPUT/OUTPUT:


1
1 2
1 2 3
1 2 3 4
1 2 3 4 5





WAP to make number half-pyramid 2:


1
1 1
1 1 1 
1 1 1 1
1 1 1 1 1

SOURCE CODE:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include<stdio.h>
#include<conio.h>
 
int main()
{
int i,j;
for(i=1; i<=5; i++)
{
for(j=1; j<=i; j++)
{
printf("1 ");
}
printf("\n");
}
 
getch();
return 1;
}

INPUT/OUTPUT:


1
1 1
1 1 1
1 1 1 1
1 1 1 1 1




WAP to use switch case to find
(i) number is odd or even (ii) largest no among three entered


SOURCE CODE:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include<iostream.h>
#include <conio.h>
 
 
int main()
{
int ch,num1, num2, num3;
 
cout<<"\n MENU: \n";
cout<<"\n 1. Even/Odd ";
cout<<"\n 2. Largest of 3 ";
cout<<"\n Your Choice: ";
cin>>ch;
 
switch(ch)
{
case 1: 
cout<<"\n Enter number: ";
cin>>num1;
 
if(num1%2==0)
cout<<"\n Number is Even. ";
else
cout<<"\n Number is Odd. ";
break;
 
case 2:
cout<<"\n Enter First number: ";
cin>>num1;
 
cout<<"\n Enter Second number: ";
cin>>num2;
 
cout<<"\n Enter Third number: ";
cin>>num3;
 
if(num1>=num2 && num1>=num3)
cout<<"\n"<<num1<<" is greatest. ";
else if( num2>=num1 && num2>=num3)
cout<<"\n"<<num2<<" is greatest. ";
else
cout<<"\n"<<num3<<" is greatest. ";
break;
 
}
getch();
}
 
 


INPUT/OUTPUT:


 MENU:

 1. Even/Odd
 2. Largest of 3
 Your Choice: 1

 Enter number: 67

 Number is Odd.

 MENU:

 1. Even/Odd
 2. Largest of 3
 Your Choice: 2

 Enter First number: 67

 Enter Second number: 300

 Enter Third number: 45

 300 is greatest.





WAP to find sum and reverse of entered number's digits.


SOURCE CODE:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include<stdio.h>
#include<conio.h>
 
int main()
{
int i,digit,rev=0,sum=0, num;
 
printf("\n Enter the number: ");
scanf("%d",&num);
 
int n=num;
 
do
{
digit=num%10;
 
sum+=digit;
rev=rev*10+digit;
 
num/=10;
}
while(num>0);
 
printf("\n Reverse: %d",rev);
printf("\n Sum: %d", sum);
 
getch();
return 1;
}
 

INPUT/OUTPUT:

 
 Enter the number: 634

 Reverse: 436
 Sum: 13







Comments