Please write a program that will asks to give an integer number K that will be higher than 20.
If the number is not higher than 20 then make the program to show a relevant message and then ask to insert again the number K.
After that, depending on the users choice:
If he/she presses 1, it will be shown the sum of numbers from 1 to K.
If the number is 2, it will be shown the odd numbers from 1 to K.
If the number is 3, the program will end.
If any other numbers are inserted then make your code to show a warning message (that the user must insert numbers from 1 to 3) and then the program must do again the process of inserting the number 1-3.
#include <cstdlib>
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
int k;
cout<<"\n Insert a number greater than 20. ";
cin>>k;
cout<<"\n The number is "<<k<<" ."<<endl<<endl;
while ( k<=20 ) {
cout<<"It must be greater than 20. Give again the number. ";
cin>>k;
}
int num, sum, odd;
cout<<"\n Now,give a number from 1 to 3. ";
cin>>num;
cout<<endl;
switch (num)
{ case 1: {sum = 0;
for ( int i=num ; i<=k ; i++)
sum+=i;
cout<<"The sum from "<<num<<" to "<<k<<" is: "<<sum<<endl;
}
break;
case 2: { int i = 1;
cout<<"\n The odd numbers from 1 to "<<k<<" are:\n";
for ( int i=1 ; i<=k ; i=i+2 )
cout<<i<<" ";
cout<<"\n";
odd=i++;
}
break;
case 3:
break;
default: {cout<<"The number must be 1, 2 or 3.\n";
cout<<"\n Give again the number ";
cin>>num; cout<<"\n";
}
break;
}
cout<<endl;
system("PAUSE");
return EXIT_SUCCESS;
}