Loops in C programming language is a conditional concept used for consecutively executing a line or a block of code over and over again until it reaches the value desired by the programmer. In C programming, there are three types of loops, namely For Loop, While Loop and Do While Loop. Loops in C can also be combined with other control statements that include Break statement, Goto statement and Control statement. These loops can be used anywhere in the program, either as entry control or exit control units.
There are 3 types of LOOPS in C.That are following ::
A While loop is the most straightforward looping structure. It is an entry-controlled loop. In a while loop, a condition is evaluated before processing a body of the loop. If a condition is true, then and only then the body of a loop is executed.
After the body of a loop is executed, the control again goes back to the beginning, and the condition is checked. If it is true, the same process is executed until the condition becomes false. Once the condition becomes false, the control goes out of the loop.
In a while loop, if the condition is not true, then the body of a loop will not be executed, not even once.
The for loop in C language is used to iterate the statements or a part of the program several times. It is frequently used to traverse the data structures like the array and linked list.
In a while loop, if the condition is not true, then the body of a loop will not be executed, not even once.
A Do-while loop is similar to the while loop except that the condition is always executed after the body of a loop. It is also called an exit-controlled loop.
In the do-while loop, the body of a loop is always executed at least once. After the body is executed, then it checks the condition. If the condition is true, then it will again execute the body of a loop. Otherwise, the control is transferred out of the loop.
#include <stdio.h> int main() { int i, originalNum, num, lastDigit, sum; long fact; /* Input a number from user */ printf("Enter any number to check Strong number: "); scanf("%d", &num); /* Copy the value of num to a temporary variable */ originalNum = num; sum = 0; /* Find sum of factorial of digits */ while(num > 0) { /* Get last digit of num */ lastDigit = num % 10; /* Find factorial of last digit */ fact = 1; for(i=1; i<=lastDigit; i++) { fact = fact * i; } /* Add factorial to sum */ sum = sum + fact; num = num / 10; } /* Check Strong number condition */ if(sum == originalNum) { printf("%d is STRONG NUMBER", originalNum); } else { printf("%d is NOT STRONG NUMBER", originalNum); } return 0; }
#include <stdio.h> int main() { int n, num, rev = 0; /* Input a number from user */ printf("Enter any number to check palindrome: "); scanf("%d", &n); /* Copy original value to 'num' to 'n'*/ num = n; /* Find reverse of n and store in rev */ while(n != 0) { rev = (rev * 10) + (n % 10); n /= 10; } /* Check if reverse is equal to 'num' or not */ if(rev == num) { printf("%d is palindrome.", num); } else { printf("%d is not palindrome.", num); } return 0; }
#include <stdio.h> #include <math.h> #define BASE 2 int main() { long long binary, decimal=0, tempBinary; int N=0; printf("Enter any binary number: "); scanf("%lld", &binary); tempBinary = binary; while(tempBinary!=0) { /* If current binary digit is 1 */ if(tempBinary % 10 == 1) { decimal += pow(BASE, N); } N++; tempBinary /= 10; } printf("Binary number = %lld\n", binary); printf("Decimal number= %lld", decimal); return 0; }
#include <stdio.h> int main() { int i, num1, num2, min, hcf=1; /* Input two numbers from user */ printf("Enter any two numbers to find HCF: "); scanf("%d%d", &num1, &num2); /* Find minimum between two numbers */ min = (num1<num2) ? num1 : num2; for(i=1; i<=min; i++) { /* If i is factor of both number */ if(num1%i==0 && num2%i==0) { hcf = i; } } printf("HCF of %d and %d = %d\n", num1, num2, hcf); return 0; }
#include <stdio.h> #define SIZE 8 int main() { char binary[SIZE + 1], onesComp[SIZE + 1]; int i, error=0; printf("Enter %d bit binary value: ", SIZE); /* Input binary string from user */ gets(binary); /* Store all inverted bits of binary val to onesComp */ for(i=0; i<SIZE; i++) { if(binary[i] == '1') { onesComp[i] = '0'; } else if(binary[i] == '0') { onesComp[i] = '1'; } else { printf("Invalid Input"); error = 1; /* Exits from loop */ break; } } /* Marks the end of onesComp string */ onesComp[SIZE] = '\0'; /* Check if there are binary string contains no err */ if(error == 0) { printf("Original binary = %s\n", binary); printf("Ones complement = %s", onesComp); } return 0; }
#include <stdio.h> int main() { int i, num, isPrime; isPrime = 1; /* Input a number from user */ printf("Enter any number to check prime: "); scanf("%d", &num); for(i=2; i<=num/2; i++) { /* Check divisibility of num */ if(num%i==0) { /* Set isPrime to 0 indicating it as composite num */ isPrime = 0; /* Terminate from loop */ break; } } /* * If isPrime contains 1 then it is prime */ if(isPrime == 1 && num > 1) { printf("%d is prime number", num); } else { printf("%d is composite number", num); } return 0; }
#include <stdio.h> int main() { int i, j, end, sum; /* Input upper limit to print perfect number */ printf("Enter upper limit: "); scanf("%d", &end); printf("All Perfect numbers between 1 to %d:\n", end); /* Iterate from 1 to end */ for(i=1; i<=end; i++) { sum = 0; for(j=1; j<i; j++) { if(i % j == 0) { sum += j; } } /* If the current number i is Perfect number */ if(sum == i) { printf("%d, ", i); } } return 0; }
#include<stdio.h> int main() { int num,copy_of_num,sum=0,rem; //Store input number in variable num printf("\nEnter a number:"); scanf("%d",&num); /* Value of variable num would change in the below while loop so we are storing it in another variable to compare the results at the end of program */ copy_of_num = num; /* We are adding cubes of every digit * and storing the sum in variable sum */ while (num != 0) { rem = num % 10; sum = sum + (rem*rem*rem); num = num / 10; } /* If sum of cubes of every digit is equal to number * itself then the number is Armstrong */ if(copy_of_num == sum) printf("\n%d is an Armstrong Number",copy_of_num); else printf("\n%d is not an Armstrong Number",copy_of_num); return(0); }
#include <stdio.h> int main() { long long num; int count = 0; /* Input number from user */ printf("Enter any number: "); scanf("%lld", &num); /* Run loop till num is greater than 0 */ do { /* Increment digit count */ count++; /* Remove last digit of 'num' */ num /= 10; } while(num != 0); printf("Total digits: %d", count); return 0; }
#include <stdio.h> int main() { int a, b, c, i, terms; /* Input number from user */ printf("Enter number of terms: "); scanf("%d", &terms); /* Fibonacci magic initialization */ a = 0; b = 1; c = 0; printf("Fibonacci terms: \n"); /* Iterate through n terms */ for(i=1; i<=terms; i++) { printf("%d, ", c); a = b; // Copy n-1 to n-2 b = c; // Copy current to n-1 c = a + b; // New term } return 0; }