কম্পিউটার

দুটি সংখ্যার GCD খুঁজে পেতে জাভা প্রোগ্রাম


এই নিবন্ধে, আমরা জানব কিভাবে জাভাতে দুটি সংখ্যার GCD খুঁজে বের করতে হয়। দুটি সংখ্যার সর্বশ্রেষ্ঠ সাধারণ ভাজক (GCD) হল বৃহত্তম সংখ্যা যা উভয়কে ভাগ করে।

নীচে একই -

এর একটি প্রদর্শন রয়েছে৷

ইনপুট

ধরুন আমাদের ইনপুট হল −

Value_1 : 18
Value_2 : 24

আউটপুট

কাঙ্খিত আউটপুট হবে −

GCD of the two numbers : 6

অ্যালগরিদম

Step1- Start
Step 2- Declare three integers: input_1, inpur_2 and gcd
Step 3- Prompt the user to enter two integer value/ Hardcode the integer
Step 4- Read the values
Step 5- Check that the number divides both (x and y) numbers completely or not. If
divides completely store it in a variable.
Step 6- Display the ‘i’ value as GCD of the two numbers
Step 7- Stop

উদাহরণ 1

এখানে, একটি প্রম্পটের উপর ভিত্তি করে ব্যবহারকারী দ্বারা ইনপুট প্রবেশ করানো হচ্ছে। আপনি আমাদের কোডিং গ্রাউন্ড টুলে এই উদাহরণটি লাইভ চেষ্টা করতে পারেন দুটি সংখ্যার GCD খুঁজে পেতে জাভা প্রোগ্রাম

import java.util.Scanner;
public class GCD{
   public static void main(String[] args){
      int input_1 , input_2 , gcd ;
      Scanner reader = new Scanner(System.in);
      System.out.println("A reader object has been defined ");
      System.out.print("Enter a first number: ");
      input_1 = reader.nextInt();
      System.out.print("Enter a second number: ");
      input_2 = reader.nextInt();
      gcd = 1;
      for(int i = 1; i <= input_1 && i <= input_2; i++){
         if(input_1%i==0 && input_2%i==0)
         gcd = i;
      }
      System.out.printf("\nThe GCD of %d and %d is: %d", input_1, input_2, gcd);
   }
}

আউটপুট

A reader object has been defined
Enter a first number: 24
Enter a second number: 18
The GCD of 24 and 18 is: 6

উদাহরণ 2

এখানে, পূর্ণসংখ্যা পূর্বে সংজ্ঞায়িত করা হয়েছে, এবং এর মান অ্যাক্সেস করা হয়েছে এবং কনসোলে প্রদর্শিত হয়েছে।

public class GCD{
   public static void main(String[] args){
      int input_1 , input_2 , gcd ;
      input_1 = 12;
      input_2 = 18;
      gcd = 1;
      System.out.print("The first number is " + input_1);
      System.out.print("\nThe second number is " + input_2);
      for(int i = 1; i <= input_1 && i <= input_2; i++){
         if(input_1%i==0 && input_2%i==0)
         gcd = i;
      }
      System.out.printf("\nThe GCD of %d and %d is: %d", input_1, input_2, gcd);
   }
}

আউটপুট

The first number is 24
The second number is 18
The GCD of 24 and 18 is: 6

  1. দুটি সংখ্যার LCM খুঁজুন

  2. দুটি সংখ্যার যোগফল এবং পার্থক্য বের করার জন্য সি প্রোগ্রাম

  3. পুনরাবৃত্ত ফাংশন ব্যবহার করে সংখ্যার GCD খুঁজে বের করতে C প্রোগ্রাম

  4. জাভা প্রোগ্রাম স্কোয়ারের এলাকা খুঁজে বের করতে