Author: admin

  • C Program to Find LCM of two Numbers

    This tutorial will teach you how to find the LCM (Least Common Multiple) of two numbers in C. To understand this example, you should have knowledge of the following C programming topics.

    The LCM (Least Common Multiple) of two integers is the smallest positive integer that is perfectly divisible by both the numbers  (without a remainder). For example, the LCM of 30 and 40 is 120.


    Find LCM in C using loop

    #include <stdio.h>
    
    int main()
    {
      int num1, num2, largest;
    
      printf("Enter 2 positive integers:\n");
      scanf("%d %d", &num1, &num2);
      
      //the largest among these two is stored in largest
      largest = (num1 > num2) ? num1 : num2;
    
      while (1)  //always true
      {
        if (largest % num1 == 0 && largest % num2 == 0)
        {
          printf("The LCM of %d and %d is: %d", num1, num2, largest);
          break;
        }
    
        ++largest;
      }
    
      return 0;
    }

    Output:

    Enter 2 positive integers:
    30
    40
    The LCM of 30 and 40 is: 120


    C Program to Find LCM using GCD

    #include <stdio.h>
    
    int main()
    {
      int num1, num2, i, gcd, lcm;
    
      printf("Enter 2 positive integers:\n");
      scanf("%d %d", &num1, &num2);
    
      for (i = 1; i <= num1 && i <= num2; ++i)
      {
        // check if i is a factor of both integers
        if (num1 % i == 0 && num2 % i == 0)
          gcd = i;
      }
    
      lcm = (num1 *num2) / gcd;   //use of gcd
    
      printf("The LCM of %d and %d: %d", num1, num2, lcm);
      return 0;
    }

    Output:

    Enter 2 positive integers:
    30
    40
    The LCM of 30 and 40: 120


  • C Program to Find the GCD and LCM of Two Integers

    The post on C program calculates the GCD and LCM of two integer numbers.

    GCD(Greatest Common Divisor) or HCF: Largest Integer that can divide both the numbers without any remainder or with 0 as remainder. For example, the GCD or HCF of 36 and 48 is 12.

    LCM(Least Common Multiple): The LCM of two integers is the smallest positive integer that is perfectly divisible by both the numbers (without a remainder). For example, the LCM of 16 and 24 is 48.


    C Program to find HCF and LCM of two Integer Number

    #include <stdio.h>
    
    int main()
    {
      int num1, num2, hcf, lcm, count = 1, small;
    
      printf("Enter 2 Numbers: \n");
      scanf("%d %d", &num1, &num2);
    
      small = (num1 < num2) ? num1 : num2;
    
      while (count <= small)
      {
        if (num1 % count == 0 && num2 % count == 0)
        {
          hcf = count;
        }
    
        count++;
      }
    
      lcm = (num1 *num2) / hcf;
    
      printf("GCD is: %d\n", hcf);
      printf("Lcm is: %d", lcm);
    
      return 0;
    }

    Output:

    Enter 2 Numbers:
    36
    48
    GCD is: 12
    LCM is: 144


    C Program to find HCF and LCM using Recursion

    Learn more on recursion.

    #include <stdio.h>
    
    int gcdFunction(int, int);
    
    int main()
    {
      int num1, num2, hcf, lcm;
    
      printf("Enter 2 number: \n");
      scanf("%d %d", &num1, &num2);
    
      hcf = gcdFunction(num1, num2);
      lcm = (num1 *num2) / hcf;
    
      printf("GCD of %d and %d: %d\n", num1, num2, hcf);
      printf("LCM of %d and %d: %d\n", num1, num2, lcm);
    
      return 0;
    }
    
    int gcdFunction(int x, int y)
    {
      if (y == 0)
        return x;
      else
        return gcdFunction(y, x % y); //recursion
    }

    Output:

    Enter 2 number:
    36
    48
    GCD of 36 and 48: 12
    LCM of 36 and 48: 144


  • C Program to Find Sum of First and Last Digit of any Number

    In this tutorial, we will learn, how to find the sum of the first and last digit of a number in C. We will see two examples with different approaches. Before that, you should have knowledge of the following topics in C.


    C Program to Find Sum of First and Last Digit of any Number using loop

    #include <stdio.h>
    
    int main()
    {
      int num, sum = 0, firstDigit, lastDigit;
    
      printf("Enter the Number: ");
      scanf("%d", &num);
    
      // last digit of a number
      lastDigit = num % 10;
    
      //first digit by dividing n by 10 until n greater then 10
      while (num >= 10)
      {
        num = num / 10;
      }
    
      firstDigit = num;
    
      //Calculate sum of first and last digit
      sum = firstDigit + lastDigit;
    
      printf("Sum of first and last digit of entered Number: %d", sum);
      return 0;
    }

    Output:

    Enter the Number: 325
    Sum of first and last digit of entered Number: 8

    Now if you enter a number with only one digit, the program will take that single digit as a firstDigit as well as lastDigit and add them both as shown below.

    Enter the Number: 2
    Sum of first and last digit of entered Number: 4


    C Program to Find Sum of First and Last Digit of any Number without using loop

    #include <stdio.h>
    #include <math.h>
    
    int main()
    {
      int num, sum = 0, firstDigit, lastDigit, digit;
    
      printf("Enter the Number: ");
      scanf("%d", &num);
    
      // last digit of a number
      lastDigit = num % 10;
    
     	//total number of digit - 1
      digit = (int) log10(num);
    
      //first digit
      firstDigit = (int)(num / pow(10, digit));
    
      //Calculate sum of first and last digit
      sum = firstDigit + lastDigit;
    
      printf("Sum of first and last digit of entered Number = %d", sum);
      return 0;
    }

    Output:

    Enter the Number: 964
    Sum of first and last digit of entered Number: 13

    Note: log10() is a mathematical function present in math.h header file. It returns the log base 10 value of the passed parameter to log10() function.


  • C Program to Generate Fibonacci Series

    This is the tutorial on the Fibonacci series in C with an example. Before that, you should have knowledge of the following topic in C.

    What is Fibonacci Series?

    Fibonacci series is the series of numbers where the next number is achieved by the addition of the previous two numbers. The initial addition of two numbers is 0 and 1.

    For example: 0,1,1,2,3,5,8,13….etc.

    We will learn two ways to display the Fibonacci series in C.

    1. using for loop
    2. using recursion

    1. C Program to Generate Fibonacci Series using for loop

    #include <stdio.h>
    
    int main()
    {
      int i, n, term1 = 0, term2 = 1;
      int nextTerm = term1 + term2;
    
      printf("Enter the number of terms you want: ");
      scanf("%d", &n);
    
      printf("Fibonacci Series: %d, %d, ", term1, term2);
    
      for (i = 2; i < n; ++i)
      {
        printf("%d, ", nextTerm);
        term1 = term2;
        term2 = nextTerm;
        nextTerm = term1 + term2;
      }
    
      return 0;
    }

    Output:

    Enter the number of terms you want: 10
    Fibonacci Series: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34,


    2. Fibonacci series using Recursion in C

    #include <stdio.h>
    
    void displayFibonacci(int);
    int main()
    {
      int num;
      printf("Enter the number of terms you want: ");
      scanf("%d", &num);
    
      printf("Fibonacci Series: %d %d ", 0, 1);
    
      displayFibonacci(num - 2); //since two numbers are already printed, hence n-2    
      return 0;
    }
    
    void displayFibonacci(int n)
    {
      static int term1 = 0, term2 = 1, nextTerm;
    
      if (n > 0)
      {
        nextTerm = term1 + term2;
        term1 = term2;
        term2 = nextTerm;
        printf("%d ", nextTerm);
        displayFibonacci(n - 1);
      }
    }

    Output:

    Enter the number of terms you want: 10
    Fibonacci Series: 0 1 1 2 3 5 8 13 21 34


  • C Program to Check Whether a Number is Palindrome or Not

    In this tutorial, you will learn how to check whether the number is palindrome or not in C programming. Before that, let us understand the Palindrome number.

    What is a palindrome number?

    A number is said to be a Palindrome number if it remains the same when its digits are reversed or are the same as forward. It can also be applied to the word, phrase, or other sequences of symbols.

    For example: 14141, 777, 272 are palindrome numbers as they remain the same even if reversed. If we take the example of ‘mam’ or ‘madam’, these are also palindrome words.


    C Program to Check Whether a Number is Palindrome or Not

    #include <stdio.h>
    
    int main()
    {
      int num, revNum = 0, remainder, originalNum;
    
      printf("Enter the Number: ");
      scanf("%d", &num);
    
      originalNum = num;
    
      //storing by reversing the number
      while (num != 0)
      {
        remainder = num % 10;
        revNum = revNum *10 + remainder;
        num /= 10;
      }
    
      // check if originalNum and revNum are equal or not
      if (originalNum == revNum)
        printf("%d is a palindrome Number", originalNum);
      else
        printf("%d is not a palindrome Number", originalNum);
    
      return 0;
    }

    Output:

    //Run 1
    Enter the Number: 121
    121 is a palindrome Number

    //Run 2
    Enter the Number: 245
    245 is not a palindrome Number


  • Sum of digits of a Number in C

    This tutorial is to find the sum of digits of a number using for loop and while loop in C. You need to have knowledge of the following topics in C.

    Explanation:
    To find the sum of the digits means to add all the digits present in the number. For example: consider a number 123, sum of the digits of 123 will be 1+2+3 = 6.


    C Program to find the sum of the digits of a number using for loop

    #include <stdio.h>
    
    int main()
    {
      int num, sum = 0, rem, i;
    
      printf("Enter a number: ");
      scanf("%d", &num);
    
      for (i = num; num != 0; num = num / 10)
      {
        rem = num % 10;
        sum = sum + rem;
      }
    
      printf("Sum of digits of a number: %d", sum);
    
      return 0;
    }

    Output:

    Enter a number: 245
    Sum of digits of a number: 11


    C Program to find the sum of the digits of a number using while loop

    To find the sum of a digit in c using while loop is similar to the for loop, here you need to initialize i with num after the input is taken from the user and i increment is done inside the while body as shown below.

    #include <stdio.h>
    
    int main()
    {
      int num, sum = 0, rem, i;
    
      printf("Enter a number: ");
      scanf("%d", &num);
    
      i = num;
    
      while (num != 0)
      {
        rem = num % 10;
        sum = sum + rem;
        num = num / 10;
      }
    
      printf("Sum of digits of a number: %d", sum);
    
      return 0;
    }

    Output:

    Enter a number: 123
    Sum of digits of a number: 6


    C Program to find the sum of the digits of a number using recursion function

    #include <stdio.h>
    
    int sumDigits(int);
    int main()
    {
      int num, result;
    
      printf("Enter a number: ");
      scanf("%d", &num);
    
      result = sumDigits(num);
    
      printf("Sum of digits of a number: %d", result);
    
      return 0;
    }
    
    int sumDigits(int n)
    {
      if (n == 0)
        return 0;
    
      return (n % 10 + sumDigits(n / 10));	//recursion call
    }

    Output:

    Enter a number: 123
    Sum of digits of a number: 6

    Learn more on recursion.


  • Automorphic Number Program in Java

    In this tutorial, you will learn about the Automorphic Number along with an example and also code java to check whether the number is automorphic or not.

    What is an Automorphic number?

    A number is said to be an automorphic number if the square of the given number ends with the same digits as the number itself. Example: 25, 76, 376, etc.

    Automorphic Number

    Check Whether a number is an Automorphic Number in Java

    import java.util.Scanner;
    
    public class Automorphic
    {
      public static void main(String args[])
      {
        Scanner in = new Scanner(System.in);
        System.out.println("Enter the Number: ");
        int num = in .nextInt();
    
        int count = 0, sqr = num * num;
        int temp = num;	//copying num into temp
    
        //condition applied
        while (temp > 0)
        {
          count++;
          temp = temp / 10;
        }
    
        int lastSquareDigits = (int)(sqr % (Math.pow(10, count)));
    
        //comparing to check Automorphic
        if (num == lastSquareDigits)
          System.out.println(num + " is an Automorphic number");
        else
          System.out.println(num + " is not an Automorphic number");
      }
    }

    Output:

    Enter the Number:
    25
    25 is an Automorphic number

    //Another execution
    Enter the Number:
    23
    23 is not an Automorphic number


    Java Program to Find all Automorphic Numbers in the Interval

    Now what if we need to find Automorphic Numbers between the range of a given number, the above example will not work as it is applied only for a single number.

    To check all the numbers within some given range of numbers, we will create a separate function that will return a true or false value. In the main function, we will call the created function in a loop so that it can check/call for every number.

    The loop will run from the starting number to the range mentioned in the program.

    import java.util.Scanner;
    
    public class AutomorphicClass
    {
      public static void main(String args[])
      {
        Scanner in = new Scanner(System.in);
    
        int lower, upper;
    
        System.out.println("Enter the lower Range: ");
        lower = in .nextInt();
        System.out.println("Enter the Upper Range: ");
        upper = in .nextInt();
    
        System.out.println("Automorphic numbers between " + lower + " and " + upper);
        for (int i = lower; i <= upper; i++)
        {
          if (checkForAutomorphic(i))
            System.out.print(i + " ");
        }
      }
    
      //funcion
      private static boolean checkForAutomorphic(int num)
      {
        int count = 0, sqr = num * num;
        int temp = num;	//copying num
    
        //condition check
        while (temp > 0)
        {
          count++;
          temp = temp / 10;
        }
    
        int lastSquareDigits = (int)(sqr % (Math.pow(10, count)));
    
        return num == lastSquareDigits;
      }
    }

    Output:

    Enter the lower Range:
    1
    Enter the Upper Range:
    100
    Automorphic numbers between 1 and 100
    1 5 6 25 76


  • C Program to Print Natural Numbers in Reverse order with a difference of 2

    In this tutorial, you will learn how to print the natural numbers in reverse order from n to 1 and also between the range of two numbers with a difference of 2 in C. You need to have the knowledge of the following in C.


    C Program to Print Natural Numbers in Reverse from n to 1 with a difference of 2

    The program below is done with help of for loop but you can do the same with the help of a while loop too.

    #include <stdio.h>
    
    int main()
    {
      int num, i;
    
      printf("Enter the upper value: ");
      scanf("%d", &num);
    
      printf("Natural Numbers with difference 2 ranging between %d to 1 are: \n", num);
      for (i = num; i >= 1; i-=2)
      {
        printf("%d ", i);
      }
    
      return 0;
    }

    Output:

    Enter the upper value: 10
    Natural Numbers with difference 2 ranging between 10 to 1 are:
    10 8 6 4 2 1

    You can do the same with a while loop just replace the for statement with the following code. Remember to assign the num to i after taking the input as shown below.

    i = num;
    while (i >= 1)
    {
      printf("%d ", i);;
      i--;
    }

    C Program to return Natural Numbers in Reverse within a Range with a difference of 2

    #include <stdio.h>
    
    int main()
    {
      int numStart, numEnd, i;
    
      printf("Enter the upper value: ");
      scanf("%d", &numStart);
      printf("Enter the lower value: ");
      scanf("%d", &numEnd);
    
      i = numStart;
    
      printf("Natural Numbers with difference 2 ranging between %d to %d are: \n", numStart, numEnd);
      while (i >= numEnd)
      {
        printf("%d ", i);;
        i -= 2;
      }
    
      return 0;
    }

    Output:

    Enter the upper value: 20
    Enter the lower value: 10
    Natural Numbers with difference 2 ranging between 20 to 10 are:
    20 18 16 14 12 10

    Also, you can replace the while loop with for loop as shown in the first example above.


  • C Program to Print Natural Numbers in Reverse from n to 1

    In this tutorial, you will learn how to print the natural numbers in reverse order from n to 1 and also between the range of two numbers in C. You need to have the knowledge of the following in C.


    C Program to Print Natural Numbers in Reverse from n to 1 using loop

    The program below is done with help of for loop but you can do the same with the help of while loop too.

    #include <stdio.h>
    
    int main()
    {
      int num, i;
    
      printf("Enter the upper value: ");
      scanf("%d", &num);
    
      printf("Natural Numbers ranging between %d to 1 are: \n", num);
      for (i = num; i >= 1; i--)
      {
        printf("%d ", i);
      }
    
      return 0;
    }

    Output:

    Enter the upper value: 10
    Natural Numbers ranging between 10 to 1 are:
    10 9 8 7 6 5 4 3 2 1

    You can do the same with a while loop just replace the for statement with the following code. Remember to assign the num to i after taking the input as shown below.

    i = num;
    while (i >= 1)
    {
      printf("%d ", i);;
      i--;
    }

    C Program to return Natural Numbers in reverse within a Range

    #include <stdio.h>
    
    int main()
    {
      int numStart, numEnd, i;
    
      printf("Enter the upper value: ");
      scanf("%d", &numStart);
      printf("Enter the lower value: ");
      scanf("%d", &numEnd);
    
      i = numStart;
    
      printf("Natural Numbers ranging between %d to %d are: \n", numStart, numEnd);
      while (i >= numEnd)
      {
        printf("%d ", i);;
        i--;
      }
    
      return 0;
    }

    Output:

    Enter the upper value: 10
    Enter the lower value: 4
    Natural Numbers ranging between 10 to 4 are:
    10 9 8 7 6 5 4

    Also, you can replace the while loop with for loop as shown in the first example above.


  • C Program to Reverse a Number using Loops

    In this tutorial, we will write a C program to reverse a number using loops. Before that, you may go through the following topics in C.

    To reverse an integer in C is achieved using loop and operators. For example, if the input number is 123 then the program should give 321 as a result.

    We will learn three ways to display the number in reverse order in C.


    All of the programs below take the user input for an integer. Then using three different loops in C, we calculate the reverse of the number entered. The final result is displayed at the end.

    C program to reverse a number using while loop

    #include <stdio.h>
    
    int main() 
    {
       int num, rev = 0, rem;
        
       //User Input
       printf("Enter the Number: ");
       scanf("%d", &num);
        
       while (num != 0) 
       {
         rem = num % 10;
         rev = rev * 10 + rem;
         num /= 10;
       }
    
       printf("Reversed number: %d", rev);
       return 0;
    }

    Output:

    Enter the Number: 1234
    Reversed number: 4321


    C program to reverse a number using for loop

    #include <stdio.h>
    
    int main()
    {
      int num, rev = 0, rem;
    
      //User Input
      printf("Enter the Number: ");
      scanf("%d", &num);
    
      for (; num != 0; num = num / 10)
      {
        rem = num % 10;
        rev = rev *10 + rem;
      }
    
      printf("Reversed number: %d", rev);
      return 0;
    }

    Output:

    Enter the Number: 1234
    Reversed number: 4321


    C program to reverse a number using do-while loop

    #include <stdio.h>
    
    int main()
    {
      int num, rev = 0, rem;
    
      //User Input
      printf("Enter the Number: ");
      scanf("%d", &num);
    
      do {
        rem = num % 10;
        rev = rev *10 + rem;
        num /= 10;
      } while (num != 0);	//condition
    
      printf("Reversed number: %d", rev);
      return 0;
    }

    Output:

    Enter the Number: 1234
    Reversed number: 4321


    C program to reverse a number using recursion

    Learn more on recursion function in C programming

    #include <stdio.h>
    
    int reverse(int);
    
    int main()
    {
      int num, result;
    
      printf("Enter the Number: ");
      scanf("%d", &num);
    
      result = reverse(num);
    
      printf("Reversed number: %d", result);
    
      return 0;
    }
    
    int reverse(int n)
    {
      static int rev = 0;
    
      if (n == 0)
        return 0;
    
      rev = rev * 10;
      rev = rev + n % 10;
      reverse(n / 10); //recursion
      return rev;
    }

    Output:

    Enter the Number: 1234
    Reversed number: 4321