কম্পিউটার

স্টার প্যাসকেলের ত্রিভুজ প্রিন্ট করার জন্য জাভা প্রোগ্রাম


এই নিবন্ধে, আমরা বুঝব কিভাবে তারা প্যাসকেলের ত্রিভুজ প্রিন্ট করতে হয়। প্যাসকেলের ত্রিভুজটি একাধিক ফর-লুপ এবং প্রিন্ট স্টেটমেন্ট ব্যবহার করে গঠিত হয়। ত্রিভুজের বাইরের সমস্ত মান শূন্য (0) হিসাবে বিবেচিত হয়। প্রথম সারিটি হল 0 1 0 যেখানে শুধুমাত্র 1টি প্যাসকেলের ত্রিভুজে একটি স্থান অর্জন করে, 0 সেকেন্ড অদৃশ্য। দ্বিতীয় সারিটি (0+1) এবং (1+0) যোগ করে অর্জিত হয়। আউটপুট দুটি শূন্যের মধ্যে স্যান্ডউইচ করা হয়। প্রয়োজনীয় স্তর অর্জন না হওয়া পর্যন্ত প্রক্রিয়া চলতে থাকে।

নীচে একই -

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

ইনপুট

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

Enter the number of rows in Pascal's Triangle : 8

আউটপুট

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

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

Enter the number of rows in Pascal's Triangle : 8
The Pascal's Triangle :
        1
       1 1
      1 2 1
     1 3 3 1
    1 4 6 4 1
   1 5 10 10 5 1
  1 6 15 20 15 6 1
 1 7 21 35 35 21 7 1
1 8 28 56 70 56 28 8 1

অ্যালগরিদম

Step 1 - START
Step 2 - Declare three integer values namely i, j and my_input
Step 3 - Read the required values from the user/ define the values
Step 4 - Define a function ‘factorial()’ to compute the factorial of two numbers and a function ‘Combination()’ to compute combination of two numbers
Step 5 - We iterate through two nested 'for' loops to get space between the characters.
Step 6 - After iterating through the innermost loop, we iterate through another 'for' loop. This will help Combination values of ‘i’ and ‘j’.
Step 7 - Now, print a newline to get the specific number of Combination values of ‘i’ and ‘j’ in the subsequent lines.
Step 8 - Display the result
Step 9 - Stop

উদাহরণ 1

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

import java.util.Scanner;
public class PascalsTriangle {
   static int factorial(int my_input) {
      int factors;
      for(factors = 1; my_input > 1; my_input--){
         factors *= my_input;
      }
      return factors;
   }
   static int combination(int my_input,int r) {
      return factorial(my_input) / ( factorial(my_input-r) * factorial(r) );
   }
   public static void main(String args[]){
      System.out.println();
      int my_input, i, j;
      my_input = 5;
      System.out.println("Required packages have been imported");
      Scanner my_scanner = new Scanner(System.in);
      System.out.println("A reader object has been defined ");
      System.out.print("Enter the number of rows in Pascal's Triangle : ");
      my_input = my_scanner.nextInt();
      System.out.println("The Pascal's Triangle : ");
      for(i = 0; i <= my_input; i++) {
         for(j = 0; j <= my_input-i; j++){
            System.out.print(" ");
         }
        for(j = 0; j <= i; j++){
           System.out.print(" "+combination(i, j));
        }
        System.out.println();
     }
   }
}

আউটপুট

Required packages have been imported
A reader object has been defined
Enter the number of rows in Pascal's Triangle : 8
The Pascal's Triangle :
         1
        1 1
       1 2 1
      1 3 3 1
     1 4 6 4 1
    1 5 10 10 5 1
   1 6 15 20 15 6 1
  1 7 21 35 35 21 7 1
 1 8 28 56 70 56 28 8 1

উদাহরণ 2

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

public class PascalsTriangle {
   static int factorial(int my_input) {
      int factors;
      for(factors = 1; my_input > 1; my_input--){
         factors *= my_input;
      }
      return factors;
   }
   static int combination(int my_input,int r) {
      return factorial(my_input) / ( factorial(my_input-r) * factorial(r) );
   }
   public static void main(String args[]){
      System.out.println();
      int my_input, i, j;
      my_input = 8;
      System.out.println("The number of rows in Pascal's Triangle is defined as " +my_input);
      System.out.println("The Pascal's Triangle : ");
      for(i = 0; i <= my_input; i++) {
         for(j = 0; j <= my_input-i; j++){
            System.out.print(" ");
         }
         for(j = 0; j <= i; j++){
            System.out.print(" "+combination(i, j));
         }
         System.out.println();
      }
   }
}

আউটপুট

The number of rows in Pascal's Triangle is defined as 8
The Pascal's Triangle :
        1
       1 1
      1 2 1
     1 3 3 1
    1 4 6 4 1
   1 5 10 10 5 1
  1 6 15 20 15 6 1
 1 7 21 35 35 21 7 1
1 8 28 56 70 56 28 8 1

  1. ASCII মান প্রিন্ট করার জন্য জাভা প্রোগ্রাম

  2. একটি স্ট্রিং প্রিন্ট করার জন্য জাভা প্রোগ্রাম

  3. একটি পূর্ণসংখ্যা প্রিন্ট করার জন্য জাভা প্রোগ্রাম

  4. জাভা প্রোগ্রাম Z আকারে ম্যাট্রিক্স প্রিন্ট করতে