0% found this document useful (0 votes)
2 views34 pages

C++ Program Question

The document contains a series of C++ programming exercises that cover various programming concepts such as printing output, using arithmetic and relational operators, handling user input, and implementing control structures. Each exercise includes code snippets demonstrating how to perform specific tasks, such as checking for leap years, finding the largest or smallest number, and determining if a number is a palindrome. The exercises are aimed at helping learners practice and understand fundamental programming principles in C++.

Uploaded by

rvrahul.9122
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views34 pages

C++ Program Question

The document contains a series of C++ programming exercises that cover various programming concepts such as printing output, using arithmetic and relational operators, handling user input, and implementing control structures. Each exercise includes code snippets demonstrating how to perform specific tasks, such as checking for leap years, finding the largest or smallest number, and determining if a number is a palindrome. The exercises are aimed at helping learners practice and understand fundamental programming principles in C++.

Uploaded by

rvrahul.9122
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

- Hira Kumar

1. Write a program in c++ to print “Hello wolrd !”


#include<iostream>
#include<conio.h> // for getch() not mandatory
using namespace std;
int main()
{
cout << "Hello World !";
getch();
return 0;

1
2. Use all arithmetical operator
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int a,b;
cout << "-------------All airthmetical operator--------------------" << endl <<endl;
cout << "Enter 1st number = ";
cin >> a;
cout << "Enter 2nd number = ";
cin >> b;
cout << a << " + " << b << " = " << a+b << endl;
cout << a << " - " << b << " = " << a-b << endl;
cout << a << " * " << b << " = " << a*b << endl;
cout << a << " / " << b << " = " << a/b << endl;
cout << a << " % " << b << " = " << a%b << endl;
getch();
return 0;
}

2
3. Write a c++ program that takes 3-digit number (example : 147) as input and print :
first digit, second digit, third digit.
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int a;
3
cout << " Enter any three digit number = ";
cin >> a;
int x,y,z,p;
z=a%10;
y=a/10%10;
x=a/10/10;
cout << "1st digit = " << z << endl;
cout << "2nd digit = " << y << endl;
cout << "3rd digit = " << x << endl;
getch();
return 0;

4
4. Use all relational/comparison operators
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int a=20,b=30;
cout << "-------------All Relational operator--------------------" << endl;
cout << " Computer give answer if True = 1 & False = 0" <<endl << endl;
cout << a << " > " << b <<" = " << (a>b) << endl;
cout << a << " < " << b <<" = " << (a<b) << endl;

5
cout << a << " >= " << b <<" = " << (a>=b) << endl;
cout << a << " <= " << b <<" = " << (a<=b) << endl;
cout << a << " == " << b <<" = " << (a==b) << endl;
cout << a << " != " << b <<" = " << (a!=b) << endl;
getch();
return 0;
}

5. Use Logical operator


#include<iostream>
#include<conio.h>
using namespace std;
int main()

6
{
int a=20,b=30, c = 40;
cout << "-------------All Logical operator--------------------" << endl;
cout << " Computer give answer if True = 1 & False = 0" <<endl << endl;
bool r = (a>b) && (b>c);
bool s = (a>b) && (b>c);
cout << r << endl;
cout << s << endl;
cout << ((a>b) || (b<c)) << endl;
cout << (!(a>b));
getch();
return 0;
}

7
6. Use increment & decrement operator
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int a=20;
a++;
cout << " value of a = " << a << endl;
++a;
8
cout << " value of a = " << a << endl;
a= a++ + a++ ;
cout << " value of a = " << a << endl;
--a;
cout << " value of a = " << a << endl;
getch();
return 0;
}

7. Solve any expression by c++ program.


#include<iostream>

9
#include<conio.h>
using namespace std;
int main()
{
int a=40-20*3/5;
cout << " value of a = " << a << endl;
float b = 30/5%4;
cout << " value of b = " << b << endl;
getch();
return 0;

8. Use bit-wise operator

10
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int a=5, b =3;
cout << (a&b) << endl;
cout << (a|b) << endl;
cout << "xor = " << (a ^ b) << endl;
cout << (~a) << endl;
cout << (~b) << endl;
cout << (a << 1) << endl;
cout << (a >> 2) << endl;
getch();
return 0;

11
9. Use Assignment operators
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int a=10;
int b=a;
cout << "a = " << a << endl;
cout << "b = " << b << endl;
b*=a+b;
12
cout << "b = " << b;
getch();
return 0;

10. Use Ternary / Conditional operator


#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int a=20;
13
(a>=18) ? cout << " Eligible for vote" : cout << " Not eligible for vote";
getch();
return 0;
}

11. Find size of data type


#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
cout << " Size of int (in byte) = " << sizeof(int) << endl;
cout << " Size of char (in byte) = " << sizeof(char) << endl;

14
cout << " Size of float (in byte) = " << sizeof(float) << endl;
cout << " Size of long long int (in byte) = " << sizeof(long long int) << endl;
cout << " Size of void (in byte) = " << sizeof(void) << endl; // no size
cout << " Size of bool (in byte) = " << sizeof(bool) << endl;
getch();
return 0;

