Blog

  • Happy Number Program in C++

    In this tutorial, we will write a C++ program to check happy number. 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++

    Question: C++ program to check whether a number is happy or not using function

    #include <iostream>
    using namespace std;
    
    //Calculation
    int check(int num)
    {
      int rem = 0, sum = 0;
      while (num > 0)
      {
        rem = num % 10;
        sum = sum + (rem *rem);
        num /= 10;
      }
    
      return sum;
    }
    
    //Function to check
    void isHappyNumber(int num)
    {
      int result = num;
      while (result != 1 && result != 4)
      {
        result = check(result);
      }
    
      if (result == 1)
        cout << num << " is a happy number";
      else
      if (result == 4)
        cout << num << " is NOT a happy number";
    }
    
    //Drive function
    int main()
    {
      int num;
    
      cout << "Enter the number: ";
      cin >> num;
    
      isHappyNumber(num);
      return 0;
    }

    Output:

    Enter the number: 15
    15 is not a happy number

    //Another Execution
    Enter the number: 32
    32 is a happy number


  • Automorphic Number program in C++

    In this article, we will write a C++ Program for Automorphic Number. The following C++ programming topic is used in the program below.

    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

    Automorphic Number Program in C++

    The C++ program to check automorphic number below takes the number from the user as input and checks accordingly.

    #include <iostream>
    using namespace std;
    
    int main()
    {
      int num, flag = 0, sqr;
    
      //user input
      cout << "Enter the number: ";
      cin >> num;
    
      sqr = num * num;
      int temp = num;
    
      //calculation
      while (temp > 0)
      {
        if (temp % 10 != sqr % 10)
        {
          flag = 1;
          break;
        }
    
        temp /= 10;
        sqr /= 10;
      }
    
      if (flag == 1)
        cout << num << ": Not an Automorphic number.";
      else
        cout << num << ": Automorphic number.";
      return 0;
    }

    Output:

    Enter the number: 25
    25: Automorphic number.

    //second execution
    Enter the number: 95
    95: Not an Automorphic number.


  • Automorphic Number Program in C

    In this article, we will write a C Program for Automorphic Number. The following C programming topic is used in the program below.

    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

    Automorphic Number Program in C

    The C program to check automorphic number below takes the number from the user as input and checks accordingly.

    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    int main()
    {
      int num;
      printf("Enter the number: ");
      scanf("%d", &num);
    
      int sqr, temp, last;
      int count = 0;
    
      sqr = num * num;
      temp = num;
    
      //Counting digits
      while (temp > 0)
      {
        count++;
        temp = temp / 10;
      }
    
      //calculation
      int den = floor(pow(10, count));
      last = sqr % den;
    
      //display
      if (last == num)
        printf("%d is an Automorphic number.", num);
      else
        printf("%d Not Automorphic number.", num);
    
      return 0;
    }

    Output:

    Enter the number: 25
    25 is an Automorphic number.


  • C Program to Reverse a Number using for loop

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


    C program to reverse a number using for loop

    This is a reverse number program in C using for loop where the program takes the input from the user. And then iterating through for loop, the program calculates the reverse of a number and then finally displays the result.

    #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

    As you can see the initialization part in for loop is left empty and only the condition checking and increment/decrement is filled within “()” in reverse number in c using for loop.


  • C Program to Find the Frequency of Characters in a String

    Here, we will write a C program to find the occurrence of a character in a string. Before that, you may go through the following topic in C.

    We will look at two examples to calculate the frequency of characters in a string.

    1. For a particular character.
    2. For all the character in a string

    C Program to Find the Frequency of Characters in a String (for a particular character)

    Both the string and the character whose frequency needed to be found are entered by the user.

    The questions: write a program in C to find the frequency of any given character in a given string

    #include <stdio.h>
    
    int main()
    {
      char str[100], ch;
      int count = 0, i;
    
      printf("Enter the string: ");
      fgets(str, sizeof(str), stdin);
    
      printf("Enter a character whose frequency is to be found: ");
      scanf("%c", &ch);
    
      for (i = 0; str[i] != '\0'; ++i)
      {
        if (ch == str[i])
          ++count;
      }
    
      printf("\nFrequency of %c = %d", ch, count);
      return 0;
    }

    Output:

    Enter the string: This is simple2code.com
    Enter a character whose frequency is to be found: i

    Frequency of i = 3


    C Program to Find the Frequency of Characters in a String (for all the characters)

    The user needs to enter the string and the number of occurrences for each character will be displayed.

    #include <stdio.h>
    #include <string.h>
    
    int main()
    {
      char str[100], ch;
      int i, j, k, len, count = 0;
    
      printf("Enter the string: ");
      scanf("%s", str);
    
      len = strlen(str);
    
      for (i = 0; i < len; i++)
      {
        ch = str[i];
        for (j = 0; j < len; j++)
        {
          if (ch == str[j])
          {
            count++;
            for (k = j; k < (len - 1); k++)
              str[k] = str[k + 1];
    
            len--;
            str[len] = '\0';
            j--;
          }
        }
    
        printf("Frequency of %c = %d\n", ch, count);
        count = 0;
        i--;
      }
    
      return 0;
    }

    Output:

    Enter the string: simple2code.com
    Frequency of s = 1
    Frequency of i = 1
    Frequency of m = 2
    Frequency of p = 1
    Frequency of l = 1
    Frequency of e = 2
    Frequency of 2 = 1
    Frequency of c = 2
    Frequency of o = 2
    Frequency of d = 1
    Frequency of . = 1


  • C Program to Convert Uppercase to Lowercase and Vice Versa in a string

    In this tutorial, we will learn to write a C program to convert uppercase to lowercase and vice versa. Before that, you may go through the following topic in C.


    C Program to Convert Uppercase String to Lowercase String

    Explanation: The program asks the user to enter a string. The string can be fully uppercase or partially uppercase. The program converts that string to a fully uppercase string.

    We have used for loop to iterate through the string and also, we know that a is equal to 97 and A is equal to 65 (97-32).

    #include <stdio.h>
    #include <string.h>
    
    int main()
    {
      char str[25];
      int i;
      printf("Enter the string: ");
      scanf("%s", str);
    
      for (i = 0; i <= strlen(str); i++)
      {
        if (str[i] >= 65 && str[i] <= 90)
          str[i] = str[i] + 32;
      }
    
      printf("Conversion to Lower Case String: %s", str);
      return 0;
    }

    Output:

    Enter the string: sIMpLe2CoDE
    Conversion to Lower Case String: simple2code


    C Program to Convert Lowercase String to Uppercase String

    Explanation: The program asks the user to enter a string. The string can be fully lowercase or partially lowercase. The program converts that string to a fully lowercase string.

    We have used for loop to iterate through the string and also, we know that A is equal to 65 and a is equal to 97 (65+32).

    #include <stdio.h>
    #include <string.h>
    
    int main()
    {
      char str[25];
      int i;
      printf("Enter the string: ");
      scanf("%s", str);
    
      for (i = 0; i <= strlen(str); i++)
      {
        if (str[i] >= 97 && str[i] <= 122)
          str[i] = str[i] - 32;
      }
    
      printf("Conversion to uppercase String: %s", str);
      return 0;
    }

    Output:

    Enter the string: simPle2Code.com
    Conversion to Uppercase String: SIMPLE2CODE.COM


  • Java Character Pattern Program

    Java Character Pattern Program

    In this tutorial, we will go through alphabet pattern programs in java programming. Before that, you may go through the following topic in java.

    Java Character Pattern Program

    import java.util.Scanner;
    
    public class Main
    {
      public static void main(String args[])
      {
        int rows, i, j, k;
        Scanner sc = new Scanner(System.in);
    
        System.out.print("Enter the no. of rows: ");
        rows = sc.nextInt();
    
        for (i = 0; i <= rows; i++)
        {
          int alphabet = 65;
          for (j = 0; j < i; j++)
            System.out.print(" ");
    
          for (k = i; k <= rows; k++)
            System.out.print((char)(alphabet + k)+" ");
    
          System.out.println();
        }
        for (i = rows; i >= 0; i--)
        {
          int alphabet = 65;
          for (j = 0; j < i; j++)
            System.out.print(" ");
    
          for (k = i; k <= rows; k++)
            System.out.print((char)(alphabet + k)+" ");
    
          System.out.println();
        }
      }
    }

    Output:

    Enter the no. of rows: 5
    A B C D E F
     B C D E F
      C D E F
       D E F
        E F
         F
         F
        E F
       D E F
      C D E F
     B C D E F
    A B C D E F

  • Right Angle Triangle Character Pattern in Java: Pattern 5

    Right Angle Triangle Character Pattern in Java: Pattern 5

    In this tutorial, we will write a program to display a right-angle triangle pattern in java using the alphabet. Before that, you may go through the following topic in java.

    The program below takes the user input from the user for the number of rows and display the right triangle character pattern in various character orders.

    Right angled triangle pattern

    import java.util.Scanner;
    
    public class Main
    {
      public static void main(String args[])
      {
        int rows, i, j;
        int ch = 64;
        Scanner sc = new Scanner(System.in);
    
        System.out.print("Enter the number of rows: ");
        rows = sc.nextInt();
    
        System.out.print("Output:\n");
        for (i = 1; i <= rows; i++)
        {
          for (j = i; j >= 1; j--)
            System.out.print((char)(j + ch)+" ");
    
          System.out.println();
        }
      }
    }

    Output:

    Enter the number of rows: 5
    Output:
    A
    B A
    C B A
    D C B A
    E D C B A


  • Right Angle Triangle Character Pattern in Java: Pattern 4

    Right Angle Triangle Character Pattern in Java: Pattern 4

    In this tutorial, we will write a program to display a right-angle triangle pattern in java using the alphabet. Before that, you may go through the following topic in java.

    The program below takes the user input from the user for the number of rows and display the right triangle character pattern in various character orders.

    Right angled triangle pattern

    import java.util.Scanner;
    
    public class Main
    {
      public static void main(String args[])
      {
        int rows, i, j;
        int ch = 64;
        Scanner sc = new Scanner(System.in);
    
        System.out.print("Enter the number of rows: ");
        rows = sc.nextInt();
    
        System.out.print("Output:\n");
        for (i = rows; i >= 1; i--)
        {
          for (j = i; j <= rows; j++)
            System.out.print((char)(j + ch)+" ");
    
          System.out.println();
        }
      }
    }

    Output:

    Enter the number of rows: 5
    Output:
    E
    D E
    C D E
    B C D E
    A B C D E


  • Right Angle Triangle Character Pattern in Java: Pattern 3

    Right Angle Triangle Character Pattern in Java: Pattern 3

    In this tutorial, we will write a program to display a right-angle triangle pattern in java using the alphabet. Before that, you may go through the following topic in java.

    The program below takes the user input from the user for the number of rows and display the right triangle character pattern in various character orders.

    Right angled triangle pattern

    import java.util.Scanner;
    
    public class Main
    {
      public static void main(String args[])
      {
        int rows, i, j;
        int ch = 64;
        Scanner sc = new Scanner(System.in);
    
        System.out.print("Enter the number of rows: ");
        rows = sc.nextInt();
    
        System.out.print("Output:\n");
        for (i = rows; i >= 1; i--)
        {
          for (j = rows; j >= i; j--)
            System.out.print((char)(j + ch)+" ");
    
          System.out.println();
        }
      }
    }

    Output:

    Enter the number of rows: 5
    Output:
    E
    E D
    E D C
    E D C B
    E D C B A