Question

In: Computer Science

Set up a for loop to print values from n down to 1 with at most...

  1. Set up a for loop to print values from n down to 1 with at most 5 values per line. For example, when n is 7 then the output is shown below:

7    6    5    4    3
2    1

C++ code

Solutions

Expert Solution

C++ code:

#include <iostream>
using namespace std;
int main(){
//initializing count and giving a sample value for n
int count=0,n=7;
//looping from n to 1
for(int i=n;i>=1;i--){
//incrementing count
count++;
//checking if count is not 5
if(count!=5)
//printing current number i
cout<<i<<" ";
else{
//initializing count as 0
count=0;
//printing i and ending line
cout<<i<<endl;
}
}
return 0;
}


Screenshot:


Output:


Related Solutions

(C++) Write a nested loop that reads an integer n (n>0) from the keyboard and print...
(C++) Write a nested loop that reads an integer n (n>0) from the keyboard and print out "n" lines as follows: 0 0 1 0 1 2 0 1 2 3 … 0 1 2 3 … n-1
JAVA Write a for loop which will display this set of values 1,3,5,7,9.
JAVA Write a for loop which will display this set of values 1,3,5,7,9.
def annoying_valley(n): if n == 0: print() elif n == 1: print("*") elif n == 2:...
def annoying_valley(n): if n == 0: print() elif n == 1: print("*") elif n == 2: print("./") print("*") print(".\\") elif n == 3: print("../") print("./") print("*") print(".\\") print("..\\") elif n == 4: print(".../") annoying_valley(3) print("...\\") elif n == 5: print("..../") annoying_valley(4) print("....\\") elif n == 6: print("...../") annoying_valley(5) print(".....\\") else: print("." * (n - 1) + "/") annoying_valley(n - 1) print("." * (n - 1) + "\\") def annoying_int_sequence(n): if n == 0: return [] elif n == 1: return...
A rectangular loop is made by a metal bar that slides down a set of parallel...
A rectangular loop is made by a metal bar that slides down a set of parallel rails of negligible resistance in a vertical gravitational field g. The rails are connected together by a third rail of negligible resistance The resulting loop is in a uniform, vertical magnetic field B. First assume there is no friction between the bar and the rails. (a) Does the bar acquire a constant velocity and if so what is it? (b) Now take the coefficient...
def seq3np1(n): """ Print the 3n+1 sequence from n, terminating when it reaches 1. args: n...
def seq3np1(n): """ Print the 3n+1 sequence from n, terminating when it reaches 1. args: n (int) starting value for 3n+1 sequence return: None """ while(n != 1): print(n) if(n % 2) == 0: # n is even n = n // 2 else: # n is odd n = n * 3 + 1 print(n) # the last print is 1 def main(): seq3np1(3) main() Using the provided code, alter the function as follows: First, delete the print statements...
Using a “for loop” print all even numbers in range from 1 to 1000. Also, please...
Using a “for loop” print all even numbers in range from 1 to 1000. Also, please count and print how many even numbers you have found.
1.) How many relations are there from a set of size n to a set of...
1.) How many relations are there from a set of size n to a set of size m? 2.) Determine the number of entries in the following sequences: a.) {13, 19, 25, . . . , 601} b. {7, 11, 19, 35, 67, . . . , 131075}
1) If the electron drops down from the n=5 level to n=2, and then to the...
1) If the electron drops down from the n=5 level to n=2, and then to the ground state of hydrogen, what colors (wavelengths) of light will be emitted? 2) What color of light is this? 3) Photon color for n=2 to ground 4) What color of light is this?
For a Linux server: • how to Set up a print server (CUPS), allow clients to...
For a Linux server: • how to Set up a print server (CUPS), allow clients to connect, print to PDF • Create a script that backs up the printed documents every 6 hours please can you help me
Print out a table of powers. Use a for loop that goes from 101 to 112...
Print out a table of powers. Use a for loop that goes from 101 to 112 inclusive. Print out r, r squared, r cubed, square root of r, and cube root of r. Use two decimal places for the roots, and zero decimal places for the squares and cubes, and 2 decimal places for the averages. Use commas in numbers 1000 and above. After the loop, print out the average of the r squared and r cubed. Use columns for...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT