Prime number program in c using for loop and while loop

  • Any number which is divisible by 1 and itself is known as prime number.
  • To check prime number or not in c programming we need to use for loop and iterate from 2 to half of the number.
  • If any number is divisible then it is non prime number, we can exit the loop.
  • Let us see an example program on c to check a number is prime number or not
  • prime number program in c using for loop



Program #1:  Write a c program to check a number is prime number or not using for loop.


  1. #include <stdio.h>
  2. int main()
  3. {
  4.  
  5.    int n, i, count = 0;
  6.  
  7.     printf("Enter number to check prime number or not");
  8.     scanf("%d",&n);
  9.  
  10.     for(i=2; i<=n/2; ++i)
  11.     {
  12.         // check for non prime number
  13.         if(n%i==0)
  14.         {
  15.             count=1;
  16.             break;
  17.         }
  18.     }
  19.  
  20.     if (count==0)
  21.         printf("%d is a prime number.",n);
  22.     else
  23.         printf("%d is not a prime number.",n);
  24.  
  25.     getch();    
  26. }

Output:


prime or not in c program


Program #2:  Write a c program to check a number is prime number or not using while loop.


  1. #include <stdio.h>
  2. int main()
  3. {
  4.    int n, i, count = 0;
  5.  
  6.     printf("Enter number to check prime number or not");
  7.     scanf("%d",&n);
  8.     i=2;
  9.     while( i<=n/2)
  10.     {
  11.         // check for non prime number
  12.         if(n%i==0)
  13.         {
  14.             count=1;
  15.             break;
  16.         }
  17.         i++;
  18.     }
  19.  
  20.     if (count==0)
  21.         printf("%d is a prime number.",n);
  22.     else
  23.         printf("%d is not a prime number.",n);
  24.  
  25.     getch();
  26.     
  27. }
     

 Output:


  1. Enter number to check prime number or not
  2. 12
  3. 12 is not a prime number

  1. Write a c program to generate fibonacci series without recursion  
  2. Write a c program to generate fibonacci series using recursion
  3. Matrix multiplication in c program with explanation
  4. Factorial program in c without recursion
  5. C program to check number is armstrong number or not
  6. Check Armstrong number in c using recursive function  
  7. C program for even or odd using for loop 
  8. C program to check odd or even without using modulus operator and division operator
  9. Prime number program in c using for loop and while loop 
  10. C program to print prime numbers from 1 to n
  11. C program to print multiplication table using while loop and for loop
  12. C program to convert binary to decimal using for loop 
  13. C program to reverse a number using while loop and for loop
  14. C program to find sum of n numbers using for loop 
  15. C program to find leap year using if else  
  16. c program to check leap year using conditional operator  
  17. C program to print patterns of numbers,alphabets and stars in a pyramid shape
  18. C program to swap two numbers without using third variable and using functions 
  19. C Program to swap two numbers without using third variable 
  20. Write a C program to swap two integer arrays  
  21. C program for swapping of two strings  
  22. C program to print given number in words 
  23. C program to print 1 to 100 without using loop
  24. C program to find ASCII value of a string  
  25. Convert string to integer c programming  
  26. C program to display characters from a to z using loop 
  27. C programming power function example program 
  28. C program for addition subtraction multiplication and division using switch case  
  29. C program to delete an element in an array
  30. C program to insert an element in an array
  31. Switch case in c example program
  32. Prime number program in c using for loop
  33. Fibonacci series in c without recursion

C program to check odd or even without using modulus operator and division operator

  • If a number is divisible by 2 then is it an even number otherwise it is odd number.
  • Now we need to check a number is even or odd without using modulus or division operators in c programming language.
  • Yes we can check a number is even or odd without using division and modulus operators for that we need to use & operator
  • number &1 ==1 then it is an even number.
  • Let us check a number is even or not in c program using & operator.
  • Here is the example program for how to find even of odd number without using modulus in c programming language


Program #1: write a c program to check a number is even or odd without using modulus and division operators in c programming.
  • Even or odd program in c without using mod operator 

  1. #include <stdio.h>
  2. int main()
  3. {
  4.     int number;
  5.  
  6.     printf("Enter a number to check even or odd");
  7.     scanf("%d", &number);
  8.  
  9.     if((number & 1)==0)
  10.           printf("%d is even.", number);
  11.     else
  12.         printf("%d is odd.", number);
  13.         
  14.  
  15.     getch();
  16.     
  17. }

Output:


even or odd division modulus in c

  • We can check a number is even or odd without using modulus and division operator in c program
  • The another method is using shift operators
  •   number >> 1) <<1==number then it is even number
  • Like this we can find even or odd number without using modulus in c 

 Program #2: write a c program to check odd or even without using modulus operator

  1. #include <stdio.h>
  2. int main()
  3. {
  4.     int number;
  5.  
  6.     printf("Enter a number to check even or odd");
  7.     scanf("%d", &number);
  8.  
  9.     if(( number >> 1) <<1==number)
  10.           printf("%d is even.", number);
  11.     else
  12.         printf("%d is odd.", number);
  13.         
  14.  
  15.     getch();
  16.     
  17. }

Output:

  1. Enter a number to check even or odd
  2. 4
  3. 4 is Even Number
 

