asked 153k views
0 votes
Write a Visual Logic program that prompts the user for a whole number, N, that is less than or equal 50

and greater than 0. If the user enters an invalid number end the program. Output the summation of the
odd numbers from 1 to N (if N is odd or N-1 if N is even. For example, if the user enters 15, the output
should be 64 (1+3+5+7+9+11+13+15.) If the user entered 12, the output should be 36 (1+3+5+7+9+11.)

asked
User Shlensky
by
8.1k points

1 Answer

5 votes

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;

}

Step-by-step 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

answered
User Nafi
by
8.4k points
Welcome to Qamnty — a place to ask, share, and grow together. Join our community and get real answers from real people.