158k views
4 votes
g 1. Write a program that asks the user for a number greater than 5 and prints all values between 1 and n that are multiples of 5 (i.e. evenly divisible by 5). 2. Write a program that prompts the user for an integer n between 1 and 100. If the number is outside the range, it should keep asking the user for a valid number. The program computes and prints at the end two things: a.The sum of the numbers from 1 to n. b.The average of the numbers from 1 to n.

User Kerumen
by
3.3k points

1 Answer

4 votes

PROGRAM 1

#include <iostream>

using namespace std;

int main() {

int n;

// user input taken for n

cout<<"Enter any number greater than five: ";

cin>>n;

cout<<"The multiples of 5 between 1 to "<<n<<" are shown below."<<endl;

// displaying multiples of 5 between 1 to n

for(int h=1; h<=n; h++)

{

// if number is completely divisible by 5, remainder will be 0

if( (h%5)==0)

cout<<h<<endl;

}

return 0;

}

OUTPUT

Enter any number greater than five: 22

The multiples of 5 between 1 to 22 are shown below.

5

10

15

20

The program does not implements any input validation since this is not mentioned in the question.

User input is taken. All the multiples of 5 computed inside a for loop, and displayed to the console.

PROGRAM 2

#include <iostream>

using namespace std;

int main() {

// variables to hold respective values

int n;

int sum=0;

double avg=0;

do

n>100)

cout<<"Invalid number. Enter valid number."<<endl;

cout<<""<<endl;

while(n<1 || n>100);

// computing sum and average of numbers from 1 to n

for(int h=1; h<=n; h++)

{

sum = sum+h;

}

avg=avg+(sum/n);

cout<<"Sum of all the numbers from 1 to "<<n<<" is "<<sum<<endl;

cout<<""<<endl;

cout<<"Average of all the numbers from 1 to "<<n<<" is "<<avg<<endl;

return 0;

}

OUTPUT

Enter any number between 1 and 100: 111

Invalid number. Enter valid number.

Enter any number between 1 and 100: 0

Invalid number. Enter valid number.

Enter any number between 1 and 100: 23

Sum of all the numbers from 1 to 23 is 276

Average of all the numbers from 1 to 23 is 12

The program not implements any input validation since this is mentioned in the question.

User input is taken inside do-while loop till valid input is obtained.

The sum of all numbers from 1 to n computed inside a for loop, average computed outside for loop.

Both values are displayed to the console.

User Leanette
by
3.3k points