C program to reverse a number using while loop and for loop

  • Finding reverse number means for example take 123 then we need to get 321 is its reverse number.
  • In c programming we can do it by using for loop and while loop.
  • Lets us see an example C program to get reverse number of a number by using while loop and for loop.
  • while(n != 0)
  •    {
  •         rem = n%10;
  •         reverse_Number = reverse_Number*10 + rem;
  •         n /= 10;
  •     }



Program #1 : Write a c program to reverse  number using for loop.


  1. #include <stdio.h>
  2. // www. instanceofjava.com all rights reserved
  3. int main()
  4. {
  5.     int n, reverse_Number = 0, rem,Original_number=0;
  6.  
  7.     printf("Enter a number to get reverse number ");
  8.     scanf("%d", &n);
  9.  Original_number=n;
  10.     while(n != 0)
  11.     {
  12.         rem = n%10;
  13.         reverse_Number = reverse_Number*10 + rem;
  14.         n /= 10;
  15.     }
  16.  
  17.     printf("Reversed Number of %d is = %d",Original_number=0;,reverse_Number);
  18.     getch();
  19. }

 Output:


reverse number in c program


Program #2 :  Write a C program to reverse a number using for loop
 

  1. #include <stdio.h>
  2. // www. instanceofjava.com all rights reserved
  3. int main()
  4. {
  5.     int n, reverse_Number = 0, rem,Original_number=0;
  6.  
  7.     printf("Enter a number to get reverse number ");
  8.     scanf("%d", &n);
  9.     Original_number=n;
  10.     for(;n != 0;)
  11.     {
  12.         rem = n%10;
  13.         reverse_Number = reverse_Number*10 + rem;
  14.         n /= 10;
  15.     }
  16.  
  17.     printf("Reversed Number of %d is = %d",Original_number,reverse_Number);
  18.  
  19.     getch();
  20. }

 Output:

  1. Enter a number to get reverse number
  2. 123987
  3. Reversed Number of 123987 is = 789321 

C program to convert binary to decimal using for loop

  • c program to convert binary to decimal using array and for loop.
  • Lets us see an example program on c to convert binary format number to decimal format.
  • Write a function which accepts a number as binary.
  •       rem = n%10;
  •         n /= 10;
  •         decimal += rem*pow(2,i);
  • This is the logic to get decimal format of the number.




Program #1 : write a c program to convert binary to decimal using while loop function

  1. #include <stdio.h>
  2. int convertBinaryToDecimal(long long n);
  3.  
  4. int main()
  5. {
  6.     long long n;
  7.     printf("Enter a binary number: ");
  8.     scanf("%lld", &n);
  9.     printf("%lld binary format= %d decimal format", n, convertBinaryToDecimal(n));
  10.     
  11.     getch();
  12. }
  13.  
  14. int convertBinaryToDecimal(long long n)
  15. {
  16.     int decimal = 0, i = 0, rem;
  17.     while (n!=0)
  18.     {
  19.         rem = n%10;
  20.         n /= 10;
  21.         decimal += rem*pow(2,i);
  22.         ++i;
  23.     }
  24.     return decimal;
  25. }

 Output:


Binary to decimal in c program


Program #2 : write a c program to convert binary to decimal using while loop function

  1. #include <stdio.h>
  2. int convertBinaryToDecimal(long long n);
  3.  
  4. int main()
  5. {
  6.     long long n;
  7.     printf("Enter a binary number: ");
  8.     scanf("%lld", &n);
  9.     printf("%lld binary format= %d decimal format", n, convertBinaryToDecimal(n));
  10.     
  11.     getch();
  12. }
  13.  
  14. int convertBinaryToDecimal(long long n)
  15. {
  16.     int decimal = 0, i = 0, rem;
  17.     for (i=0; n!=0;i++)
  18.     {
  19.         rem = n%10;
  20.         n /= 10;
  21.         decimal += rem*pow(2,i);
  22.       
  23.     }
  24.     return decimal;
  25. }

Output:

  1. Enter a binary number:
  2. 1011
  3. 1011 binary format= 11 decimal format

