Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 10

C++++++++++++++++++++++++++++++++++++++++

1. Example: Print Number Entered by User


2. #include <iostream>
3. using namespace std;
4.
5. int main()
6. {
7. int number;
8.
9. cout << "Enter an integer: ";
10. cin >> number;
11.
12. cout << "You entered " << number;
13. return 0;
14. }

Output

Enter an integer: 23
You entered 23

2. Example: Display all Factors of a Number


1. #include <iostream>
2. using namespace std;
3.
4. int main()
5. {
6. int n, i;
7.
8. cout << "Enter a positive integer: ";
9. cin >> n;
10.
11. cout << "Factors of " << n << " are: " << endl;
12. for(i = 1; i <= n; ++i)
13. {
14. if(n % i == 0)
15. cout << i << endl;
16. }
17.
18. return 0;
19. }
Output
Enter a positive integer: 60
Factors of 60 are: 1 2 3 4 5 6 12 15 20 30 60
3. Example 1: Check Whether Number is Even or
Odd using if else
1. #include <iostream>
2. using namespace std;
3.
4. int main()
5. {
6. int n;
7.
8. cout << "Enter an integer: ";
9. cin >> n;
10.
11. if ( n % 2 == 0)
12. cout << n << " is even.";
13. else
14. cout << n << " is odd.";
15.
16. return 0;
17. }
Output
Enter an integer: 23
23 is odd.

In this program, if..else statement is used to check whether n%2 == 0 is true or not. If this
expression is true, n is even if not n is odd.
You can also use ternary operators ?: instead of if..else statement. Ternary operator is
short hand notation of if...else statement.

A positive integer is called an Armstrong number if the sum of cubes of individual digit is
equal to that number itself. For example:

153 = 1 * 1 * 1 + 5 * 5 * 5 + 3 * 3 * 3 // 153 is an Armstrong number.

12 is not equal to 1 * 1 * 1 + 2 * 2 * 2 // 12 is not an Armstrong number.

4. Example: Check Armstrong Number


1. #include <iostream>
2. using namespace std;
3.
4. int main()
5. {
6. int origNum, num, rem, sum = 0;
7. cout << "Enter a positive integer: ";
8. cin >> origNum;
9.
10. num = origNum;
11.
12. while(num != 0)
13. {
14. rem = num % 10;
15. sum += rem * rem * rem;
16. num /= 10;
17. }
18.
19. if(sum == origNum)
20. cout << origNum << " is an Armstrong number.";
21. else
22. cout << origNum << " is not an Armstrong number.";
23.
24. return 0;
25. }
Output
Enter a positive integer: 371
371 is an Armstrong number.

In the above program, a positive integer is asked to enter by the user which is stored in
the variable origNum.
Then, the number is copied to another variable num. This is done because we need to
check the origNum at the end.
Inside the while loop, last digit is separated from num by the operation digit = num %
10;. This digit is cubed and added to the variable sum.

This program takes an integer from user and that integer is reversed.

If the reversed integer is equal to the integer entered by user then, that number is a
palindrome if not that number is not a palindrome.

5. Example: Check Palindrome Number


1. #include <iostream>
2. using namespace std;
3.
4. int main()
5. {
6. int n, num, digit, rev = 0;
7.
8. cout << "Enter a positive number: ";
9. cin >> num;
10.
11. n = num;
12.
13. do
14. {
15. digit = num % 10;
16. rev = (rev * 10) + digit;
17. num = num / 10;
18. } while (num != 0);
19.
20. cout << " The reverse of the number is: " << rev << endl;
21.
22. if (n == rev)
23. cout << " The number is a palindrome.";
24. else
25. cout << " The number is not a palindrome.";
26.
27. return 0;
28. }
Output
Enter a positive number: 12321
The reverse of the number is: 12321
The number is a palindrome.

Enter a positive number: 12331


The reverse of the number is: 13321
The number is not a palindrome.

In the above program, use is asked to enter a positive number which is stored in the
variable num.
The number is then saved into another variable n to check it when the original number
has been reversed.
Inside the do...while loop, last digit of the number is separated using the code digit =
num % 10;. This digit is then added to the rev variable.

6. Example: C++ Program to Reverse an Integer


1. #include <iostream>
2. using namespace std;
3.
4. int main()
5. {
6. int n, reversedNumber = 0, remainder;
7.
8. cout << "Enter an integer: ";
9. cin >> n;
10.
11. while(n != 0)
12. {
13. remainder = n%10;
14. reversedNumber = reversedNumber*10 + remainder;
15. n /= 10;
16. }
17.
18. cout << "Reversed Number = " << reversedNumber;
19.
20. return 0;
21. }
Output
Enter an integer: 12345
Reversed number = 54321

This program takes an integer input from the user and stores it in variable n.
Then the while loop is iterated until n != 0 is false.

7. Example: Compute quotient and remainder


1. #include <iostream>
2. using namespace std;
3.
4. int main()
5. {
6. int divisor, dividend, quotient, remainder;
7.
8. cout << "Enter dividend: ";
9. cin >> dividend;
10.
11. cout << "Enter divisor: ";
12. cin >> divisor;
13.
14. quotient = dividend / divisor;
15. remainder = dividend % divisor;
16.
17. cout << "Quotient = " << quotient << endl;
18. cout << "Remainder = " << remainder;
19.
20. return 0;
21. }

