NESTED LOOPS - EXAMPLES

Before solving tasks from this area, you can read the article Nested loops in C/C++

1. Matrix

Print a sequence of the first 60 even natural numbers, so that they are placed in 3 columns and the required number of rows.

2. Multiplication table

Write a program that prints the corresponding numbers for the entered positive integer value n. Print the multiplication table from 1 to n
#include <stdio.h>
using namespacestd;
int main()
{
/*Declaration of variables*/
int n,i,j;
/*Entering variables*/
printf("Enter the number n");
scanf("%d",&n);
/*Print the table of multiplication of dimensions n*n*/
for(i=1;i<=n;i++){
for(j=1;j<=n;j++)
{
/*The value of each number is the product of the type and the column*/
printf("%d * %d = %3d\t",i,j,i*j);
}
/*A new line is inserted at the end of each output*/
printf("\n");
}
return 0;
}

3. Star triangle

Write a program that loads a positive number n and then draws a right triangle made up of stars. The leg of the triangle is of length n, and the right angle is in the upper right corner of the picture​. Nested Loops-Star Triangle example execution
Figure 1: Nested Loops-Star Triangle example execution
#include <stdio.h>
using namespacestd;
int main()
{
/*Declaration of variables*/
int n,i,j;
/*Entering variables*/
printf("Enter the number n");
scanf("%d",&n);
/*Drawing the required triangle*/
for(i=1;i<=n;i++){
/*Print the whites that precede the stars */
for(j=0;j<=i;j++)
{
/*The value of each number is the product of the type and the column*/
printf(" ");
}
/*Printing the stars*/
for(j=0;j<=n-i;j++)
{
printf("*");
}
/*A new line is inserted at the end of each output*/
printf("\n");
}
return 0;
}

4. Removing numbers from the end and beginning of the array

Write a program that loads a positive integer n, and prints numbers from 1 to n in the first line, then from 2 to n-1 in the second, from 3 to n-3 in the third, etc. Printing ends when it is not possible to print a single number.

Example 1
Input
5
Output
1 2 3 4 5 2 3 4 3

Example 2
Input
6
Output
1 2 3 4 5 6 2 3 4 5 3 4
#include <stdio.h>
using namespacestd;
int main()
{
/*Declaration of variables*/
int n,i,j;
/*Entering variable values*/
printf("Unesite broj n");
scanf("%d",&n);
/* The outer loop changes the series of numbers to be printed * If, for example, n=6, * the first series would be 1 2 3 4 5 6, * second: 2,3,4,5 * and finally the third: 3.4 * It can be concluded that the number of series is twice less than n, more precisely (n+1)/2 */
for(i=1;i<=(n+1)/2;i++){
/*In each series, the numbers from i to n-i+1 are printed*/
for(j=0;j<=i;j++)
{
printf("%d ",j);
}
/*A new line is inserted at the end of each output*/
printf("\n");
}
return 0;
}

5. Variations of triplets


​Each of the 3 friends has a certain number of coins, but none of them have the same number of them. If you know the maximum possible number of coins that each friend can have, write a program that prints all the possible triples of coin numbers that they can have.
Input
From the standard input, the number n (1≤n≤20) is entered - the largest number of coins that each of the friends can have.

Output
Print to standard output all possible numbers of coins that comrades can have, arranged lexicographically.
​
Example:
Input:
​2
Output
0 1 2
0 2 1
1 0 2
1 2 0
2 0 1
2 1 0