You Might Like:
  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 print multiplication table using while loop and for loop

C Program to Print Multiplication Table Using While Loop and For Loop
In the programming world, loops are the most basic. It is only by mastering the rules of a loop, and then replacing them with a loop when necessary, that we can program something that is reproducible. This article explores how to print a multiplication table in C using two types of loops: the `for` loop and the `while` loop. In fact, both approaches are often used—not only in commercial programming languages such as Basic but also in later ones like C. Both are also very simple and straightforward, helping you to consolidate your programming skills.

 What is a Multiplication Table?

A multiplication table is a table used to define the multiplication operation for an algebraic system. Multiplying 5 by 1, 5 by 2, and so on—all ten answers to these multiplication problems make up the multiplication table of 5. One of the simplest forms is the multiplication table of 1—since 1 multiplied by any number always equals that same number. The product of a number and any of its factors is in this way called a "multiple." For example, multiply a circular disk by its diameter to get the circumference; the product of 3.14 and a disk with a diameter of 1 is 3.14.
  • Its is very easy to print multiplication table using c program
  • We need one for loop which iterates from 1 to 10
  • Inside for loop just multiply numbers and print result in each iteration.
  • Lets us see an example C program on how to print multiplication table using for loop.
  • multiplication table program in c using for loop
  • write a c program to input a number from user and print multiplication table of the given number using for loop. how to print multiplication table of a given number in c programming.
Program #1 : Write a c program to print multiplication table using for loop.Take input from user.

  1. #include <stdio.h>
  2. int main()
  3. {
  4.    int n, i;
  5.  
  6.     printf("Enter a Number ");
  7.     scanf("%d",&n);
  8.  
  9.     for(i=1; i<=10; ++i)
  10.     {
  11.         printf("%d * %d = %d \n", n, i, n*i);
  12.     }
  13.      
  14.     getch();
  15.     
  16. }
  
Output:


multiplication table in c program

Program #2: Write a c program to print multiplication table using while loop
write a c program to print multiplication table of any number using for loop


  1. #include <stdio.h>
  2. int main()
  3. {
  4.    int n, i;
  5.  
  6.     printf("Enter a Number ");
  7.     scanf("%d",&n);
  8.     i=1;
  9.     while(i<=10){
  10.                 
  11.         printf("%d * %d = %d \n", n, i, n*i);
  12.         ++i;
  13.     }
  14.      
  15.     getch();
  16.     
  17. }
  
Output:

  1. Enter a Number 
  2. 2
  3. 2 * 1 = 2
  4. 2 * 2 = 4
  5. 2 * 3 = 6
  6. 2 * 4 = 8
  7. 2 * 5 = 10
  8. 2 * 6 = 12
  9. 2 * 7 = 14
  10. 2 * 8 = 16
  11. 2 * 9 = 18
  12. 2 * 10 = 20
     

Using a `while` Loop to Print a Multiplication Table

The `while` loop is a control flow statement that allows code to be executed repeatedly based on a given condition. If you want to print the multiplication table of a given number using a `while` loop, here is how you can do it in C:

  1. #include <stdio.h>

  2. int main() {
  3.     int number, i = 1;

  4.     // Input the number for which you want to print the multiplication table
  5.     printf("Enter a number: ");
  6.     scanf("%d", &number);

  7.     // Use a while loop to print the multiplication table
  8.     printf("Multiplication Table of %d using while loop:\n", number);
  9.     while (i <= 10) {
  10.         printf("%d x %d = %d\n", number, i, number * i);
  11.         i++;
  12.     }

  13.     return 0;
  14. }

Explanation:
  • Input the Number: The program first asks the user to input a number for which the multiplication table will be generated.
  • Initialize `i`: The variable `i` is initialized to 1, which will act as the multiplicand.
  • While Loop Condition: As long as `i` is less than or equal to 10, the loop continues.
  • Print the Result: Within the loop, the program prints the result of `number * i`.
  • increment `i`: After each iteration, `i` is incremented by 1 to prepare for the next run.

