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

Matrix multiplication in c program with explanation

  • Matrix multiplication in c.
  • Take three two dimensional arrays 
  • int first_matrix[10][10], second_matrix[10][10], multiply_result[10][10];
  • Two for take input from user for matrices one for capture multiplication of two matrices result.
  • Ask user to enter number of columns of rows of first matrix.  m ,n
  • Read from keyboard using scanf() function in java. and store all the elements (rows*columns) in first matrix using two for loops.
  • Ask user to enter number of columns of rows of second matrix. p , q
  • Read from keyboard using scanf() function in java. and store all the elements (rows*columns) in first matrix using two for loops.
  • Now take two more loops for rows of first matrix and columns of second matrix.
  • Third for loop for multiplication iteratek
  • for (c = 0; c < m; c++) {
  •       for (d = 0; d < q; d++) {
  •         for (k = 0; k < p; k++) {
  •           sum = sum + first_matrix[c][k]*second_matrix[k][d];
  •         }
  •  
  •         multiply_result[c][d] = sum;
  •         sum = 0;
  •       }
  •     } 
  • c program for multiplication of two matrices using arrays


Program #1: Write a c program to multiply two matrices. c program for matrix multiplication using arrays
 
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int main()
  5. {
  6.   int m, n, p, q, c, d, k, sum = 0;
  7.   int first_matrix[10][10], second_matrix[10][10], multiply_result[10][10];
  8.  
  9.   printf("Enter the number of rows and columns of first matrix\n");
  10.   scanf("%d%d", &m, &n);
  11.   printf("Enter the elements of first matrix\n");
  12.    for (c = 0; c < m; c++)
  13.    for (d = 0; d < n; d++)
  14.       scanf("%d", &first_matrix[c][d]);
  15.  
  16.   printf("Enter the number of rows and columns of second matrix\n");
  17.   scanf("%d%d", &p, &q);
  18.  
  19.   if (n != p)
  20.     printf("Matrices with entered orders can't be multiplied with each other.\n");
  21.   else
  22.   {
  23.     printf("Enter the elements of second matrix\n");
  24.  
  25.     for (c = 0; c < p; c++)
  26.       for (d = 0; d < q; d++)
  27.         scanf("%d", &second_matrix[c][d]);
  28.  
  29.     for (c = 0; c < m; c++) {
  30.       for (d = 0; d < q; d++) {
  31.         for (k = 0; k < p; k++) {
  32.           sum = sum + first_matrix[c][k]*second_matrix[k][d];
  33.         }
  34.  
  35.         multiply_result[c][d] = sum;
  36.         sum = 0;
  37.       }
  38.     }
  39.  
  40.     printf("Product of entered matrices:-\n");
  41.  
  42.     for (c = 0; c < m; c++) {
  43.       for (d = 0; d < q; d++)
  44.         printf("%d\t", multiply_result[c][d]);
  45.  
  46.       printf("\n");
  47.     }
  48.   }
  49.  
  50.  getch();
  51. }

Output:


multiply matrices c program

Write a c program to generate fibonacci series using recursion

  • Fibonacci series starts from 0 ,1 and the next number should be sum of previous two numbers.
  • 0+1=1, so the next number in Fibonacci series in c program is 1. 0 1 1 
  • 0 1 1 2 3 5 8.. and so on.
  • We have already seen this c program without using recursion.
  • Now we will see how to generate fibonacci series by using recursion.
  • A function called by itself : recursion. 
  • Fibonacci series c programming using function


Program #1 : Write a C program to print / generate fibonacci series up to n numbers.  Take input from user. By using recursion


  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. // C program to generate  fibonacci series by using recursion 
  5. //www.instanceofjava.com
  6.  
  7. //Function declaration
  8. long long Printfibonacci(int num);
  9.  
  10.  
  11. int main()
  12. {
  13.     int num;
  14.     long long fibonacci;
  15.      
  16.     //Reads number from user to find nth fibonacci term
  17.     printf("Enter any number to generaye fibonacci series up to ");
  18.     scanf("%d", &num);
  19.      
  20.     printf("%d %d ",0,1);  
  21.     Printfibonacci(num-2);//n-2 numbers. i.e 1 and 2 are printed already
  22.      
  23.     getch();
  24. }
  25.  
  26.  
  27. /**
  28.  
  29.  * Recursive function  
  30. */
  31. long long Printfibonacci(int num) 
  32. {
  33.     static int n1=0,n2=1,n3;  
  34.     if(num>0){  
  35.          n3 = n1 + n2;  
  36.          n1 = n2;  
  37.          n2 = n3;  
  38.          printf("%d ",n3);  
  39.          Printfibonacci(num-1); //Recursively calls Printfibonacci()
  40.     }   
  41. }

Output:


fibonacci recursive c prorgam

Select Menu