Example 2: for loop // Program to calculate the sum of first n natural numbers // Positive integers 1,2,3...n are known as natural numbers #include <stdio.h> int main() { int num, count, sum = 0; printf("Enter a positive integer: "); scanf("%d", &num); // for loop term...
In the above example we have a for loop inside another for loop, this is called nesting of loops. One of the example where we use nested for loop isTwo dimensional array. Multiple initialization inside for Loop in C We can have multiple initialization in the for loop as shown below. for...
Example 1: C# for Loop using System; namespace Loop { class ForLoop { public static void Main(string[] args) { for (int i=1; i<=5; i++) { Console.WriteLine("C# For Loop: Iteration {0}", i); } } } } When we run the program, the output will be: C# For Loop: Iteratio...
Finally, the main() function returns 0 to indicate that the program has been executed successfully.Example 2: To Generate & Print A Fibonacci Series Using For Loop In C++In this example, we’ll generate a fibonacci series using a for loop in C++ to calculate each term sequentially....
C while Loop: Example 1 Input two integers and find their average using while loop. #include<stdio.h>intmain(){inta,b,avg,count;count=1;while(count<=3){printf("Enter values of a and b:");scanf("%d%d",&a,&b);avg=(a+b)/2;printf("Average =%d",avg);}return0;} ...
A loop is used for executing a block of statements repeatedly until a particular condition is satisfied. For example, when you are displaying number from 1 to 100 you may want set the value of a variable to 1 and display it 100 times, increasing its valu
Note:For those who don’t know printf or need to know more about printf format specifiers, then first a look at ourprintf C language tutorial. Let’s look at the “for loop” from the example: We first start by setting the variable i to 0. This is where we start to count. Then ...
For Loop in C Programming | Definition, Syntax & Examples While Loop in C++ | Syntax, Uses & Examples 4:08 min Do While Loop: Definition, Example & Results 4:08 min Nesting Loops & Statements in C Programming 3:25 min Loop Control Statements in C++ Programming Risks & Errors in While,...
In this example, when the value of “i” is 5, the “goto” statement is encountered. It transfers the control of the program to the label named “skip”. As a result, the remaining statements within the current iteration of the loop are skipped, and the program directly proceeds to the...
1. For Loop In this example, the for loop simulates a countdown sequence from 3 to 1, with a half-second delay between each number using the usleep function. After completing the countdown, the program prints “Happy Birthday!”.