কম্পিউটার

প্রাকৃতিক সংখ্যার যোগফল গণনা করার জন্য জাভা প্রোগ্রাম


এই নিবন্ধে, আমরা জানব কিভাবে জাভাতে প্রাকৃতিক সংখ্যার যোগফল গণনা করা যায়। 1 থেকে অসীম পর্যন্ত সমস্ত সম্ভাব্য ধনাত্মক সংখ্যাকে প্রাকৃতিক সংখ্যা বলা হয়।

নীচে একই -

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

ইনপুট

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

50 and 100

আউটপুট

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

Sum of natural numbers from 50 to 100 is 3825

অ্যালগরিদম

Step1- Start
Step 2- Declare three integers my_lower_limit , my_upper_limit, sum.
Step 3- Prompt the user to enter two integer value/ define the integers
Step 4- Read the values
Step 5- Run a for-loop, add the number with its next number until the upper limit is reached.
Store the sum in a variable.
Step 6- Display the result
Step 7- Stop

উদাহরণ 1

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

import java.util.Scanner;
public class NaturalNumbersSum {
   public static void main(String[] args) {
      int my_lower_limit , my_upper_limit, sum;
      System.out.println("Required packages have been imported");
      Scanner scanner = new Scanner(System.in);
      System.out.println("A scanner object has been defined ");
      System.out.print("Enter the starting number: ");
      my_lower_limit = scanner.nextInt();
      System.out.print("Enter the max number: ");
      my_upper_limit = scanner.nextInt();
      sum = 0;
      for(int i = my_lower_limit; i <= my_upper_limit; ++i){
         sum += i;
      }
      System.out.println("The sum of natural numbers from " + my_lower_limit + " to " + my_upper_limit + " is " +sum);
   }
}

আউটপুট

Required packages have been imported
A scanner object has been defined
Enter the starting number: 50
Enter the max number: 100
The sum of natural numbers from 50 to 100 is 3825

উদাহরণ 2

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

public class NaturalNumbersSum {
   public static void main(String[] args) {
      int my_input_1 , my_input_2, sum;
      my_input_1 = 50;
      my_input_2 = 100;
      sum = 0;
      System.out.println("The first and last numbers are defined as " +my_input_1 +" and "+my_input_2 );
      for(int i = my_input_1; i <= my_input_2; ++i){
         sum += i;
      }
      System.out.println("The sum of natural numbers from " + my_input_1 + " to " + my_input_2 + " is " +sum);
   }
}

আউটপুট

The first and last numbers are defined as 50 and 100
The sum of natural numbers from 50 to 100 is 3825

  1. C প্রোগ্রামে প্রথম n প্রাকৃতিক সংখ্যার যোগফল

  2. C প্রোগ্রামে প্রথম n প্রাকৃতিক সংখ্যার বর্গক্ষেত্রের সমষ্টি?

  3. C প্রোগ্রামে প্রথম n জোড় সংখ্যার বর্গক্ষেত্রের সমষ্টি

  4. প্রথম n প্রাকৃতিক সংখ্যার ঘনক্ষেত্রের যোগফলের জন্য C প্রোগ্রাম?