In the example C++ program, Inside the main() function, we declare four variables: n to store the number of terms in the Fibonacci series, t1 initialized to 0 (the first term of the series), t2 initialized to 1 (the second term), and t3 to hold the next term in the series. Next...
Let’s understand these things with this one example. Example -: If we want to print “Welcome To CsTutorialpoint” 10 times, then one way we can print “Welcome To CsTutorialpoint” by writing 10 printf () in the program. Another way can be that we use Loop Statements. If we use ...
These are statements such as break, return, or goto. To demonstrate a more functional for loop in C programming language, take the example of a program that counts down from ten to start a game of hide and seek. The for loop will take three expressions:...
Looping plays a very pivotal role in any programming language; the same it happens in the case of C++. When one loop resides inside another loop is called nesting. When we loop two loops together, i.e. kind of nesting, then the outer loop takes control of the number of times the inner...
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...
Example: For loop in C Compiler // Program to print numbers from 1 to 10 #include <stdio.h> int main() { int i; for (i = 0; i < 10; i++) { printf("%d\n", i+1); } return 0; } Run Code >> The above code prints the numbers from 1 to 10 using a for loop in...
The “For” loop vs. “Switch” case in C The for() loop iterates through set code blocks. The switch() case, on the other hand, iterates through discrete code blocks. Consider the following example: int i; i = 4; switch(i) { ...
Example: Nested for Loop // C++ program to display 7 days of 3 weeks#include<iostream>usingnamespacestd;intmain(){intweeks =3, days_in_week =7;for(inti =1; i <= weeks; ++i) {cout<<"Week: "<< i <<endl;for(intj =1; j <= days_in_week; ++j) {cout<<" Day:"<< j <...
Example for (i = 2; i <= 512; i *= 2) { printf("%d\n", i);} Try it Yourself » And in this example, we create a program that prints the multiplication table for a specified number:Example int number = 2;int i;// Print the multiplication table for the number 2for (i ...
Next we write the c code to create the infinite loop by using while loop with the following example. Code: #include <stdio.h> void main() { int i = 10; while(1) { printf("%d\t",i); i++; } } Output: As in the above code while loop runs infinite times because the condition...