C supports nesting of loops in C. Nesting of loops is the feature in C that allows the looping of statements inside another loop. Let's observe an example of nesting loops in C. Any number of loops can be defined inside another loop, i.e., there is no restriction for defining any number of loops. The nesting level can be defined at n times. You can define any type of loop inside another loop; for example, you can define 'while' loop inside a 'for' loop.
Syntax:
for( initial ; Condition ; increment / decrement ) // Outer Loop Statements
{
for( initial ; Condition ; increment / decrement ) // Inner Loop Statements
{
. . . .
}
}
This program is written in C language and uses nested for loops to print two different patterns of asterisks (*).
The first for loop, with the loop variable "i" starts at 0 and runs for 5 iterations. Within this for loop, there is another for loop, with the loop variable "j", which also starts at 0 and runs for 5 iterations. The inner for loop prints an asterisk (*) on each iteration, so it will print 5 asterisks in total during each iteration of the outer for loop. This creates a pattern of 5 rows of 5 asterisks each.
Then a line of separator is added to distinguish the two different patterns
The second set of nested loops starts with i = 1 and runs for 5 iterations. Within this for loop, there is another for loop, with the loop variable "j" which starts at 1 and runs for i iterations. The inner for loop prints an asterisk (*) on each iteration, so it will print i asterisks in total during each iteration of the outer for loop. This creates a pattern of 5 rows of asterisks, with the first row having 1 asterisk, the second row having 2 asterisks, and so on, up to the fifth row which has 5 asterisks.
In the end, the main function returns 0 to indicate successful execution of the program.
//Nested For Loop /* ***** ***** ***** ***** ***** * ** *** **** ***** */ #include<stdio.h> int main() { int i,j; for(i=0;i<5;i++) { for(j=0;j<5;j++) { printf("*"); } printf("\n"); } printf("\n----------------------------\n"); for(i=1;i<=5;i++) { for(j=1;j<=i;j++) { printf("*"); } printf("\n"); } return 0; }To download raw file Click Here
* * * * * * * * * * * * * * * * * * * * * * * * * ---------------------------- * * * * * * * * * * * * * * *
Write a program to find the factor of the given number.
Write a program to find whether the given number is prime or not.
Write a program to find the given number is Armstrong number or not.
Example: 153 1³ = 1 5³ = 125 3³ = 27 1 + 125 + 27 = 153
Write a program to print the Armstrong numbers between 1 to 10000.
Write a program to count and print the number of odd and even numbers.
Example: [2, 85, 6, 77, 5] Enter the limit: 5 Enter the value: 10 Enter the value: 25 Enter the value: 65 Enter the value: 12 Enter the value: 45 Even: 2 Odd: 3
Write a program to find the reverse of an n-digit number using a while loop.
Clue: Use % and // operators.
Write a program to find the binary number from a given decimal number.
Write a program to find the decimal number from a given binary number.
Write a program to find the factorial of the given number.
Enter the number: 5 Factorial of 5 is: 120
Write a program to find the Fibonacci series of the given number.
Enter the limit: 8 0 1 1 2 3 5 8 13
Write a program to find whether the given number is a perfect number.
Example: 6 Factors: 1, 2, 3 1 + 2 + 3 = 6
Write a program to print the perfect numbers between 1 and 1000.
Write a program to find whether the given number is a strong number.
Example: 145 Digits: 1 4 5 Factorials: 1! = 1 4! = 24 5! = 120 Sum: 1 + 24 + 120 = 145
145 is a Strong Number
Two numbers are entered through the keyboard. Write a program to find the value of one number raised to the power of another.
Example: Enter the Base: 2 Enter the Power: 3 Clue: 2 × 2 × 2 = 8 Result: 8
Learn All in Tamil © Designed & Developed By Tutor Joes | Privacy Policy | Terms & Conditions