In: Computer Science
in C++ programing language
Write a program that prompts the user for an integer, then prints all of the numbers from one to that integer, separated by spaces. Use a loop to print the numbers. But for multiples of three, print "Fizz" instead of the number, and for the multiples of five print "Buzz". For numbers which are multiples of both three and five print "FizzBuzz". Drop to a new line after printing each 20 numbers. If the user typed 100, the output would be
Please upvote :) For any doubt please ask :)
#include<iostream>
using namespace std;
int main()
{
int num,i;
cout<<"Enter a number: "; //Prompts user for a
number
cin>>num; //user enters a number
cout<<endl;
for(i=1;i<=num;i++) //Runs a loop from 1 to
number
{
if(i%20==1) //This prints a new line after printing 20
numbers
cout<<endl;
if(i%15==0) //If number is multiple of 3 and 5 it is
of 15 so we print FizzBuzz
cout<<"FizzBuzz"<<" ";
else if(i%3==0) //For multiple of 3 print FIzz
cout<<"Fizz"<<" ";
else if(i%5==0) //For multiple of 5 print Buzz
cout<<"Buzz"<<" ";
else
cout<<i<<" "; //Otherwise print the
number
}
}