C program for even or odd using for loop

  • If a number is divisible by 2 then it is an even number.
  • If a number is not divisible by to it is an odd number.
  • To check even or odd number in c we just need to check that using one if condition.
  • If if condition of number%2 returns true then it is even number other wise it is odd number.
  • Let us see an example c program to check a given number is even or odd without using any recursive function.



Program #1: write a c program to check a number is even or odd without using recursive function.


  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int main(int argc, char *argv[])
  5. {
  6.   
  7.   int number;
  8.  
  9.     printf("Enter a nuber to check even or odd");
  10.     scanf("%d", &number);
  11.  
  12.     // returns true if number is divisible by 2: even
  13.     if(number % 2 == 0)
  14.         printf("%d is even.", number);
  15.     else
  16.         printf("%d is odd.", number);
  17.  
  18. getch();
  19.  
  20. }

   Output:


evenorodd in c without function



Program #2: write a c program to check a number is even or odd without using if condition














Check Armstrong number in c using recursive function

  • Armstrong number is a number which order of n if
  • xyz=x n + y n + z n;
  • To check three digit Armstrong number in c programming
  • 153=13 * 53*33
  • In the same way we can also check four digit Armstrong number
  • 1634=14+64+34+44 is Armstrong  number.
  • Now let us check a four digit number is armstrong or not by using recursive function in c.
  • Armstrong number in c using recursion function.
  • Four digit armstrong number in c. check below program for four digit or n digit armstrong number in c programming.



Program #1: Write a c program to check four / 4 digit number or N digit number is armstrong number or not by using recursive function

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int powerfun(int, int);
  5.  
  6. int main()
  7. {
  8.    int n, sum = 0, temp, remainder, digits = 0;
  9.  
  10.    printf("Input number to check Armstrong or not");
  11.    scanf("%d", &n);
  12.  
  13.    temp = n;
  14.    // check the number of digits in given number
  15.    while (temp != 0) {
  16.       digits++;
  17.       temp = temp/10;
  18.    }
  19.  
  20.    temp = n;
  21.  
  22.    while (temp != 0) {
  23.       remainder = temp%10;
  24.       sum = sum + powerfun(remainder, digits);
  25.       temp = temp/10;
  26.    }
  27.  
  28.    if (n == sum)
  29.       printf("%d is an Armstrong number.\n", n);
  30.    else
  31.       printf("%d is not an Armstrong number.\n", n);
  32.  
  33.    getch();
  34. }
  35.  
  36. int powerfun(int num, int r) {
  37.    int c, res = 1;
  38.  
  39.    for (c = 1; c <= r; c++) 
  40.       res = res*num;
  41.  
  42.    return res;   
  43. }

Output:


armstrong number recusrion function c

C program to check number is armstrong number or not

  • Armstrong number is a number which order of n if
  • xyz=x n + y n + z n;
  • To check three digit Armstrong number in c programming
  • 153=13 * 53*33
  • In the same way we can also check four digit Armstrong number
  • 1634=14+64+34+44 is Armstrong  number.
  • In c Programming to check a number is armstrong or not we need to split that number and need to find nth power of each number and sum all the results.
  • If total sum is equal to the same number then we can say it is Armstrong number.
  • Let us see an Example program in c to check a number is Armstrong number or not without using recursion.
  • First we will check a three digit number is armstrong number or not 

Program #1 : write a c program to check a three digit number is armstrong number or not without using recursion.

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. void main(){
  4. int number, originalNumber, rem, result = 0;
  5.  
  6.     printf("Enter a three digit number to check armstrong number or not: ");
  7.     scanf("%d", &number);
  8.  
  9.     originalNumber = number;
  10.  
  11.     while (originalNumber != 0)
  12.     {
  13.         rem = originalNumber%10;
  14.         result += rem*rem*rem;
  15.         originalNumber /= 10;
  16.     }
  17.  
  18.     if(result == number)
  19.         printf("%d is an Armstrong number.",number);
  20.     else
  21.         printf("%d is not an Armstrong number.",number);
  22.  
  23.     getch();
  24.     
  25. }

 Output:


Armstrong number in c program

Factorial program in c without recursion

  • Factorial of n= n*n-1*.....1
  • To find factorial of a number in c programming language we need to use for loop and iterate from n to 1 
  • in side loop we need to write a logic to multiply the result.
  •  factorial *= i;    
  • Finally in  factorial  we will have the result of  1 *2 *.....n
  • Let us see an example c program on finding factorial of a number without using recursion. 



 Program #1: Write a c program to print factorial of a number without using recursion.

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int main()
  5. {
  6.     int n, i;
  7.     unsigned long long factorial = 1;
  8.  
  9.     printf("Enter a number to find factorial: ");
  10.     scanf("%d",&n);
  11.  
  12.     // show error if the user enters a negative integer
  13.     if (n < 0)
  14.         printf("Error! Please enter any positive integer number");
  15.  
  16.     else
  17.     {
  18.         for(i=1; i<=n; ++i)
  19.         {
  20.             factorial *= i;              // factorial = factorial*i;
  21.         }
  22.         printf("Factorial of Number %d = %llu", n, factorial);
  23.     }
  24.  
  25.     getch();
  26. }

Output:

factorial number in c program without recursion
Select Menu