Output Example:

  1. Enter a number: 7
  2. Multiplication Table of 7 using while loop:
  3. 7 x 1 = 7
  4. 7 x 2 = 14
  5. 7 x 3 = 21
  6. 7 x 4 = 28
  7. 7 x 5 = 35
  8. 7 x 6 = 42
  9. 7 x 7 = 49
  10. 7 x 8 = 56
  11. 7 x 9 = 63
  12. 7 x 10 = 70


Using a `for` Loop to Print a Multiplication Table

The `for` loop is another control flow statement providing a compact way to write loops. If you want to print the multiplication table of a number using a `for` loop, here is how you can do it in C:

  1. #include <stdio.h>

  2. int main() {
  3.     int number;

  4.     // Input the number for which you want to print the multiplication table
  5.     printf("Enter a number: ");
  6.     scanf("%d", &number);

  7.     // Use a for loop to print the multiplication table
  8.     printf("Multiplication Table of %d using for loop:\n", number);
  9.     for (int i = 1; i <= 10; i++) {
  10.         printf("%d x %d = %d\n", number, i, number * i);
  11.     }

  12.     return 0;
  13. }


Explanation:
1. input the Number: Just as in the example with the `while` loop, this program first asks the user to input a number.
2. For Loop Initialization: The `for` loop initializes `i` to 1, checks the condition `i <= 10`, and increments `i` on each iteration.
3. Print the Result: The program prints the result of `number * i` inside the loop.


Output:

  1. Enter a number: 7
  2. Multiplication Table of 7 using for loop:
  3. 7 x 1 = 7
  4. 7 x 2 = 14
  5. 7 x 3 = 21
  6. 7 x 4 = 28
  7. 7 x 5 = 35
  8. 7 x 6 = 42
  9. 7 x 7 = 49
  10. 7 x 8 = 56
  11. 7 x 9 = 63
  12. 7 x 10 = 70

Key Differences Between a `while` Loop and a `for` Loop

Syntax: The syntax of the `for` loop is more succinct than that of the `while` loop. With the `for` loop, we can combine initialization, condition testing, and incrementation all into one line; whereas, in the `while` loop, these steps must be executed separately.
When to Use: Use a `while` loop when you don't know how many times the statement execution will be repeated. Use a `for` loop when you know the number of iterations in advance.


Both loops are powerful tools in the C language for handling repetitive work. By understanding how to use them to print a multiplication table, you can apply these methods to solve more complex problems in the future. Whether you choose a `while` loop or a `for` loop totally depends on your actual use case and coding style. I hope you have fun playing around with the code to understand the range of the multiplication table generated, or even combining the two loops under one program to enhance your understanding of them all. Happy programming!

You Might Like:

  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 print prime numbers 1 to n

  • 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.
  • Now we need to check prime numbers from 1 to 100 or 1 to n.
  • Logic is same and we are adding one more for loop for 1 to N numbers.
  • Now we will iterate for loop from 1 to n and check prime or nor for each number.
  • Lets see an example program on c to check prime numbers from 1 to N. 
  • Prime number program in c using for loop.
  • prime number program in c using while loop
  • prime number using while loop in c
Note: Buy Udemy course from here if you want to learn c programming from beginning.
Master C programming with Example programs, C programming practices for absolute beginners to excel in the industry 


 Program #1 : Write a c program to check prime numbers from 1 to N by using for loop.
  • prime number using for loop in c 
  • C program to check prime number using while loop

  1. #include <stdio.h>
  2. int main()
  3. {
  4.    int n, i,j, count = 0;
  5.  
  6.     printf("Enter a Number");
  7.     scanf("%d",&n);
  8. for(i=2;i<=n;i++)
  9.  {
  10.   for(j=2;j<i;j++)
  11.   {
  12.    if(i%j==0)
  13.    break;
  14.    else if(i==j+1)
  15.    printf("%d\n",i);
  16. }
  17. }
  18.  
  19.     getch();
  20.     
  21. }


 Output:


prime numbers from 1 to n in c

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

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














Select Menu