Write a program that prints, line by line, the numbers from 1 to 100. For multiples of three print the word "Cow" instead of the number and for the multiples of seven print "Pie" instead of the number. For numbers which are multiples of both three and seven print "CowPie''

Answer :

MrRoyal

Answer:

Condition 1: Print whole numbers from 1 to 100 line by line

Condition 2: All multiples of 3 should be printed as Cow

Condition 3: All multiples of 7 should be printed as Pie

Condition 4: All multiples of 3 and 7 should be printed as CowPie

//Program starts here.

// Program is written in C++

// Comments are used for explanatory purpose

#include <iostream>

using namespace std;

int main()

{

// Iterate a counter from 1 to 100

for(int i = 1; i<=100; i++)

{

//Test for Multiples of 3

if(i%3 == 0 && i%7 != 0)

{

cout<<"Cow"<<endl;

}

//Test for Multiples of 7

else f(i%3 != 0 && i%7 == 0)

{

cout<<"Pie"<<endl;

}

// Test for Multiples of 3 and 7

else if(i%3 == 0 && i%7 == 0)

{

cout<<"CowPie"<<endl;

}

//Other numbers

else

{

cout<<i<<endl;

}

}

return 0;

}

Other Questions