Breaking News

C Program to Calculate the Sum of Natural Numbers

Example #1: Sum of Natural Numbers Using for Loop #include <stdio.h> int main() { int n, i, sum = 0; printf("Enter a positive integer: "); scanf("%d",&n); for(i=1; i <= n; ++i) { sum += i; // sum = sum+i; } printf("Sum = %d",sum); return 0; } The above program takes the input from the user and stores in variable n. ... Read More »

C Program to Find Factorial of a Number

Example: Factorial of a Number #include <stdio.h> int main() { int n, i; unsigned long long factorial = 1; printf("Enter an integer: "); scanf("%d",&n); // show error if the user enters a negative integer if (n < 0) printf("Error! Factorial of a negative number doesn't exist."); else { for(i=1; i<=n; ++i) { factorial *= i; // factorial = factorial*i; } ... Read More »

C Program to Generate Multiplication Table

Example #1: Multiplication Table Up to 10 #include <stdio.h> int main() { int n, i; printf("Enter an integer: "); scanf("%d",&n); for(i=1; i<=10; ++i) { printf("%d * %d = %d \n", n, i, n*i); } return 0; } Output Enter an integer: 9 9 * 1 = 9 9 * 2 = 18 9 * 3 = 27 9 * 4 ... Read More »

C Program to Display Fibonacci Sequence

Example #1: Fibonacci Series up to n number of terms #include <stdio.h> int main() { int i, n, t1 = 0, t2 = 1, nextTerm = 0; printf("Enter the number of terms: "); scanf("%d", &n); printf("Fibonacci Series: "); for (i = 1; i <= n; ++i) { // Prints the first two terms. if(i == 1) { printf("%d, ", t1); ... Read More »

C Program to Find GCD of two Numbers

Example #1: GCD Using for loop and if Statement #include <stdio.h> int main() { int n1, n2, i, gcd; printf("Enter two integers: "); scanf("%d %d", &n1, &n2); for(i=1; i <= n1 && i <= n2; ++i) { // Checks if i is factor of both integers if(n1%i==0 && n2%i==0) gcd = i; } printf("G.C.D of %d and %d is %d", ... Read More »

C Program to Find LCM of two Numbers

Example #1: LCM using while Loop and if Statement #include <stdio.h> int main() { int n1, n2, minMultiple; printf("Enter two positive integers: "); scanf("%d %d", &n1, &n2); // maximum number between n1 and n2 is stored in minMultiple minMultiple = (n1>n2) ? n1 : n2; // Always true while(1) { if( minMultiple%n1==0 && minMultiple%n2==0 ) { printf("The LCM of %d ... Read More »

C Program to Display Characters from A to Z Using Loop

Example #1: Program to Display English Alphabets #include <stdio.h> int main() { char c; for(c = 'A'; c <= 'Z'; ++c) printf("%c ", c); return 0; } Output A B C D E F G H I J K L M N O P Q R S T U V W X Y Z In this program, the for loop ... Read More »

C Program to Count Number of Digits in an Integer

Example #1: Program to Count Number of Digits in an Integer #include <stdio.h> int main() { long long n; int count = 0; printf("Enter an integer: "); scanf("%lld", &n); while(n != 0) { // n = n/10 n /= 10; ++count; } printf("Number of digits: %d", count); } Output Enter an integer: 3452 Number of digits: 4 Read More »

C Program to Reverse a Number

#include <stdio.h> int main() { int n, reversedNumber = 0, remainder; printf("Enter an integer: "); scanf("%d", &n); while(n != 0) { remainder = n%10; reversedNumber = reversedNumber*10 + remainder; n /= 10; } printf("Reversed Number = %d", reversedNumber); return 0; } Output Enter an integer: 2345 Reversed Number = 5432 Read More »

C Program to Calculate the Power of a Number

Example #1: Power of a Number Using while Loop #include <stdio.h> int main() { int base, exponent; long long result = 1; printf("Enter a base number: "); scanf("%d", &base); printf("Enter an exponent: "); scanf("%d", &exponent); while (exponent != 0) { result *= base; --exponent; } printf("Answer = %lld", result); return 0; } Output Enter a base number: 3 Enter an ... Read More »

Scroll To Top