Write loops in c code that implement the following Sum the i
Write loops in c++ code that implement the following:
Sum the integers from 0 to 20
A loop that gets an input from the user and validates that user has entered a number between 50 and 75
A loop that starts at 500 and counts down to 0 by 2 on each iteration
A loop that asks the user to input 10 numbers, computes their sum, and reports their average upon exit from the loop
Solution
Answer:
---- Sum of integers from 0 to 20 :
#include<stdio.h>
#include<conio.h>
void main()
{
int j, sum = 0;
for( j=0; j<=20; j++)
{
sum += j;
}
printf(\"Sum = %d\", sum);
}
---> 2nd program - validating user input :
#include <stdio.h>
#include <conio.h>
void main()
{
int usr_input;
clrscr();
scanf(\"%d\",&usr_input);
while( 1 )
{
if(usr_input >= 50 && usr_input <= 75)
{
printf(\"Entery accepted\");
getch();
break;
}
else
{
printf(\"Wrong Entery\");
continue;
}
}
}
---> 3rd - decreaments by 2 each time
void main()
{
for( int I = 500; I >= 0; I++)
{
printf(\"%d\ \",I);
I -= 2;
}
getch();
}
