Professional Documents
Culture Documents
Example: Print Number Entered by User: Output
Example: Print Number Entered by User: Output
Output
Enter an integer: 23
You entered 23
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:
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.
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.
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.
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
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
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
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.
Output