You are on page 1of 2

1. #include <iostream.

h>
2. #include <conio.h>
3.
4. void main()
5. {
6. int number, i = 1, factorial = 1;
7.
8. cout << "Enter a positive integer: ";
9. cin >> number;
10.
11. while ( i <= number)
12. {
13. factorial = factorial * i;
14. i++;
15. }
16.
17. cout<<"Factorial of "<< number <<" = "<< factorial;
18. getch();
19. }

Output
Enter a positive integer: 4
Factorial of 4 = 24

Example 1: Display Multiplication table of any


number
1. #include <iostream.h>
2. #include <conio.h>
3. void main()
4. {
5. int n;
6.
7. cout << "Enter a positive integer: ";
8. cin >> n;
9.
10. for (int i = 1; i <= 10; i++)
11. {
12. cout << n << " * " << i << " = " << n * i << endl;
13. }
14.
15. getch();
16. }
Example: C++ switch Statement
1. // Program to built a simple calculator using switch Statement
2.
3. #include <iostream.h>
4. #include <conio.h>
5.
6. void main()
7. {
8. char o;
9. float num1, num2;
10.
11. cout << "Enter an operator (+, -, *, /): ";
12. cin >> o;
13.
14. cout << "Enter two operands: ";
15. cin >> num1 >> num2;
16.
17. switch (o)
18. {
19. case '+':
20. cout << num1 << " + " << num2 << " = " << num1+num2;
21. break;
22. case '-':
23. cout << num1 << " - " << num2 << " = " << num1-num2;
24. break;
25. case '*':
26. cout << num1 << " * " << num2 << " = " << num1*num2;
27. break;
28. case '/':
29. cout << num1 << " / " << num2 << " = " << num1/num2;
30. break;
31. default:
32. // operator is doesn't match any case constant (+, -, *, /)
33. cout << "Error! operator is not correct";
34. break;
35. }
36.
37. getch();
38. }

You might also like