It 103-Computer Programming 2 Topic: Looping Construct: A Program That Prints All Even Numbers From 50 To 1. Answer
It 103-Computer Programming 2 Topic: Looping Construct: A Program That Prints All Even Numbers From 50 To 1. Answer
It 103-Computer Programming 2 Topic: Looping Construct: A Program That Prints All Even Numbers From 50 To 1. Answer
Direction: Make a program that will solve the given problems below using C++
programming language. You may use any type of looping statement – while, do while or
for. Choose at least two programs to be presented during our online class on Wednesday,
May 19, 2021.
1. A program declares an Int variable named evenNum and initializes it to 2. Write the C ++
code to display the even integers 2, 4, 6, 8, and 10 on separate lines on the computer
screen.
2. Write the C ++ code to display the integers 15, 12, 9, 6, 3, and 0 on separate lines on the
computer screen.
3. A program that prints all even numbers from 50 to 1.
Answer:
#include <stdio.h>
Int main() {
Int i;
Printf(“Even numbers from 50 to 1 :\n”);
For (i = 50; i >= 1; i --)
{
If(i%2 == 0)
{
Printf(“%d “, i);
}
}
Return 0;
}
4. A program that prints numbers in reverse order from 100 to 50 with an interval of
5.
Answer:
#include <stdio.h>
int main() {
int i;
printf("Even numbers 100 to 50:\n");
for (i = 100; i >= 50; i--)
{
if(i%5 == 0)
{
printf("%d ", i);
}
}
return 0;
}
5. A program to calculate the sum of all numbers from 1 to 20. The output should be
the total of all numbers from 1 to 20
(1+2+3+4+5+6+7+8+9+10+11+12+13+14+15+16+17+18+19+20).
Answer:
#include
using namespace std;
int main(){
int n, sum=0;
cout<<"Enter the value of n(positive integer): "; //Storing the input integer value i sa
atong variable n
cin>>n; /* If negative number imo ibutang sa value sa n then modisplay syag * error
message nga ang value of n is invalid */
if(n<=0)
{
cout<<"Invalid value of n";
}
else
{ // using for loop to calculate the sum
for(int i = 1; i <= n; i++)
{
sum = sum+i;
}
cout<<"Total of all numbers from 1 to n is: "<<sum;
}
return 0;
}
#include <iostream>
int main ( )
int guess;
do
cin>> guess;
cout<<”Too low!”;
return 0;
Answer:
Prepared by:
MELIZA J. CHATTO
Instructor