Question

In: Computer Science

write a program that uses exactly four for loops to print the sequence of letters below...

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

Solutions

Expert Solution

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


Related Solutions

Write a program that uses nested for loops to print a multiplication table. Your program should...
Write a program that uses nested for loops to print a multiplication table. Your program should print out the multiplication table starting a 1 and going to 12. Output should be like: 1 x 1 = .1 x 2 = 2
Write a Java program that uses nested for loops to print a multiplication table as shown...
Write a Java program that uses nested for loops to print a multiplication table as shown below.   Make sure to include the table headings and separators as shown.   The values in the body of the table should be computed using the values in the heading   e.g. row 1 column 3 is 1 times 3.
Write a Java program that accepts a sequence of commands and print out the number of...
Write a Java program that accepts a sequence of commands and print out the number of successful insertions, the number of successful deletions, the number of successful searches (through the “find” command), the number of items remaining in the list after executing all commands and the final contents of the list. Three commands that will be taken as inputs are “insert”, “delete” and “find”. Input Line 1: The number of transactions m on the list, where 1  m 200. Line 2...
Write a program that uses a for loop to print One of the months of the...
Write a program that uses a for loop to print One of the months of the year is January One of the months of the year is February ...
Write a program in java which is in main and uses no classes, methods, loops or...
Write a program in java which is in main and uses no classes, methods, loops or if statements Ask the user to enter their first name Ask the user to enter their last name Print their initials followed by periods (ie. Clark Kent = C. K.) Print the number of letters in their last name
C++. Write a program that uses for loops to perform the following steps: a. Prompt the...
C++. Write a program that uses for loops to perform the following steps: a. Prompt the user to input two positive integers. variables: firstNum and secondNum (firstNum must be less than secondNum). Validate the user's input; prompt the user again if firstNum is not less than secondNum (use for loop). b. Output all odd numbers between firstNum and secondNum. (use for loop). c. Output the sum of all even numbers between firstNum and secondNum. (use for loop). d. Output the...
1. Write a program that read a sequence of positive integer inputs and print the sum...
1. Write a program that read a sequence of positive integer inputs and print the sum and average of the inputs. Assume that the user always enters a positive number (integer) as an input value or an empty space as a sentinel value. Please use the below to test your code [45 points]: Enter an int value or empty string to end: 98 Enter an int value or empty string to end: 78 Enter an int value or empty string...
IN C++ Write a program that uses nested loops to collect data and calculate the average...
IN C++ Write a program that uses nested loops to collect data and calculate the average rainfall over a period of years. The program should first ask the user for the number of years. The outer loop will iterate once for each year. The inner loop will iterate 12 times, once for each month. Each iteration of the inner loop will ask the user for the inches of rainfall for that month. After all iterations, the ­­program should display the...
Write a program that uses loops (both the for-loop and the while loop). This assignment also...
Write a program that uses loops (both the for-loop and the while loop). This assignment also uses a simple array as a collection data structure (give you some exposure to the concept of data structure and the knowledge of array in Java). In this assignment, you are asked to construct an application that will store and retrieve data. The sequence of data retrieval relative to the input is Last In First Out (LIFO). Obviously, the best data structure that can...
Write a C++ program that uses nested loops to collect data and calculate the average rainfall...
Write a C++ program that uses nested loops to collect data and calculate the average rainfall over a period of years. The program should first ask the user for the number of years. The outer loop will iterate once for each year. The inner loop will iterate 12 times, once for each month. Each iteration of the inner loop will ask the user for the inches of rainfall for that month. After all iterations, the program should display the number...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT