In: Computer Science
write a program that uses exactly four for loops to print the sequence of letters below
A A A A A A A A A A B B B B B B B C C C C C C E E E E
E
Here we want to write a program that uses exactly four for loops to print the given sequence,
A A A A A A A A A A B B B B B B B C C C C C C E E E E E
This sequence contains 10 A's followed by 7 B's followed by 6 C's followed by 5 E's.
C program to print the given sequence with four for loop and explanation
#include <stdio.h> /* Include header file for access standard input output built in functions */
int main() /* Execution of the
program begins here */
{
for(int i=0;i<10;i++) /* First
for loop ,initialize i as integer and check i<10 and update i by
1 for each loop .(this for loop condition true until
i=9*/
{
printf("A "); /* print letter A if the for
loop condition is satisfy */
}
for(int i=0;i<7;i++) /* second for loop
,initialize i as integer and check i<7 and update i by 1 for
each loop .(this for loop condition is true until i=6
*/
{
printf("B ");/* print letter B if the for loop
condition is satisfy */
}
for(int i=0;i<6;i++)/* third for loop
,initialize i as integer and check i<6 and update i by 1 for
each loop .(this for loop condition true until
i=5*/
{
printf("C ");/* print letter C if the for loop
condition is satisfy */
}
for(int i=0;i<5;i++)/* fourth for loop
,initialize i as integer and check i<5 and update i by 1 for
each loop .(this for loop condition true until
i=4*/
{
printf("E ");/* print letter E if the for loop
condition is satisfy */
}
} /* End of main function*/
C++ program to print the given sequence with four for loop
#include <iostream>
using namespace std;
int main()
{
for(int i=0;i<10;i++)
{
cout<<"A ";
}
for(int i=0;i<7;i++)
{
cout<<"B ";
}
for(int i=0;i<6;i++)
{
cout<<"C ";
}
for(int i=0;i<5;i++)
{
cout<<"E ";
}
return 0;
}
Python program to print the given sequence with four for loop
for i in range(10): # for loop executes
from 0-9.that is range(10) will give numbers from 0-9.
print("A",end=" ") # print letter A
,print letters in same line using end equal to space.
for i in range(7): # for loop
executes from 0-6.that is range(7) will give numbers from
0-6.
print("B",end=" ") # print letter B,print
letters in same line using end equal to space.
for i in range(6): # for loop
executes from 0-5.that is range(6) will give numbers from
0-5.
print("C",end=" ") # print letter C ,print
letters in same line using end equal to space.
for i in range(5): # for loop
executes from 0-4.that is range(4) will give numbers from
0-4.
print("E",end=" ") # print letter E ,print
letters in same line using end equal to space.
output