Example #1: Display Prime Numbers Between two Intervals #include <stdio.h> int main() { int low, high, i, flag; printf("Enter two numbers(intervals): "); scanf("%d %d", &low, &high); printf("Prime numbers between %d and %d are: ", low, high); while (low < high) { flag = 0; for(i = 2; i <= low/2; ++i) { if(low % i == 0) { flag = ... Read More »

## Category Archives: Uncategorized

## C Program to Check Leap Year

Example: Program to Check Leap Year #include <stdio.h> int main() { int year; printf("Enter a year: "); scanf("%d",&year); if(year%4 == 0) { if( year%100 == 0) { // year is divisible by 400, hence the year is a leap year if ( year%400 == 0) printf("%d is a leap year.", year); else printf("%d is not a leap year.", year); } ... Read More »

## C Program to Check Whether a Number is Positive or Negative

Example #1: Check if a Number is Positive or Negative Using if…else #include <stdio.h> int main() { double number; printf("Enter a number: "); scanf("%lf", &number); if (number <= 0.0) { if (number == 0.0) printf("You entered 0."); else printf("You entered a negative number."); } else printf("You entered a positive number."); return 0; } You can also solve this problem using ... Read More »

## C Program to Check Whether a Character is an Alphabet or not

Example: Program to Check Alphabet #include <stdio.h> int main() { char c; printf("Enter a character: "); scanf("%c",&c); if( (c>='a' && c<='z') || (c>='A' && c<='Z')) printf("%c is an alphabet.",c); else printf("%c is not an alphabet.",c); return 0; } Output Enter a character: * * is not an alphabet Read More »

## 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 »