Find the Largest Number and Calculate Factorial in C++

Classified in Computers

Written on in English with a size of 2.11 KB

Input three numbers and find the largest. Enter a number and display the day name of the week.
#include
#include
using namespace std;
using namespace std;
int main() {
int num1, num2, num3;
int day;
cout << "Enter three numbers: ";
cout << "Enter a number (From 1 to 7): ";
cin >> num1 >> num2 >> num3;
cin >> day;
if (num1 > num2 && num1 > num3) {
cout << "Largest number is " << num1;
} else if (num2 > num3) {
cout << "Largest number is " << num2;
} else {
cout << "Largest number is " << num3;
}
switch (day) {
case 1: cout << "Sunday"; break;
case 2: cout << "Monday"; break;
case 3: cout << "Tuesday"; break;
case 4: cout << "Wednesday"; break;
case 5: cout << "Thursday"; break;
case 6: cout << "Friday"; break;
case 7: cout << "Saturday"; break;
default: cout << "Invalid choice";
}
return 0;
}

____________________________________________

Sum of the digits of an integer number
#include
using namespace std;
int main() {
int num, sum = 0, rem;
cout << "Enter a number: ";
cin >> num;
while (num > 0) {
rem = num % 10;
sum = sum + rem;
num = num / 10;
}
cout << "Sum of digits = " << sum;
return 0;
}

___________________________________________

Find the factorial of a given number
#include
using namespace std;
int main() {
int num, i;
float fact = 1;
cout << "Enter an integer number: ";
cin >> num;
for (i = 1; i <= num; i++) {
fact = fact * i;
}
cout << "Factorial of given number = " << fact;
return 0;
}

Related entries: