For loop in C / C++ with examples
In this tutorial, we will learn about the C / C++ for
loop and its working with the help of some examples. This is one of the basic knowledge in C / C++ programming language, so please learn it carefully.
Top 20 for loop in C / C++ with examples
Here, we will implement examples using for
loop in C / C++ from basic to advanced. For each example, we will implement with two different languages C and C++.
Example 1: Printing numbers from 1 to 100 with for loop in C / C++
C++ programming:
#include <iostream> using namespace std; int main() { for (int i = 1; i <= 100; ++i) { cout << i << " "; } cout<<"\n-------------------------------\n"; cout<<"This program is posted at learnnc.com"; return 0; }
C programming:
#include <stdio.h> int main(void) { for (int i = 1; i <= 100; ++i) { printf(" %d", i); } printf("\n-------------------------------\n"); printf("This program is posted at learnnc.com"); return 0; }
Output:
Example 2: Displaying a text 10 times with for loop in C / C++
C++ programming:
#include <iostream> using namespace std; int main() { for (int i = 1; i <= 10; ++i) { cout <<"Hello, I'm learnc.net\n"; } cout<<"\n-------------------------------\n"; cout<<"This program is posted at learnnc.com"; return 0; }
C programming:
#include <stdio.h> int main(void) { for (int i = 1; i <= 10; ++i) { printf("Hello, I'm learnc.net\n"); } printf("\n-------------------------------\n"); printf("This program is posted at learnnc.com"); return 0; }
Output:
Example 3: Find the sum of first n Natural Numbers with for loop in C / C++
C++ programming:
#include <iostream> using namespace std; int main() { int num, sum; sum = 0; cout << "Enter a positive integer: "; cin >> num; for (int i = 1; i <= num; ++i) { sum += i; } cout << "Sum = " << sum << endl; cout<<"\n-------------------------------\n"; cout<<"This program is posted at learnnc.com"; return 0; }
C programming:
#include <stdio.h> int main(void) { int num, sum; sum = 0; printf("Enter a positive integer: "); scanf("%d", &num); for (int i = 1; i <= num; ++i) { sum += i; } printf("Sum = %d\n",sum); printf("\n-------------------------------\n"); printf("This program is posted at learnnc.com"); return 0; }
Output:
Example 4: Display elements of array using for loop in C / C++
C++ programming:
#include <iostream> using namespace std; int main() { int arr[]={21,9,56,99, 202}; /* We have set the value of variable i * to 0 as the array index starts with 0 * which means the first element of array * starts with zero index. */ cout<<"Elements of array: "; for(int i=0; i<5; i++){ cout<<arr[i]<<" "; } cout<<"\n-------------------------------\n"; cout<<"This program is posted at learnnc.com"; return 0; }
C programming:
#include <stdio.h> int main(void) { int arr[]={21,9,56,99, 202}; /* We have set the value of variable i * to 0 as the array index starts with 0 * which means the first element of array * starts with zero index. */ printf("Elements of array: "); for(int i=0; i<5; i++){ printf("%d ",arr[i]); } printf("\n-------------------------------\n"); printf("This program is posted at learnnc.com"); return 0; }
Output:
Example 5: Displaying even numbers from 1 to n with for loop in C / C++
C++ programming:
#include <iostream> using namespace std; int main() { int n; cout<<"Enter number n: "; cin>>n; cout<<"Even numbers from 1 to "<< n<<" is: "; for(int i = 1; i <= n; i++){ if(i % 2 == 0) cout<<i<<" "; } cout<<"\n-------------------------------\n"; cout<<"This program is posted at learnnc.com"; return 0; }
C programming:
#include <stdio.h> #include <stdbool.h> int main(void) { int n; printf("Enter number n: "); scanf("%d", &n); printf("Even numbers from 1 to %d is: ", n); for(int i = 1; i <= n; i++){ if(i % 2 == 0) printf("%d ", i); } printf("\n-------------------------------\n"); printf("This program is posted at learnnc.com"); return 0; }
Output:
Example 6: Displaying odd numbers from 1 to n with for loop in C / C++
C++ programming:
#include <iostream> using namespace std; int main() { int n; cout<<"Enter number n: "; cin>>n; cout<<"Odd numbers from 1 to "<< n<<" is: "; for(int i = 1; i <= n; i++){ if(i % 2 == 1) cout<<i<<" "; } cout<<"\n-------------------------------\n"; cout<<"This program is posted at learnnc.com"; return 0; }
C programming:
#include <stdio.h> #include <stdbool.h> int main(void) { int n; printf("Enter number n: "); scanf("%d", &n); printf("Odd numbers from 1 to %d is: ", n); for(int i = 1; i <= n; i++){ if(i % 2 == 1) printf("%d ", i); } printf("\n-------------------------------\n"); printf("This program is posted at learnnc.com"); return 0; }
Output:
Example 7: Displaying primes from 1 to n with for loop in C / C++
C++ programming:
#include <iostream> using namespace std; bool isPrimes(int n) { if (n < 2){ return false; } if (n == 2){ return true; } if (n % 2 == 0){ return false; } for (int i = 3; i < (n - 1); i += 2){ if (n % i == 0){ return false; } } return true; } int main() { int n; cout<<"Enter number n: "; cin>>n; cout<<"Primes from 1 to "<< n<<" is: "; for(int i = 1; i <= n; i++){ if(isPrimes(i)) cout<<i<<" "; } cout<<"\n-------------------------------\n"; cout<<"This program is posted at learnnc.com"; return 0; }
C programming:
#include <stdio.h> #include <stdbool.h> bool isPrimes(int n) { if (n < 2){ return false; } if (n == 2){ return true; } if (n % 2 == 0){ return false; } for (int i = 3; i < (n - 1); i += 2){ if (n % i == 0){ return false; } } return true; } int main(void) { int n; printf("Enter number n: "); scanf("%d", &n); printf("Primes from 1 to %d is: ", n); for(int i = 1; i <= n; i++){ if(isPrimes(i)) printf("%d ", i); } printf("\n-------------------------------\n"); printf("This program is posted at learnnc.com"); return 0; }
Output:
Example 8: Displaying even numbers in array with for loop in C / C++
C++ programming:
#include <iostream> using namespace std; int main() { int arr[]={1, 5, 9, 7, 11, 2, 10, 6, 7}; /* We have set the value of variable i * to 0 as the array index starts with 0 * which means the first element of array * starts with zero index. */ cout<<"Elements of array: "; for(int i=0; i<9; i++){ cout<<arr[i]<<" "; } cout<<"\nEven numbers in array: "; for(int i=0; i<9; i++){ if(arr[i] % 2 == 0) cout<<arr[i]<<" "; } cout<<"\n-------------------------------\n"; cout<<"This program is posted at learnnc.com"; return 0; }
C programming:
#include <stdio.h> #include <stdbool.h> int main(void) { int arr[]={1, 5, 9, 7, 11, 2, 10, 6, 7}; /* We have set the value of variable i * to 0 as the array index starts with 0 * which means the first element of array * starts with zero index. */ printf("Elements of array: "); for(int i=0; i<9; i++){ printf("%d ",arr[i]); } printf("\nEven numbers in array: "); for(int i=0; i<9; i++){ if(arr[i] % 2 == 0) printf("%d ",arr[i]); } printf("\n-------------------------------\n"); printf("This program is posted at learnnc.com"); return 0; }
Output:
Example 9: Displaying odd numbers in array with for loop in C / C++
C++ programming:
#include <iostream> using namespace std; int main() { int arr[]={1, 5, 9, 7, 11, 2, 10, 6, 7}; /* We have set the value of variable i * to 0 as the array index starts with 0 * which means the first element of array * starts with zero index. */ cout<<"Elements of array: "; for(int i=0; i<9; i++){ cout<<arr[i]<<" "; } cout<<"\nOdd numbers in array: "; for(int i=0; i<9; i++){ if(arr[i] % 2 == 1) cout<<arr[i]<<" "; } cout<<"\n-------------------------------\n"; cout<<"This program is posted at learnnc.com"; return 0; }
C programming:
#include <stdio.h> #include <stdbool.h> int main(void) { int arr[]={1, 5, 9, 7, 11, 2, 10, 6, 7}; /* We have set the value of variable i * to 0 as the array index starts with 0 * which means the first element of array * starts with zero index. */ printf("Elements of array: "); for(int i=0; i<9; i++){ printf("%d ",arr[i]); } printf("\nOdd numbers in array: "); for(int i=0; i<9; i++){ if(arr[i] % 2 == 1) printf("%d ",arr[i]); } printf("\n-------------------------------\n"); printf("This program is posted at learnnc.com"); return 0; }
Output:
Example 10: Displaying primes numbers in array with for loop in C / C++
C++ programming:
#include <iostream> using namespace std; bool isPrimes(int n) { if (n < 2){ return false; } if (n == 2){ return true; } if (n % 2 == 0){ return false; } for (int i = 3; i < (n - 1); i += 2){ if (n % i == 0){ return false; } } return true; } int main() { int arr[]={1, 5, 9, 7, 11}; /* We have set the value of variable i * to 0 as the array index starts with 0 * which means the first element of array * starts with zero index. */ cout<<"Elements of array: "; for(int i=0; i<5; i++){ cout<<arr[i]<<" "; } cout<<"\nPrimes in array: "; for(int i=0; i<5; i++){ if(isPrimes(arr[i])) cout<<arr[i]<<" "; } cout<<"\n-------------------------------\n"; cout<<"This program is posted at learnnc.com"; return 0; }
C programming:
#include <stdio.h> #include <stdbool.h> bool isPrimes(int n) { if (n < 2){ return false; } if (n == 2){ return true; } if (n % 2 == 0){ return false; } for (int i = 3; i < (n - 1); i += 2){ if (n % i == 0){ return false; } } return true; } int main(void) { int arr[]={1, 5, 9, 7, 11}; /* We have set the value of variable i * to 0 as the array index starts with 0 * which means the first element of array * starts with zero index. */ printf("Elements of array: "); for(int i=0; i<5; i++){ printf("%d ",arr[i]); } printf("\nPrimes in array: "); for(int i=0; i<5; i++){ if(isPrimes(arr[i])) printf("%d ",arr[i]); } printf("\n-------------------------------\n"); printf("This program is posted at learnnc.com"); return 0; }
Output:
Example 11: Find the sum of elements in array with for loop C / C++
C++ programming:
#include <iostream> using namespace std; int main() { int sum = 0; int arr[]={1, 5, 9, 7, 11, 2, 10, 6, 7}; /* We have set the value of variable i * to 0 as the array index starts with 0 * which means the first element of array * starts with zero index. */ cout<<"Elements of array: "; for(int i=0; i<9; i++){ cout<<arr[i]<<" "; } for(int i=0; i<9; i++){ sum += arr[i]; } cout<<"\nSum of elements in array: "<<sum; cout<<"\n-------------------------------\n"; cout<<"This program is posted at learnnc.com"; return 0; }
C programming:
#include <stdio.h> #include <stdbool.h> int main(void) { int sum = 0; int arr[]={1, 5, 9, 7, 11, 2, 10, 6, 7}; /* We have set the value of variable i * to 0 as the array index starts with 0 * which means the first element of array * starts with zero index. */ printf("Elements of array: "); for(int i=0; i<9; i++){ printf("%d ",arr[i]); } for(int i=0; i<9; i++){ sum += arr[i]; } printf("\nSum of elements in array: %d",sum); printf("\n-------------------------------\n"); printf("This program is posted at learnnc.com"); return 0; }
Output:
Example 12: Find the sum of even elements in array with for loop C / C++
C++ programming:
#include <iostream> using namespace std; int main() { int sum = 0; int arr[]={1, 5, 9, 7, 11, 2, 10, 6, 7}; /* We have set the value of variable i * to 0 as the array index starts with 0 * which means the first element of array * starts with zero index. */ cout<<"Elements of array: "; for(int i=0; i<9; i++){ cout<<arr[i]<<" "; } for(int i=0; i<9; i++){ if(arr[i] % 2 == 0) sum += arr[i]; } cout<<"\nSum of even elements in array: "<<sum; cout<<"\n-------------------------------\n"; cout<<"This program is posted at learnnc.com"; return 0; }
C programming:
#include <stdio.h> #include <stdbool.h> int main(void) { int sum = 0; int arr[]={1, 5, 9, 7, 11, 2, 10, 6, 7}; /* We have set the value of variable i * to 0 as the array index starts with 0 * which means the first element of array * starts with zero index. */ printf("Elements of array: "); for(int i=0; i<9; i++){ printf("%d ",arr[i]); } for(int i=0; i<9; i++){ if(arr[i] % 2 == 0) sum += arr[i]; } printf("\nSum of even elements in array: %d",sum); printf("\n-------------------------------\n"); printf("This program is posted at learnnc.com"); return 0; }
Output:
Example 13: Find the sum of odd elements in array with for loop C / C++
C++ programming:
#include <iostream> using namespace std; int main() { int sum = 0; int arr[]={1, 5, 9, 7, 11, 2, 10, 6, 7}; /* We have set the value of variable i * to 0 as the array index starts with 0 * which means the first element of array * starts with zero index. */ cout<<"Elements of array: "; for(int i=0; i<9; i++){ cout<<arr[i]<<" "; } for(int i=0; i<9; i++){ if(arr[i] % 2 == 1) sum += arr[i]; } cout<<"\nSum of odd elements in array: "<<sum; cout<<"\n-------------------------------\n"; cout<<"This program is posted at learnnc.com"; return 0; }
C programming:
#include <stdio.h> #include <stdbool.h> int main(void) { int sum = 0; int arr[]={1, 5, 9, 7, 11, 2, 10, 6, 7}; /* We have set the value of variable i * to 0 as the array index starts with 0 * which means the first element of array * starts with zero index. */ printf("Elements of array: "); for(int i=0; i<9; i++){ printf("%d ",arr[i]); } for(int i=0; i<9; i++){ if(arr[i] % 2 == 1) sum += arr[i]; } printf("\nSum of odd elements in array: %d",sum); printf("\n-------------------------------\n"); printf("This program is posted at learnnc.com"); return 0; }
Output:
Example 14: Dislaying the multiplication table with for loop C / C++
C++ programming:
#include <iostream> using namespace std; int main() { int n; cout<<"Enter number n: "; cin>>n; for(int i = 1; i<= 9; i++){ cout<<n<<" x " <<i<<" = "<<n*i<<"\n"; } cout<<"\n-------------------------------\n"; cout<<"This program is posted at learnnc.com"; return 0; }
C programming:
#include <stdio.h> #include <stdbool.h> int main(void) { int n; printf("Enter number n: "); scanf("%d", &n); for(int i = 1; i<= 9; i++){ printf("%d x %d = %d\n",n,i,n*i); } printf("\n-------------------------------\n"); printf("This program is posted at learnnc.com"); return 0; }
Output:
Example 15: Import and export elements in an array
C++ programming:
#include <iostream> using namespace std; int main() { int n; cout<<"Enter the number of elements in the array: "; cin>>n; int numbers[n]; for (int i = 0; i < n; i++){ cout << "Enter elements, i = : "; cin >> numbers[i]; } cout<<"\nElements in array: "; for (int i = 0; i < n; i++){ cout << numbers[i] << ","; } cout<<"\n-------------------------------\n"; cout<<"This program is posted at learnnc.com"; return 0; }
C programming:
#include <stdio.h> int main(void) { int n; printf("Enter the number of elements in the array: "); scanf("%d", &n); int numbers[n]; for (int i = 0; i < n; i++){ printf("Enter elements, i = : "); scanf("%d",&numbers[i]); } printf("\nElements in array: "); for (int i = 0; i < n; i++){ printf("%d ",numbers[i]); } printf("\n-------------------------------\n"); printf("This program is posted at learnnc.com"); return 0; }
Output:
Example 16: Sort array ascending with for loop in C / C++
C++ programming:
#include <iostream> using namespace std; void sort_numbers_ascending(int number[], int count) { int temp, i, j, k; for (j = 0; j < count; ++j) { for (k = j + 1; k < count; ++k) { if (number[j] > number[k]) { temp = number[j]; number[j] = number[k]; number[k] = temp; } } } cout<<"\n\nArray after sorting: "; for (i = 0; i < count; ++i) cout<<" "<< number[i]; } int main() { int n; int sum = 0; cout<<"Enter the number of elements in the array: "; cin>>n; int numbers[n]; for(int i = 0;i < n;i++){ cout << "Enter elements, i = : "; cin >> numbers[i]; } cout<<"\nElements in array: "; for(int i = 0;i < n;i++){ cout << numbers[i] << " "; } sort_numbers_ascending(numbers,n); cout<<"\n-------------------------------\n"; cout<<"This program is posted at learnnc.com"; return 0; }
C programming:
#include <stdio.h> void sort_numbers_ascending(int number[], int count) { int temp, i, j, k; for (j = 0; j < count; ++j) { for (k = j + 1; k < count; ++k) { if (number[j] > number[k]) { temp = number[j]; number[j] = number[k]; number[k] = temp; } } } printf("\n\nArray after sorting: "); for (i = 0; i < count; ++i) printf("%d ",number[i]); } int main(void) { int n; int sum = 0; printf("Enter the number of elements in the array: "); scanf("%d", &n); int numbers[n]; for(int i = 0;i < n;i++){ printf("Enter elements, i = : "); scanf("%d",&numbers[i]); } printf("\nElements in array: "); for(int i = 0;i < n;i++){ printf("%d ",numbers[i]); } sort_numbers_ascending(numbers,n); printf("\n-------------------------------\n"); printf("This program is posted at learnnc.com"); return 0; }
Output:
Example 17: Sort array descending with for loop in C/C++
C++ programming:
#include <iostream> using namespace std; void sort_numbers_descending(int number[], int count) { int temp, i, j, k; for (j = 0; j < count; ++j) { for (k = j + 1; k < count; ++k) { if (number[j] < number[k]) { temp = number[j]; number[j] = number[k]; number[k] = temp; } } } cout<<"\n\nArray after sorting: "; for (i = 0; i < count; ++i) cout<<" "<< number[i]; } int main() { int n; int sum = 0; cout<<"Enter the number of elements in the array: "; cin>>n; int numbers[n]; for(int i = 0;i < n;i++){ cout << "Enter elements, i = : "; cin >> numbers[i]; } cout<<"\nElements in array: "; for(int i = 0;i < n;i++){ cout << numbers[i] << " "; } sort_numbers_descending(numbers,n); cout<<"\n-------------------------------\n"; cout<<"This program is posted at learnnc.com"; return 0; }
C programming:
#include <stdio.h> void sort_numbers_descending(int number[], int count) { int temp, i, j, k; for (j = 0; j < count; ++j) { for (k = j + 1; k < count; ++k) { if (number[j] < number[k]) { temp = number[j]; number[j] = number[k]; number[k] = temp; } } } printf("\n\nArray after sorting: "); for (i = 0; i < count; ++i) printf("%d ",number[i]); } int main(void) { int n; int sum = 0; printf("Enter the number of elements in the array: "); scanf("%d", &n); int numbers[n]; for(int i = 0;i < n;i++){ printf("Enter elements, i = : "); scanf("%d",&numbers[i]); } printf("\nElements in array: "); for(int i = 0;i < n;i++){ printf("%d ",numbers[i]); } sort_numbers_descending(numbers,n); printf("\n-------------------------------\n"); printf("This program is posted at learnnc.com"); return 0; }
Output:
Above is a summary of for
loop examples. In the next tutorial, we will give examples of while loop in C / C++.