Output

Enter dividend: 13
Enter divisor: 4
Quotient = 3
Remainder = 1
8.Example 1: C++ Program to convert binary
number to decimal
1. #include <iostream>
2. #include <cmath>
3.
4. using namespace std;
5.
6. int convertBinaryToDecimal(long long);
7.
8. int main()
9. {
10. long long n;
11.
12. cout << "Enter a binary number: ";
13. cin >> n;
14.
15. cout << n << " in binary = " << convertBinaryToDecimal(n) << "in decimal";
16. return 0;
17. }
18.
19. int convertBinaryToDecimal(long long n)
20. {
21. int decimalNumber = 0, i = 0, remainder;
22. while (n!=0)
23. {
24. remainder = n%10;
25. n /= 10;
26. decimalNumber += remainder*pow(2,i);
27. ++i;
28. }
29. return decimalNumber;
30. }
Output
Enter a binary number: 1111
1111 in binary = 15

9. Example 1: Program to Convert Binary to Octal


In this program, we will first convert the binary number to decimal. Then, the decimal
number is converted to octal.

1. #include <iostream>
2. #include <cmath>
3.
4. using namespace std;
5.
6. int convertBinarytoOctal(long long);
7. int main()
8. {
9. long long binaryNumber;
10.
11. cout << "Enter a binary number: ";
12. cin >> binaryNumber;
13.
14. cout << binaryNumber << " in binary = " <<
convertBinarytoOctal(binaryNumber) << " in octal ";
15.
16. return 0;
17. }
18.
19. int convertBinarytoOctal(long long binaryNumber)
20. {
21. int octalNumber = 0, decimalNumber = 0, i = 0;
22.
23. while(binaryNumber != 0)
24. {
25. decimalNumber += (binaryNumber%10) * pow(2,i);
26. ++i;
27. binaryNumber/=10;
28. }
29.
30. i = 1;
31.
32. while (decimalNumber != 0)
33. {
34. octalNumber += (decimalNumber % 8) * i;
35. decimalNumber /= 8;
36. i *= 10;
37. }
38.
39. return octalNumber;
40. }
Output
Enter a binary number: 10001
10001 in binary = 21 in octal

The binary number entered by the user is passed to convertBinaryToOctal() function.


And, this function converts the number to octal and returns to the main() function

In this program, the octal number is converted to decimal at first. Then, the decimal
number is converted to binary number.

1. #include <iostream>
2. #include <cmath>
3.
4. using namespace std;
5.
6. long long convertOctalToBinary(int);
7. int main()
8. {
9. int octalNumber;
10.
11. cout << "Enter an octal number: ";
12. cin >> octalNumber;
13.
14. cout << octalNumber << " in octal = " << convertOctalToBinary(octalNumber)
<< "in binary";
15.
16. return 0;
17. }
18.
19. long long convertOctalToBinary(int octalNumber)
20. {
21. int decimalNumber = 0, i = 0;
22. long long binaryNumber = 0;
23.
24. while(octalNumber != 0)
25. {
26. decimalNumber += (octalNumber%10) * pow(8,i);
27. ++i;
28. octalNumber/=10;
29. }
30.
31. i = 1;
32.
33. while (decimalNumber != 0)
34. {
35. binaryNumber += (decimalNumber % 2) * i;
36. decimalNumber /= 2;
37. i *= 10;
38. }
39.
40. return binaryNumber;
41. }
Output
Enter an octal number: 54
54 in octal = 101100

The octal number entered by the user is passed to convertOctalToBinary() function.


And, this function converts the number to binary and returns the main() function

10.Example: Calculate Factorial Using Recursion


1. #include<iostream>
2. using namespace std;
3.
4. int factorial(int n);
5.
6. int main()
7. {
8. int n;
9.
10. cout << "Enter a positive integer: ";
11. cin >> n;
12.
13. cout << "Factorial of " << n << " = " << factorial(n);
14.
15. return 0;
16. }
17.
18. int factorial(int n)
19. {
20. if(n > 1)
21. return n * factorial(n - 1);
22. else
23. return 1;
24. }
Output
Enter an positive integer: 6
Factorial of 6 = 720

In the above program, suppose the user inputs a number 6. The number is passed to
the factorial() function.
In this function, 6 is multiplied to the factorial of (6 - 1 = 5). For this, the number 5 is
passed again to the factorial() function.
Likewise in the next iteration, 5 is multiplied to the factorial of (5 - 1 = 4). And, 4 is
passed to the factorial() function.

This continues until the value reaches 1 and the function returns 1.

Now, each function returns the value back to compute 1 * 2 * 3 * 4 * 5 * 6 = 720, which
is returned to the main() function.

11. Example: Sum of Natural Numbers using loop


1. #include <iostream>
2. using namespace std;
3.
4. int main()
5. {
6. int n, sum = 0;
7.
8. cout << "Enter a positive integer: ";
9. cin >> n;
10.
11. for (int i = 1; i <= n; ++i) {
12. sum += i;
13. }
14.
15. cout << "Sum = " << sum;
16. return 0;
17. }

Output

Enter a positive integer: 50


Sum = 1275

This program assumes that user always enters positive number.

If user enters negative number, Sum = 0 is displayed and program is terminated.

You might also like