Programmator
Programmator
  • Home
  • Download
  • Social
    • YouTube
    • Instagram
    • Facebook
    • Telegram
  • Features
    • C/C++
    • Java
      • Category 1
      • Category 2
      • Category 3
      • Category 4
      • Category 5
    • Sub Menu 3
    • Sub Menu 4
  • Contact Us

Write a java program to display alphabets (A to Z) using loop

In this program, we can loop through A to Z using for loop because they are stored as ASCII characters in java.

This java program iterates the ASCII code from 65 to 90, representing the alphabets A to Z and print them.

With a little modification you can display lowercase alphabets to simply replace the A with a and Z with z. In this case, internally your loop through 97 to 122.

  • Program:

class Main {
  public static void main(String[] args) {

    char n;

    for(n = 'A'; n <= 'Z'; ++n) // For lowercase: for(n = 'a'; n <= 'b'; ++n)
      System.out.print(n + " ");
    }
}

  • Output:

A B C D E F G H I J K L M N O P Q R S T U V W X Y Z 


Java program to find LCM of two numbers

In this program, LCM(Least common multiple) of two or more numbers is the least positive number that can be divided by both the numbers, without leaving the remainder. It is also known as Lowest common multiple, Least Common Denominator and smallest common multiple. It is denoted by LCM (a, b) or lcm (a, b) where a and b are two integers.

It is used when we add, subtract, or compare the fractions. While we perform addition or subtraction of the fractions, we find the LCM of the denominators and then solve the fraction. The LCM of the denominator is known as the Least Common Denominator (LCD).

  • Program:

public class Main {
    public static void main(String[] args) {

        int a = 72, b = 120, lcm;

        // maximum number between a and b is stored in lcm
        lcm = (a > b) ? a : b;
        while(true) {
            if( lcm % a == 0 && lcm % b == 0 ) {
                System.out.printf("The LCM of %d and %d is %d.", a, b, lcm);
                break;
            }
            ++lcm;
        }
    }
}

  • Output:

The LCM of 72 and 120 is 360.

In this program, we have covered different logic in java programs to find GCD of two numbers.

We have initialized two numbers n1=11 and n2=102. After that, we have used a for loop that runs from 1 to the smallest of both numbers. It executes until the condition i<=n1 && i<=n2 returns true. Inside the for loop, we have also used if statement that tests the condition (n1%i== && n2%i==0) and returns true if both conditions are satisfied. At last, we have stored the value of i in the variable gcd and print the same gcd variable.

  • Program:

class Main {
  public static void main(String[] args) {
    
    int n1 = 11, n2 = 102; // find GCD between n1 and n2
   
    int gcd = 1; // initially set to gcd

    for (int i = 1; i <= n1 && i <= n2; ++i) {
   
      if (n1 % i == 0 && n2 % i == 0) // check if i perfectly divides both n1 and n2
        gcd = i;
    }

    System.out.println("GCD of " + n1 +" and " + n2 + " is " + gcd);
  }
}

  • Output:

GCD of 11 and 102 is 1

Newer Posts Older Posts Home

ABOUT

I could look back at my life and get a good story out of it. It's a picture of somebody trying to figure things out.

SUBSCRIBE & FOLLOW

POPULAR POSTS

Advertisement

👆 Shopping 👆

Powered by Blogger

Archive

  • January 20231
  • December 20223
  • November 202215
  • August 20222

Report Abuse

Copyright

  • Home
  • About US
  • Privacy Policy
  • Disclaimer

About Me

Nilesh Patel
View my complete profile

Copyright © Programmator. Designed by OddThemes