Write a program that contains a menu which allows a user to choose any of the following functions:
• function that asks the user for a number n and prints the sum of the numbers 1 to n
• function that prints the next 20 leap years.
• function that prints all prime numbers. (Note: if your programming language does not support arbitrary size numbers, printing all primes up to the largest number you can represent is fine too.)
• function that prints a multiplication table for a given number n.
#include <stdio.h>
#include <conio.h>
using namespace std;
int main()
{
cout << "**************************" << endl;
cout << "1. Sum of number 1 to n" << endl;
cout << "2. Print next 20 years" << endl;
cout << "3. All prime numbers" << endl;
cout << "4. Multiplication table" << endl;
cout << "**************************" << endl;
}
int main()
{
int n;
cout << "Enter n Value: ";
cin >> n;
cout << "The Sum Is: " << prime(n);
}
int prime(int n)
{
int num = 0;
bool prime;
for (int i = 2; i <= n; i++)
{
prime = true;
for (int j = 2; j < i; j++)
{
if (i % j == 0)
{
prime = false;
break;
}
}
if (prime)
{
num += i;
}
}
return num;
}
int leapyear(int yr)
{
if ((yr % 4 == 0) && !(yr % 100 == 0))
cout << yr;
else if (yr % 400 == 0) // year divisible by 4 but not by 100
cout << yr;
return yr;
}
int main()
{
const int arraySize = 5; // arraySize must be const
int year[arraySize]; // declare array for input
cout << "Enter " << arraySize << " four digits years:\n";
for (int i = 0; i < arraySize; i++)
cin >> year[i];
cout << leapyear(year) << " is a leap year.\n" << endl;
}
int primeFactors(int n)
{
// Print the number of 2s that divide n
while (n % 2 == 0)
{
printf("%d ", 2);
n = n / 2;
}
// n must be odd at this point. So we can skip one element (Note i = i +2)
for (int i = 3; i <= sqrt(n); i = i + 2)
{
// While i divides n, print i and divide n
while (n%i == 0)
{
printf("%d ", i);
n = n / i;
}
}
// This condition is to handle the case whien n is a prime number
// greater than 2
if (n > 2)
printf("%d ", n);
}