Author: admin

  • Half Pyramid of Alphabets in C

    In this tutorial, we will learn and code alphabet patterns in C programming language specifically the Half pyramid of alphabets in C programming. However, in this tutorial, we will create a pyramid character pattern in C using for loop. So you may go through the following topic in C.

    Pattern 1: C program for character/alphabet.

    This program asks for the alphabet till which you want to print.

    Enter the character till which you want to print in uppercase: F
    A
    B B
    C C C
    D D D D
    E E E E E
    F F F F F F

    Source code:

    #include <stdio.h>
    
    int main()
    {
      int i, j;
      char ch, alphabet = 'A';
    
      printf("Enter the character till which you want to print in uppercase: ");
      scanf("%c", &ch);
    
      for (i = 1; i <= (ch - 'A' + 1); ++i)
      {
        for (j = 1; j <= i; ++j)
          printf("%c ", alphabet);
    
        ++alphabet;
        printf("\n");
      }
    
      return 0;
    }

    Pattern 2: C program for character/alphabet.

    This program asks the user for the number of rows that you want.

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

    Source code:

    #include <stdio.h>
    
    void main()
    {
      int rows, i, j;
      char ch = 'A';
    
      printf("Enter the no. of rows: ");
      scanf("%d", &rows);
    
      for (i = 0; i <= rows; i++)
      {
        for (j = 0; j <= i; j++)
        {
          printf(" %c", (char)(ch + i));
        }
        printf("\n");
      }
    
      getch();
    }

    Pattern 3: C program for character/alphabet.

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

    Source code:

    #include <stdio.h>
    
    void main()
    {
      int rows, i, j;
    
      printf("Enter the no. of rows: ");
      scanf("%d", &rows);
    
      // ASCII value of alphabet 'A'
      int alphabet = 65;
    
      for (i = 0; i < rows; i++)
      {
        for (j = 0; j <= i; j++)
        {
          printf("%c ", (char)(alphabet + j));
        }
        printf("\n");
      }
    
      getch();
    }

    Pattern 4: C program for character/alphabet.

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

    Source code:

    #include <stdio.h>
    
    void main()
    {
      int rows, i, j;
    
      printf("Enter the no. of rows: ");
      scanf("%d", &rows);
    
      // ASCII value of alphabet 'A'
      int alphabet = 65;
    
      for (i = rows - 1; i >= 0; i--)
      {
        for (j = rows - 1; j >= i; j--)
        {
          printf("%c ", (char)(alphabet + j));
        }
        printf("\n");
      }
    
      getch();
    }

    Pattern 5: C program for character/alphabet.

    Enter the no. of rows: 5
    A
    BB
    CCC
    DDDD
    EEEEE
    FFFFFF

    Source code:

    #include <stdio.h>
    
    void main()
    {
      int rows, i, j, k;
    
      printf("Enter the no. of rows: ");
      scanf("%d", &rows);
    
      // ASCII value of alphabet 'A'
      int alphabet = 65;
    
      for (i = 0; i <= rows; i++)
      {
        for (j = 1; j <= rows - i; j++)
          printf(" ");
    
        for (k = 0; k <= i; k++)
          printf("%c", (char)(i + alphabet));
    
        printf("\n");
      }
    
      getch();
    }

    Pattern 6: C program for character/alphabet.

    Source code:

    #include <stdio.h>
    
    void main()
    {
      int rows, i, j;
    
      printf("Enter the no. of rows: ");
      scanf("%d", &rows);
    
      // ASCII value of alphabet 'A'
      int alphabet = 65;
    
      for (i = 1; i <= rows; i++)
      {
        int count = rows - 1;
        int temp = i;
    
        for (j = 1; j <= i; j++)
        {
          printf("%4c", (char) temp + alphabet - 1);
          temp = temp + count;
          count--;
        }
    
        printf("\n");
      }
    
      getch();
    }

    Pattern 7: C program for character/alphabet.

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

    Source code:

    #include <stdio.h>
    
    void main()
    {
       int i, j, rows;
    
       printf("Enter the no. of rows: ");
       scanf("%d", &rows);
    
       for (i = 1; i <= rows; i++)
       {
          for (j = 1; j <= i; j++)
          {
             printf("%c ", (char)(rows - i + 1 + 64));
          }
          printf("\n");
       }
    }

    Pattern 8: C program for character/alphabet.

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

    Source code:

    #include <stdio.h>
    
    void main()
    {
       int i, j, rows;
    
       printf("Enter the no. of rows: ");
       scanf("%d", &rows);
    
       for (i = 1; i <= rows; i++)
       {
          for (j = i; j >= 1; j--)
             printf("%c ", (char)(j + 64));
    
          printf("\n");
       }
    }

    Pattern 9: C program for character/alphabet.

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

    Source code:

    #include <stdio.h>
    
    void main()
    {
       int i, j, k, rows;
    
       printf("Enter the no. of rows: ");
       scanf("%d", &rows);
    
       for (i = 1; i <= rows; i++)
       {
          k = i;
          for (j = 1; j <= i; j++, k++)
             printf("%c ", (char)(k + 64));
    
          printf("\n");
       }
    }

    Pattern 10: C program for character/alphabet.

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

    Source code:

    #include <stdio.h>
    
    void main()
    {
       int i, j, rows;
    
       printf("Enter the no. of rows: ");
       scanf("%d", &rows);
    
       for (i = rows; i >= 1; i--)
       {
          for (j = i; j <= rows; j++)
             printf("%c ", (char)(j + 64));
    
          printf("\n");
       }
    }

    Pattern 11: C program for character/alphabet.

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

    Source code:

    #include <stdio.h>
    
    void main()
    {
       int i, j, rows;
    
       printf("Enter the no. of rows: ");
       scanf("%d", &rows);
    
       for (i = 1; i <= rows; i++)
       {
          for (j = 1; j <= (i *2 - 1); j++)
             printf("%c ", (char)(j + 64));
    
          printf("\n");
       }
    }

    Pattern 12: C program for character/alphabet.

    Enter the no. of rows: 5
    A
    B C
    D E F
    G H I J
    K L M N O

    Source code:

    #include <stdio.h>
    
    void main()
    {
       int i, j, k = 1, rows;
    
       printf("Enter the no. of rows: ");
       scanf("%d", &rows);
    
       for (i = 1; i <= rows; i++)
       {
          for (j = 1; j <= i; j++, k++)
             printf("%3c", (char)(k + 64));	//3 = TO GIVE FOUR SPACES
    
          printf("\n");
       }
    }

    Pattern 13: C program for character/alphabet.

    Enter the no. of rows: 5
    a
    B c
    D e F
    g H i J
    k L m N o

    Source code:

    #include <stdio.h>
    
    void main()
    {
       int i, j, k = 0, l = 1, rows;
    
       printf("Enter the no. of rows: ");
       scanf("%d", &rows);
    
       for (i = 1; i <= rows; i++)
       {
          for (j = 1; j <= i; j++, k++, l++)
          {
             if (k % 2 == 0)
                printf("%3c", (char)(l + 96));
             else
                printf("%3c", (char)(l + 64));
          }
          printf("\n");
       }
    }

  • Inverted Half Pyramid Pattern of Alphabets in C

    In this tutorial, we will write a C program to print half Pyramid using alphabets/characters. Before that, you may go through the following topic in C.

    Pattern 1: C program for character/alphabet.

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

    Source code:

    #include <stdio.h>
    
    void main()
    {
       int i, j, rows;
    
       printf("Enter the no. of rows: ");
       scanf("%d", &rows);
    
       for (i = 1; i <= rows; i++)
       {
          for (j = i; j <= rows; j++)
             printf("%c ", (char)(i + 64));
    
          printf("\n");
       }
    }

    Pattern 2: C program for character/alphabet.

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

    Source code:

    #include <stdio.h>
    
    void main()
    {
       int i, j, rows;
    
       printf("Enter the no. of rows: ");
       scanf("%d", &rows);
    
       for (i = rows; i >= 1; i--)
       {
          for (j = 1; j <= i; j++)
             printf("%c ", (char)(i + 64));
    
          printf("\n");
       }
    }

    Pattern 3: C program for character/alphabet.

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

    Source code:

    #include <stdio.h>
    
    void main()
    {
       int i, j, rows;
    
       printf("Enter the no. of rows: ");
       scanf("%d", &rows);
    
       for (i = 1; i <= rows; i++)
       {
          for (j = 1; j <= rows - i + 1; j++)
             printf("%c ", (char)(j + 64));
    
          printf("\n");
       }
    }

    Pattern 4: C program for character/alphabet.

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

    Source code:

    #include <stdio.h>
    
    void main()
    {
       int i, j, rows;
    
       printf("Enter the no. of rows: ");
       scanf("%d", &rows);
    
       for (i = rows; i >= 1; i--)
       {
          for (j = i; j >= 1; j--)
             printf("%c ", (char)(j + 64));
    
          printf("\n");
       }
    }

    Pattern 5: C program for character/alphabet.

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

    Source code:

    #include <stdio.h>
    
    void main()
    {
       int rows, k, i, j;
    
       printf("Enter the no. of rows: ");
       scanf("%d", &rows);
    
       for (i = rows; i >= 1; i--)
       {
          k = i;
    
          for (j = 1; j <= i; j++, k++)
             printf("%c ", (char)(k + 64));
    
          printf("\n");
       }
    }

    Pattern 6: C program for character/alphabet.

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

    Source code:

    #include <stdio.h>
    
    void main()
    {
       int rows, i, j;
    
       printf("Enter the no. of rows: ");
       scanf("%d", &rows);
    
       for (i = 1; i <= rows; i++)
       {
          for (j = rows; j >= i; j--)
             printf("%c ", (char)(j + 64));
    
          printf("\n");
       }
    }

    Pattern 7: C program for character/alphabet.

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

    Source code:

    #include<stdio.h>
    
    void main()
    {
        int rows, i, j;
    
        printf("Enter the no. of rows: ");
        scanf("%d",&rows);
    
        for(i=1; i<=rows; i++)
        {
            for(j=i; j<=rows; j++)
                printf("%c ",(char)(j+64));
            
            printf("\n");
        }
    }

  • C++ Program to Multiply two Complex Numbers

    In this tutorial, we will write a C++ program to perform a multiplication on two complex numbers. Before that, you may go through the following topic in java.

    C++ Program to Multiply two Complex Numbers using Struct

    #include <stdio.h>
    
    //structure
    typedef struct COMPLEX
    {
       int a;
       int b;
    }
    
    Complex;
    
    //user-defined function to calculate
    Complex multiplicationFunc(Complex x, Complex y)
    {
       Complex z;
       z.a = x.a *y.a - x.b *y.b;
       z.b = x.a *y.b + x.b *y.a;
       return z;
    }
    
    //Main function
    int main()
    {
       int a1, b1, a2, b2;
       Complex x, y, z;
    
       printf("Enter first complex number (a+bi) : ");
       scanf("%d+%di", &a1, &b1);
       printf("Enter second complex number (a+bi) : ");
       scanf("%d+%di", &a2, &b2);
    
       x.a = a1;
       x.b = b1;
       y.a = a2;
       y.b = b2;
    
       //calling multiplication function
       z = multiplicationFunc(x, y);
    
       //Printing the result
       printf("\nFinal multiplication result: %d+%di", z.a, z.b);
       return 0;
    }

    Output:

    Enter first complex number (a+bi) : 2+3i
    Enter second complex number (a+bi) : 2+3i

    Final multiplication result: -5+12i


  • Java Program to Print Multiplication Table

    In this tutorial, we will write a Java program to print a multiplication table for any number. Before that, you may go through the following topic in java.

    Java Program to Print Multiplication Table using for loop

    Source code: The user needs to enter the number for which the table is to be printed and a program using for loop prints the multiplication table for that number.

    import java.util.Scanner;
    
    public class Main
    {
      public static void main(String[] args)
      {
        Scanner sc = new Scanner(System.in);
    
        System.out.print("Enter the number:");
        int num = sc.nextInt();
    
        System.out.println("Multiplication table for " + num + " number:");
    
        for (int i = 1; i <= 10; i++)
          System.out.println(num + " *" + i + " = " + num *i);
      }
    }

    Output:

    Enter the number:2
    Multiplication table for 2 number:
    2 * 1 = 2
    2 * 2 = 4
    2 * 3 = 6
    2 * 4 = 8
    2 * 5 = 10
    2 * 6 = 12
    2 * 7 = 14
    2 * 8 = 16
    2 * 9 = 18
    2 * 10 = 20


  • C Program to Print Multiplication Table

    In this tutorial, we will write a C program to print a multiplication table for any number. Before that, you may go through the following topic in C.

    C Program to Print Multiplication Table using While loop

    Source code: The user needs to enter the number for which the table is to be printed and a program using while loop prints the multiplication table for that number.

    #include <stdio.h>
    
    int main()
    {
      int num, i = 1;
    
      printf("Enter the Number: ");
      scanf("%d", &num);
    
      printf("Multiplication table of %d:\n\n", num);
    
      while (i <= 10)
      {
        printf(" %d x %d = %d \n ", num, i, num *i);
        i++;
      }
    
      return 0;
    }

    Output:

    Enter the Number: 2
    Multiplication table of 2:

    2 x 1 = 2
    2 x 2 = 4
    2 x 3 = 6
    2 x 4 = 8
    2 x 5 = 10
    2 x 6 = 12
    2 x 7 = 14
    2 x 8 = 16
    2 x 9 = 18
    2 x 10 = 20


  • Static block in Java

    A static block in a program is a block which is associated with a static keyword. The static block executes when the classloader loads the class. A static block is invoked before the main() method in java.

    Syntax:

    .........
    static {
    .......statements
    }
    .......

    Let us go through a static block example.

    Static block in Java with Example

    class StaticTest
    {
    	//static integer is declared
    	static int num;
    
            //static block
    	static
       
    	{
    		num = 100;	//assigned the value
    		System.out.println("This is Static block!");
    	}
    }
    
    //Main driver
    public class Main
    {
    	public static void main(String args[])
    	{
    		//printing the num value, but will be printed atlast
    		System.out.println(StaticTest.num);
    	}
    }

    Output:

    This is Static block!
    100

    As you can see, the static block is executed first, and then the Main function in the above program.


  • Mystery Number in C#

    In this tutorial, we will write a c# program to check if a number is a Mystery Number. Before that, we may go through the following topic in c#.

    A mystery number is a number that can be expressed as the sum of two numbers and those two numbers should be the reverse of each other. It lies between 22 to 198, i.e. 22<=N<=198.

    Example:

    132 = 93+39
    154 = 68 + 86
    176 = 79 + 97


    Program to check Mystery Number in C#

    using System;
    
    class MysteryNumber
    {
        static int reverseNum(int x)
        {
            string s = x.ToString();
            string str="";
            for(int i=s.Length-1;i>=0;i--)
            {
                 
                str=str+s[i];
            }
             
            int rev=Int32.Parse(str);
            return rev;
        }
         
        static bool isMysteryNumber(int n)
        {
            for (int i=1; i <= n/2; i++)
            {
                int j = reverseNum(i);
                if (i + j == n)
                {
                    Console.WriteLine( i + " " + j);
                    Console.WriteLine("It is a Mystery Number");
                    return true;
                }
            }
         
            Console.WriteLine("It is not a Mystery Number");
            return false;
        }
         
        public static void Main()
        {
            int n = 154;
            isMysteryNumber(n);
         
        }
     
    }

    Output:

    59 95
    It is a Mystery Number


  • Mystery Number in C++

    In this tutorial, we will write a C++ program to check if a number is a Mystery Number. Before that, we may go through the following topic in C++.

    A mystery number is a number that can be expressed as the sum of two numbers and those two numbers should be the reverse of each other. It lies between 22 to 198, i.e. 22<=N<=198.

    Example:

    132 = 93+39
    154 = 68 + 86
    176 = 79 + 97


    Program to check Mystery Number in C++

    #include <bits/stdc++.h>
    using namespace std;
    
    int revNum(int str)
    {
      string s = to_string(str);
    
      reverse(s.begin(), s.end());
    
      stringstream ss(s);
      int rev = 0;
      ss >> rev;
    
      return rev;
    }
    
    bool isMysteryNumber(int n)
    {
      for (int i = 1; i <= n / 2; i++)
      {
        int j = revNum(i);
        if (i + j == n)
        {
          cout << i << " " << j;
          return true;
        }
      }
    
      return false;
    }
    
    //main function
    int main()
    {
      int num;
    
      cout << "Enter the number: ";
      cin >> num;
    
      if (isMysteryNumber(num))
        cout << "\n" << num << " is a Mystery number";
      else
        cout << "\n" << num << " is NOT a Mystery number";
    }

    Output:

    Enter the number: 154
    59 95
    154 is a Mystery number


  • Mystery Number in Java

    In this tutorial, we will write a Java program to check if a number is a Mystery Number. Before that, we may go through the following topic in java.

    A mystery number is a number that can be expressed as the sum of two numbers and those two numbers should be the reverse of each other. It lies between 22 to 198, i.e. 22<=N<=198.

    Example:

    132 = 93 + 39
    154 = 68 + 86
    176 = 79 + 97


    Program to check Mystery Number in Java

    import java.util.Scanner;
    
    public class Main
    {
      static int reverseFunction(int n)
      {
        String str = Integer.toString(n);
        String string = "";
        
        for (int i = str.length() - 1; i >= 0; i--)
          string = string + str.charAt(i);
    
        int revResult = Integer.parseInt(str);
        return revResult;
      }
    
      static boolean isMystery(int num)
      {
        for (int i = 1; i <= num / 2; i++)
        {
          int j = reverseFunction(i);
          if (i + j == num)
          {
            System.out.println(i + " " + j);
            System.out.println(num + " is a mystery number.");
            return true;
          }
        }
    
        System.out.println(num + " is NOT a mystery number.");
        return false;
      }
    
      public static void main(String args[])
      {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter a number: ");
    
        int num = sc.nextInt();
        isMystery(num);
      }
    }

    Output:

    Enter a number: 132
    66 66
    132 is a mystery number.


  • Java Program to Find Smallest of Three Numbers Using Ternary Operator

    In this tutorial, we will write a program in java to find minimum of three numbers using conditional operator. Before that, you may go through the following topics in java.

    Smallest of three numbers using ternary operator in java

    import java.util.Scanner;
    
    public class Main
    {
      public static void main(String[] args)
      {
        Scanner scanner = new Scanner(System.in);
        int num1, num2, num3, result;
    
        System.out.print("Enter the first Number:");
        num1 = scanner.nextInt();
        System.out.print("Enter the second Number:");
        num2 = scanner.nextInt();
        System.out.print("Enter the third Number:");
        num3 = scanner.nextInt();
        scanner.close();
    
        result = num3<(num1 < num2 ? num1 : num2) ? num3 : ((num1 < num2) ? num1 : num2);
        System.out.println("Smallest Number is:" + result);
      }
    }

    Output:

    Enter the first Number:20
    Enter the second Number:10
    Enter the third Number:30
    Smallest Number is:10