Blog

  • Keith Number Program in C

    In this tutorial, we will write a Keith number in C. It is one of the most asked questions in an interview. Before that, you may go through the following topic in C programming.

    What is Keith Number?

    A positive n digit number x is called a Keith number or repfigit number (repetitive Fibonacci-like digit) if it is arranged in a special number sequence generated using its digits. This sequence has n terms as digit of x and the next term is determined by the sum of previous n terms and so on. And if the number x falls under this sequence then x is a Keith Number. Example: 19, 742, etc

    Explanation: Consider the number 742. Go through the following steps:

    First separate the digit, 7, 4, 2.

    To find the next term, add the digits, 7+4+2 = 13
    New Series: 7, 4, 2, 13.

    To find the next term, add the last three digits of the above sequence, 13+2+4=19
    New Series: 7, 4, 2, 13, 19.

    To find the next term, add the last three digits of the above sequence, 19+13+2=34
    New Series: 7, 4, 2, 13, 19, 34.

    To find the next term, add the last three digits of the above sequence, 34+19+13=66
    New Series: 7, 4, 2, 13, 19, 34, 66.

    To find the next term, add the last three digits of the above sequence, 66+34+19=119
    New Series: 7, 4, 2, 13, 19, 34, 66, 119.

    To find the next term, add the last three digits of the above sequence, 119+66+34=219
    New Series: 7, 4, 2, 13, 19, 34, 66, 119, 219.

    To find the next term, add the last three digits of the above sequence, 219+119+66=404
    New Series: 7, 4, 2, 13, 19, 34, 66, 119, 219, 404.

    To find the next term, add the last three digits of the above sequence, 404+219+119=742
    New Series: 7, 4, 2, 13, 19, 34, 66, 119, 219, 404, 742.

    Now we will stop as we got the number 742 as the term of the series. Hence 742 is a Keith Number.


    C Program to check whether the Number is Keith Number or not

    #
    include < stdio.h > 
    #include <stdlib.h >
    
    //user defined funtion to count the length
    int digitCount(int n)
    {
      int counter = 0;
      while (n > 0)
      {
        n = n / 10;
        counter++;
      }
    
      return counter;
    }
    
    int main()
    {
      int num1 = 0, arr1[10], temp = 0, flag = 0, i = 0, sum = 0;
    
      printf("Enter a number: ");
      scanf("%d", &num1);
    
      temp = num1;
    
      for (i = digitCount(temp) - 1; i >= 0; i--)
      {
        arr1[i] = num1 % 10;
        num1 /= 10;
      }
    
      while (flag == 0)
      {
        for (i = 0; i < digitCount(temp); i++)
          sum += arr1[i];
        if (sum == temp)
        {
          printf("The entered number is a Keith Number\n");
          flag = 1;
        }
    
        if (sum > temp)
        {
          printf("The entered number is NOT a Keith number\n");
          flag = 1;
        }
    
        for (i = 0; i < digitCount(temp); i++)
        {
          if (i != digitCount(temp) - 1)
            arr1[i] = arr1[i + 1];
          else
            arr1[i] = sum;
        }
    
        sum = 0;
      }
    }

    Output:

    //Run 1
    Enter a number: 197
    The entered number is a Keith Number

    //Run 2
    Enter a number: 15
    The entered number is NOT a Keith number


  • Keith Number Program in C++

    In this tutorial, we will write a Keith number in C++. It is one of the most asked questions in an interview. Before that, you may go through the following topic in C programming.

    What is Keith Number?

    A positive n digit number x is called a Keith number or repfigit number (repetitive Fibonacci-like digit) if it is arranged in a special number sequence generated using its digits. This sequence has n terms as digit of x and the next term is determined by the sum of previous n terms and so on. And if the number x falls under this sequence then x is a Keith Number. Example: 19, 742, etc

    Explanation: Consider the number 742. Go through the following steps:

    First separate the digit, 7, 4, 2.

    To find the next term, add the digits, 7+4+2 = 13
    New Series: 7, 4, 2, 13.

    To find the next term, add the last three digits of the above sequence, 13+2+4=19
    New Series: 7, 4, 2, 13, 19.

    To find the next term, add the last three digits of the above sequence, 19+13+2=34
    New Series: 7, 4, 2, 13, 19, 34.

    To find the next term, add the last three digits of the above sequence, 34+19+13=66
    New Series: 7, 4, 2, 13, 19, 34, 66.

    To find the next term, add the last three digits of the above sequence, 66+34+19=119
    New Series: 7, 4, 2, 13, 19, 34, 66, 119.

    To find the next term, add the last three digits of the above sequence, 119+66+34=219
    New Series: 7, 4, 2, 13, 19, 34, 66, 119, 219.

    To find the next term, add the last three digits of the above sequence, 219+119+66=404
    New Series: 7, 4, 2, 13, 19, 34, 66, 119, 219, 404.

    To find the next term, add the last three digits of the above sequence, 404+219+119=742
    New Series: 7, 4, 2, 13, 19, 34, 66, 119, 219, 404, 742.

    Now we will stop as we got the number 742 as the term of the series. Hence 742 is a Keith Number.


    C++ Program to check whether the Number is Keith Number or not

    #include <bits/stdc++.h>
    using namespace std;
    
    int digitCount(int n)
    {
      int counter = 0;
      while (n > 0)
      {
        n = n / 10;
        counter++;
      }
    
      return counter;
    }
    
    int main()
    {
      int num1 = 0, arr1[10], temp = 0, flag = 0, i = 0, sum = 0;
    
      cout << "Enter a number: ";
      cin >> num1;
    
      temp = num1;
    
      for (i = digitCount(temp) - 1; i >= 0; i--)
      {
        arr1[i] = num1 % 10;
        num1 /= 10;
      }
    
      while (flag == 0)
      {
        for (i = 0; i < digitCount(temp); i++)
          sum += arr1[i];
        if (sum == temp)
        {
          cout << "The entered number is a Keith Number\n";
          flag = 1;
        }
    
        if (sum > temp)
        {
          cout << "The entered number is NOT a Keith Number\n";
          flag = 1;
        }
    
        for (i = 0; i < digitCount(temp); i++)
        {
          if (i != digitCount(temp) - 1)
            arr1[i] = arr1[i + 1];
          else
            arr1[i] = sum;
        }
    
        sum = 0;
      }
    }

    Output:

    //Run 1
    Enter a number: 197
    The entered number is a Keith Number

    //Run 2
    Enter a number: 15
    The entered number is NOT a Keith number


  • Singly linked list Program C++

    In this tutorial, we will write a C++ program to implement singly linked list.

    In this program, we will create a separate function for each operation that is to be performed in a linked list. This operation includes:

    • create()
    • insert(): this includes insetion at the begining, at the end or at a given position.
    • delet() : delete is also the same that is at the begining, at the end or at a given position.
    • display()
    • search()

    The program takes user input at every step necessary from the user and performed accordingly. The following are the various C++ programming topics used in the program.

    Singly linked list in C++

    #include <iostream>
    #include <conio.h>
    #include <stdlib.h>
    using namespace std;
    
    class Node
    {
      public_colon
      int info;
      Node * next;
    };
    
    class List: public Node
    {
    
      Node *first, *last;
      public_colon
      List()
      {
        first = NULL;
        last = NULL;
      }
      void create();
      void insert();
      void delet();
      void display();
      void search();
    };
    
    //create function
    void List::create()
    {
      Node * temp;
      temp = new Node;
      int n;
    
      cout << "\nEnter an Element: ";
      cin >> n;
    
      temp->info = n;
      temp->next = NULL;
      if (first == NULL)
      {
        first = temp;
        last = first;
      }
      else
      {
        last->next = temp;
        last = temp;
      }
    }
    
    //insert function
    //at chosen position
    void List::insert()
    {
      Node *prev, *cur;
      prev = NULL;
      cur = first;
      int count = 1, pos, ch, n;
      Node *temp = new Node;
    
      cout << "\nEnter an Element: ";
      cin >> n;
    
      temp->info = n;
      temp->next = NULL;
      cout << "\nChoose the position:";
      cout << "\n1 for the beginning.";
      cout << "\n2 for the end.";
      cout << "\n3 for in between.";
      cout << "\nEnter Your Choice: ";
      cin >> ch;
    
      switch (ch)
      {
        case 1:
          temp->next = first;
          first = temp;
          break;
        case 2:
          last->next = temp;
          last = temp;
          break;
        case 3:
          cout << "\nEnter the Position to Insert: ";
          cin >> pos;
          while (count != pos)
          {
            prev = cur;
            cur = cur->next;
            count++;
          }
          if (count == pos)
          {
            prev->next = temp;
            temp->next = cur;
          }
          else
            cout << "\nNot Able to Insert";
          break;
      }
    }
    
    //function to delete
    //at chosen position
    void List::delet()
    {
      Node *prev = NULL, *cur = first;
      int count = 1, pos, ch;
      cout << "Select the position for deletion: \n";
      cout << "\n1 for the beginning.";
      cout << "\n2 for the end.";
      cout << "\n3 for in between.";
      cout << "\nEnter Your Choice:";
      cin >> ch;
    
      switch (ch)
      {
        case 1:
          if (first != NULL)
          {
            cout << "\nDeleted Element is " << first->info;
            first = first->next;
          }
          else
            cout << "\nOperation Failed";
          break;
        case 2:
          while (cur != last)
          {
            prev = cur;
            cur = cur->next;
          }
          if (cur == last)
          {
            cout << "\nDeleted Element is: " << cur->info;
            prev->next = NULL;
            last = prev;
          }
          else
            cout << "\nOperation Failed";
          break;
        case 3:
          cout << "\nEnter the Position of Deletion: ";
          cin >> pos;
          while (count != pos)
          {
            prev = cur;
            cur = cur->next;
            count++;
          }
    
          if (count == pos)
          {
            cout << "\nDeleted Element is: " << cur->info;
            prev->next = cur->next;
          }
          else
          {
            cout << "\nOperation Failed";
          }
          break;
      }
    }
    
    //display function
    //check for empty list first
    void List::display()
    {
      Node *temp = first;
      if (temp == NULL)
      {
        cout << "\nList is Empty";
      }
      while (temp != NULL)
      {
        cout << temp->info;
        cout << "-->";
        temp = temp->next;
      }
      cout << "NULL";
    }
    
    //search function
    //check for empty list first
    void List::search()
    {
      int value, pos = 0;
      bool flag = false;
    
      if (first == NULL)
      {
        cout << "List is Empty";
        return;
      }
      cout << "Enter the Value to be Searched: ";
      cin >> value;
    
      Node * temp;
      temp = first;
      while (temp != NULL)
      {
        pos++;
        if (temp->info == value)
        {
          flag = true;
          cout << "Element" << value << "is Found at " << pos << " Position";
          return;
        }
        temp = temp->next;
      }
      if (!flag)
      {
        cout << value << " is not present in the List";
      }
    }
    
    //drive function
    int main()
    {
      List ls;
      int ch;
      while (1)
      {
        cout << "\nChoose an Operation: ";
        cout << "\n1 to CREATE.";
        cout << "\n2 to INSERT.";
        cout << "\n3 to DELETE.";
        cout << "\n4 to SEARCH.";
        cout << "\n5 to DISPLAY.";
        cout << "\n6 to EXIT.";
        cout << "\nEnter Your Choice: ";
        cin >> ch;
    
        switch (ch)
        {
          case 1:
            ls.create();
            break;
          case 2:
            ls.insert();
            break;
          case 3:
            ls.delet();
            break;
          case 4:
            ls.search();
            break;
          case 5:
            ls.display();
            break;
          case 6:
            return 0;
        }
      }
      return 0;
    }

    Output:

    Choose an Operation:
    1 to CREATE.
    2 to INSERT.
    3 to DELETE.
    4 to SEARCH.
    5 to DISPLAY.
    6 to EXIT.
    Enter Your Choice: 1

    Enter an Element: 2

    Choose an Operation:
    1 to CREATE.
    2 to INSERT.
    3 to DELETE.
    4 to SEARCH.
    5 to DISPLAY.
    6 to EXIT.
    Enter Your Choice: 2

    Enter an Element: 4

    Choose the position:
    1 for the beginning.
    2 for the end.
    3 for in between.
    Enter Your Choice: 2

    Choose an Operation:
    1 to CREATE.
    2 to INSERT.
    3 to DELETE.
    4 to SEARCH.
    5 to DISPLAY.
    6 to EXIT.
    Enter Your Choice: 5
    2-->4-->NULL
    Choose an Operation:
    1 to CREATE.
    2 to INSERT.
    3 to DELETE.
    4 to SEARCH.
    5 to DISPLAY.
    6 to EXIT.
    Enter Your Choice: 6

    Similarly, you can follow the option, and then you can search and delete an element from the list.


  • Fascinating Number Program in C

    In this tutorial, we will write a C program to check whether the given number is a Fascinating number or not. You may go through the following topics first in C.

    What is Fascinating Number?

    A number is said to be a fascinating number if it is (having at least 3 digits) multiplied by 2 and 3, and then both these product’s results are concatenated with the original number, then the new number contains all the digits from 1 to 9 exactly once. There could be any number of zeros and are ignored.

    Fascinating number example:

    Consider a Number = 192
    Then multiply it by two and 3
    192 × 2 = 384
    192 × 3 = 576
    Concatenating the above two numbers with the original number, we get:
    “192” + “384” + “576” = 192384576 = result.
    Now the final result contains all the digits from 1 to 9. Hence it is a Fascinating Number.


    Procedure for Fascinating Number

    1. First, check if the entered/given number has three digits or not. If not, print “cannot be a Fascinating Number”.
    2. Else, Multiply the given number with 2 and 3.
    3. Then convert the product result into a string and Concatenate those strings along with the original number.
    4. Iterate the string that we get after concatenation and also keep the frequency count of the digits.
    5. Print “Not a Fascinating Number” if any of the digits (1 to 9) is missing or repeated.
    6. Else, print “It is a Fascinating Number”.

    C Program for Fascinating Number

    #include <stdio.h>
    #include <stdbool.h>
    #include <math.h>
    
    //function
    int countDigits(int n)
    {
      int count = 0;
      while (n > 0)
      {
        count++;
        n /= 10;
      }
    
      return count;
    }
    
    int main(void)
    {
      int num;
    
      printf("Enter a number: ");
      scanf("%d", &num);
    
      //calculating the product
      int product2 = num * 2;
      int product3 = num * 3;
    
      //Concatinating all three
      int concatNum = 0;
      concatNum += num;
      concatNum = concatNum* pow(10, countDigits(product2)) + product2;
      concatNum = concatNum* pow(10, countDigits(product3)) + product3;
    
      //Count the occurence
      int count[10] = { 0 };
      while (concatNum > 0)
      {
        count[concatNum % 10]++;
        concatNum = concatNum / 10;
      }
    
      //Check for 1
      bool isFound = true;
      for (int i = 1; i < 10; i++)
      {
        if (count[i] != 1)
        {
          isFound = false;
          break;
        }
      }
    
      //Display the result
      if (isFound)
        printf("%d is a Fascinating number.", num);
      else
        printf("%d is NOT a Fascinating number.", num);
    
      return 0;
    }

    Output:

    Enter a number: 192
    192 is a Fascinating number.


  • Peterson Number in C

    In this tutorial, we will learn about the Peterson number and check if the number is Peterson or not in C.

    What is Peterson Number?

    A number is said to be a Peterson number if the sum of factorials of each digit of the number is equal to the number itself.

    Peterson numbers examples:

    Number = 145
    145 = !1 + !4 + !5
    = 1 + 4*3*2*1 + 5*4*3*2*1
    = 1 + 24 + 120
    = 145 = 145

    Therefore, we can see that after the sum of the factorial of 145 is equal to the number 145 itself. Hence the Peterson Number.


    C Program to check whether the number is Peterson Number or not

    Peterson number program in C:

    #include <stdio.h>
    
    int main()
    {
      int num, temp, rem, sum = 0, fact = 1;
      int i;
    
      printf("Enter a number: ");
      scanf("%d", &num);
    
      temp = num;
      while (temp != 0)
      {
        rem = temp % 10;
        for (i = 1; i <= rem; i++)
          fact *= i;
    
        sum += fact;
        fact = 1;
        temp /= 10;
      }
    
      //display
      if (num == sum)
        printf("%d is a Peterson Number", num);
      else
        printf("%d is Not a Peterson Number", num);
    
      return 0;
    }

    Output 1:

    Enter a number: 145
    145 is a Peterson number

    Output 2:

    Enter a number: 55
    55 is not a Peterson number


  • Peterson Number in C++

    In this tutorial, we will learn about the Peterson number and write a C++ program to check if the given number is a Peterson number or not.

    What is Peterson Number?

    A number is said to be a Peterson number if the sum of factorials of each digit of the number is equal to the number itself.

    Peterson numbers example:

    Number = 145
    145 = !1 + !4 + !5
    = 1 + 4*3*2*1 + 5*4*3*2*1
    = 1 + 24 + 120
    = 145 = 145

    Therefore, we can see that after the sum of the factorial of 145 is equal to the number 145 itself. Hence the Peterson Number.


    Cpp program to check a number is Peterson number or not

    #include <iostream>
    using namespace std;
    
    int peterson_func(int num)
    {
      int rem, sum = 0, fact = 1, i;
    
      while (num != 0)
      {
        rem = num % 10;
    
        for (i = 1; i <= rem; i++)
          fact *= i;
    
        sum += fact;
        fact = 1;
        num /= 10;
    
      }
    
      return sum;
    }
    
    int main()
    {
      int num, sum = 0, temp;
    
      cout << "Enter a number: ";
      cin >> num;
    
      temp = num;
      sum = peterson_func(num);
    
      if (sum == temp)
        cout << num << " is a Peterson number";
      else
        cout << num << " is NOT a Peterson number";
    
      return 0;
    }

    Output:

    Enter a number: 145
    145 is a Peterson number

    Enter a number: 55
    55 is not a Peterson number


  • User Defined Function in C

    In this tutorial, we will learn abt the user-defined function in C programming language. Let us start by understanding what is user-defined function in C.

    A function refers to a block of code that performs a specific task.

    User-defined functions are the ones that are defined by the users according to their needs and they perform the task assigned by the users.

    Let us say that you want to calculate the area of a circle and also find the circumference of that circle, you can do so by creating two different functions and pass the radius value to those functions. The two functions could be:

    • circleArea()
    • cricleCircumference()

    Example of a simple C user-defined function. The program has a separate function to perform the addition of two numbers.

    #include <stdio.h>
    
    // function prototype
    int addFunction(int x, int y);
    
    int main()
    {
      int num1, num2, result;
    
      printf("Enters the first number: ");
      scanf("%d", &num1);
    
      printf("Enters the second number: ");
      scanf("%d", &num2);
    
      result = addFunction(num1, num2);   // function call
      printf("Result: %d", result);
    
      return 0;
    }
    
    int addFunction(int x, int y)	// function definition   
    {
      int sum;
      sum = x + y;
      return sum;	// return statement
    }

    Output:

    Enters the first number: 5
    Enters the second number: 8
    Result: 13

    Considering the above example, let us go through the following topics:

    • function prototype/declaration
    • function definition
    • function call
    • return statement

    Function Prototype

    Declaration of user-defined function in C.

    A function prototype in C is simply a declaration of a function before the main function that does not contain the body of the function. It only specifies the name of the function, its parameter, and the return type of the function.

    Syntax of function prototype:

    return_type function_name(data_type argument1, data_type argument2, ...);

    This line in a program alerts the compiler that the function may be used later in the program.

    In the above program, int addFunction(int x, int y); is the function prototype.

    Note: Also note that, if you defined the function before the main function then the separate function declaration is not required in a program else you need to mention the declaration. Example: In the syntax below, a declaration is not required

    #include <stdio.h>
    
    //user-defined function
    int sum(int a, int b = 5)
    {
    
       ...........
       ...........
    
     
       return result;
    }
    
    //Main function
    int main()
    {
       .............
       .............
    
       // calling a function
       result = sum(a, b);
       ............
       .............
    
       return 0;
    }

    Function Definition

    Defining a function in C: It is the expansion of function declaration or prototype. It contains the codes within the curly braces {} that perform some specific task.

    How to define functions in c:

    return_type function_name( parameter_list ) {
       body of the function;
    }

    Explanation of the above syntax:

    • Return type: A function may return a value unless it is a void function, which does not return any type of value. return_type is a data type of the function. It could return arithmetic values (int, float, etc.), pointers, structures, etc.
    • Function name: Function name refers to identifiers through which function can be identified uniquely. It is a function’s name that is stated to function by following some naming rules.
    • Parameter list: Parameter is also referred to as Argument. We can pass the values through the parameter when the function is invoked. The parameter list refers to the type, order, and number of the parameters of a function
    • Function Body: The function body is enclosed within the curly braces'{}’. It defines the task of a method within the curly braces.

    Example:

    /* function that returns the minimum value
    between two number passed*/
    
    int min(int num1, int num2) 
    {
       int finalValue;
     
       if (num1 > num2)
          finalValue = num1;
       else
          finalValue = num2;
     
       return finalValue;
      //return_type is int
    }

    Function Call

    We call the function by its name and pass the argument(if any) where we want to execute that function. The syntax for calling a function:

    function_name(argument_list);

    In the above program, result = addFunction(num1, num2);is the function call.


    Passing arguments to a function

    In C programming, most of the time we create a function or declare a function that takes and arguments. We call the function by passing a value or more than one value that is referred to as an argument.

    These arguments are the variable that is passed while calling a function.

    In the example above, result = addFunction(num1, num2); is the function call and num1 and num2 are the argument that are passed.


    Return statement

    The return statement in the function is used to terminate the function and return the value to the program line where it is called. The execution control is transferred to the calling function in the program.

    Syntax:

    return (expression);

    In the above program, return sum; is the return statement of the user defined function.

    However, a function can also be called without passing an argument.


    Go through the C program below to practice user defined functions programs in C and also learn how to pass an argument in a function.


  • Sunny Number Program in C

    Let us go through a C program to check whether the number is Sunny Number or Not.

    What is Sunny Number?

    A number N is said to be a sunny number if the number next to the given number (N+1) is a perfect square.

    Example: Let us take a Number 8, then the next number is 8+1=9 and as 3 is a square root of 9, hence 8 is a sunny Number.

    Another example, let the number be 5, then 5+1=6, that has no square roots, hence 5 is not a sunny Number.


    Program to check whether the number is Sunny Number or Not.

    #include <stdio.h>
    #include <math.h>
    #include <stdbool.h >	//for the use of boolean type
    
    bool is_sunny(int);
    
    int main(void)
    {
      int n;
      printf("Enter the number: ");
      scanf("%d", &n);
    
      if (is_sunny(n))
        printf("%d is a sunny number.", n);
      else
        printf("%d is not a sunny number.", n);
    
      return 0;
    
    }
    
    bool is_sunny(int n)
    {
      int square = sqrt(n + 1);
    
      return (square *square == n);
    }

    Output:

    Enter the number: 16
    16 is a sunny number.


    Display all the sunny number within a range of 0 to 100 in C programming

    #include <stdio.h>
    #include <math.h>
    #include <stdbool.h >	//for the use of boolean type
    
    bool is_sunny(int);
    
    int main(void)
    {
      int n;
    
      printf("List of sunny number from 0 to 100 range: \n");
      for (n = 0; n < 100; ++n)
      {
        if (is_sunny(n))
          printf("%d\n", n);
      }
    
      return 0;
    
    }
    
    bool is_sunny(int n)
    {
      int square = sqrt(n + 1);
    
      return (square *square == n);
    }

    Output:

    List of sunny number from 0 to 100 range:
    1
    4
    9
    16
    25
    36
    49
    64
    81


  • Sunny Number Program in C++

    In this tutorial, we will write a C++ program to check for sunny numbers.

    What is Sunny Number?

    A number N is said to be a sunny number if the number next to the given number (N+1) is a perfect square.

    Example: Let us take a Number 8, then the next number is 8+1=9 and as 3 is a square root of 9, hence 8 is a sunny Number.

    Another example, let the number be 5, then 5+1=6, that has no square roots, hence 5 is not a sunny Number.


    Sunny Number Program in C++

    Question: Check if the given number is a sunny number or not in C++ programming.

    #include <iostream>
    #include <cmath>
    
    using namespace std;
    
    bool is_sunny(int);   //function prototype
    
    int main()
    {
      int num;
    
      cout << "Enter the number: ";
      cin >> num;
    
      if (is_sunny(num))
        cout << num << " is a sunny number";
      else
        cout << num << " is NOT a sunny number";
    
      return 0;
    }
    
    bool is_sunny(int n)
    {
      // find the square root
      int square = sqrt(n + 1);
    
      return (square *square == n);
    }

    Output:

    Enter the number: 81
    81 is a sunny number


  • Happy Number Program in C

    In this tutorial, we will write a C program to check if the number is happy number or not. Let us start by defining a happy number.

    Happy Number

    A number is said to be a happy number if it yields 1 after a few steps and each step is the sum of the squares of the digits of its results. The sum of the squares starts with the given number and till it reaches one.

    Follow the diagram below:

    happy number
    Happy Number

    Explanation: In the above diagram input is 19. Then the digits 1 and 9 are squared which gives the result 82. After that, the digits 8 and 2 are squared and yield the results 68 and this continues till the final result is 1. And then we can say that 19 is a happy number, if not then it is not a happy number.


    Happy Number Program in C

    #include <stdio.h>
    
    int happyNumber(int);
    
    int main()
    {
      int num = 82;
    
      printf("Enter the number: ");
      scanf("%d", &num);
    
      int temp = num;
    
      while (temp != 1 && temp != 4)
      {
        temp = happyNumber(temp);
      }
    
      //check for 1    
      if (temp == 1)
        printf("%d is a happy number", num);
      else if (temp == 4)
        printf("%d is NOT a happy number", num);
    
      return 0;
    }
    
    //user defined function
    int happyNumber(int num)
    {
      int rem = 0, sum = 0;
    
      //Calculation    
      while (num > 0)
      {
        rem = num % 10;
        sum = sum + (rem *rem);
        num /= 10;
      }
    
      return sum;
    }

    Output:

    Enter the number: 32
    32 is a happy number