B. Write a C++ program to print the summation of even numbers from 1 to 20 and skip the summation of 4, 8 and 12 using for and switch statement.
#include <iostream>
using namespace std;
int main()
{ int i, sum=0;
cout<<"Sum of even number from 1 to 20 after skipping the 4, 8 snd 12";
for(i=0; i<=20; i++) // for loop will run until i is less than or equal to 20
{
if(i%2==0) // Considering only even numbers
{
switch(i) // There is choice that if i is equal to 4, 8 and 12 then skip them
{
case 4:
break; // Skipping 4 and loop will move further
case 8:
break; // Skipping 8 and loop will move further
case 12:
break; // Skipping 12 and loop will move further
default:
sum = sum + i; // Summation of numbers
}
}
}
cout<<"\nSum is : "<<sum; // Printing sum
return 0;
}
Step by step
Solved in 2 steps with 1 images