15
12. Convert ASCII code / Type casting
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int a=65;
cout << "ASCII of 65 is = " << char(a) << endl;
char b='Z';
cout << " ASCII of Z is = " << (int)b;
getch();
return 0;

16
13. Write a C++ program to input a person’s age and determine whether the
person is an adult or not.
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int age;
cout << "Enter your age = ";
cin >> age;
if (age >=18)
17
{
cout << " you are adult" << endl;
}
else
{
cout << " you are not adult" ;
}
getch();
return 0;

18
14. Write a program to input two numbers and print the biggest and smallest both
number;
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int a,b;
cout << " Enter 1st number = ";
cin >> a;
cout << " Enter 2nd number = ";
cin >> b;
cout << endl;
if (a>b)
{
cout << a << " greatest number" << endl;
cout << b << " smallest number" << endl;
}
if (a<b)
{
cout << b << " greatest number" << endl;

19
cout << a << " smallest number" << endl;
}
getch();
return 0;

15. Write a program to input three numbers and print the biggest number ?
#include<iostream>
#include<conio.h>

20
using namespace std;
int main()
{
int a,b,c;
cout << " Enter 1st number = ";
cin >> a;
cout << " Enter 2nd number = ";
cin >> b;
cout << " Enter 3rd number = ";
cin >> c;
cout << endl;
if (a>b && a >c)
{
cout << a << " greatest number" << endl;
}
else if (b>a && b>c)
{
cout << b << " greatest number" << endl;
}
else
{

21
cout << c << " greatest number";
}
getch();
return 0;

16. Write a program to input three numbers and print the smallest number.
#include<iostream>
#include<conio.h>

22
using namespace std;
int main()
{
int a,b,c;
cout << " Enter 1st number = ";
cin >> a;
cout << " Enter 2nd number = ";
cin >> b;
cout << " Enter 3rd number = ";
cin >> c;
cout << endl;
if (a<b && a <c)
{
cout << a << " smallest number" << endl;
}
else if (b<a && b<c)
{
cout << b << " smallest number" << endl;
}
else
{

23
cout << c << " smallest number";
}
getch();
return 0;

17. Write a program to input a number and print whether it is “positive”,


“negative” or “zero”.
#include<iostream>

24
#include<conio.h>
using namespace std;
int main()
{
int a;
cout << " Enter any integer number = ";
cin >> a;

cout << endl;


if (a > 0 )
{
cout << a << " Positive number" << endl;
}
else if ( a < 0)
{
cout << a << " Negative number " << endl;
}
else
{
cout << a << " is zero";
}

25
getch();
return 0;

Following years are leap years.


1988, 1992, 1996, 2000, 2004, 2008, 2012, 2016, 2020, 2024, 2028, 2032, 2036, 2040,
2044, 2048.

26
18. A year that is divisible by 4 is known as leap year (e.g. 1988,1992,1996), but if it
is also divisible by 100 then it is not a leap year (e.g. 1700,1800,1900), but if it is
also divisible by 400 then it is leap year (e.g. 1600,2000,2400).
Write a program to enter a year and print whether it is a leap year or not.
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int a;
cout << " Enter any year = ";
cin >> a;
if (a%4==0)
{
if(a%100==0)
{
if(a%400==0)
{
cout << " it is leap year";
}
else

27
{
cout << " not leap year";
}
}
else
{
cout << " it is leap year";
}
}
else
{
cout << " not leap year";
}
getch();
return 0;

28
19. Write a program to input three numbers and print them in either, ascending or
descending order. Ask the user the way he/she would like the output.
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int a,b,c,ask,big,small,mid;
cout << " Enter 1st number = ";
cin >> a;

29
cout << " Enter 2nd number = ";
cin >> b;
cout << " Enter 3rd number = ";
cin >> c;
cout << endl << " Menu List " << endl;
cout << " for ascending order press 1" << endl;
cout << " for decending order press 2" << endl;
cin >> ask;
if (a>b && a>c)
{
big =a;
}
else if(b>c)
{
big =b;
}
else
{
big =c;
}
if (a<b && a<c)

30
{
small =a;
}
else if(b<c)
{
small =b;
}
else
{
small =c;
}
mid = (a+b+c)-(big+small);
switch (ask)
{
case 1:
{
cout << " Ascending order is = " << big <<"," << mid <<","<< small;
break;
}
case 2:
{

31
cout << " Descending order is = " << small <<"," << mid <<","<< big;
break;
}
}

getch();
return 0;

20. Write a program to enter two operands and an operator (*,-,+,/,%) and display
the calculated result.

32
21. Write a program to enter a two digit number and print whether it is a
palindrome or not. (e.g. 121,1331,1221,….)
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int a,p,q;
cout << " enter a number (only two digit) = ";
cin >> a;
p=a%10; // 1st digit
q= a/10; // 2nd digit
int sum;
sum =p*10+q;
// second logic
// rev=(a%10)*10+(n/10);
if (a==sum)
cout << " palindrome number";
else
cout << " not palindrome number";
getch();

33
return 0;

22. Write a program to enter a three digit number and print whether it is a
palindrome or not.
23. Write a program to enter a four digit number and print whether it is a
palindrome or not.
24. Write a program to enter two three digits number and print if the middle digit
of both the numbers is same.

34

You might also like