Answer:
//program in C++(Visual studio).
#include <bits/stdc++.h>
using namespace std;
// main function
int main()
{
// variable
int num;
cout<<"Enter a number greater than 1 and less than 51:";
// read input number
cin>>num;
// check input is in between 1-50
if(num<=1||num>50)
{
cout<<"Invalid input!!"<<endl;
// exit the program
exit;
}
else
{
// sum variable
int sum=0;
// find sum of all odd numbers
for(int a=1;a<=num;a++)
{
if(a%2!=0)
sum+=a;
}
// print sum
cout<<"Sum of odd numbers from 1 to "<<num<<" is:"<<sum<<endl;
}
return 0;
}
Explanation:
Read a number from user and assign it to variable "num".If input number
is less than 1 or greater than 50 exit the program.otherwise find the sum
of all odd numbers from 1 to "num".Print the sum of odd numbers.
Output:
Enter a number greater than 1 and less than 51:-5
Invalid input!!
Enter a number greater than 1 and less than 51:55
Invalid input!!
Enter a number greater than 1 and less than 51:15
Sum of odd numbers from 1 to 15 is:64