Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)
Java: Add a Consecutive Numbers Program | Java Program
Latest :

Java: Add a Consecutive Numbers Program | Java Program

Addition of Consecutive Numbers in Java Program – This specific article talks about the code to find Find consecutive numbers in an array java using Java language.

  • The problem here is to find the sum of a set of consecutive numbers or more specifically the sum of all the numbers from 0 to a user-specified integer.
  • Our constraint here is the ability to only add positive integers to find the sum of consecutive numbers.
  • The input here is a user-defined positive integer.
  • The output is the sum of all the numbers from 0 to the input integer.
 

Output -1:

 

  • Solution:
  1. In the main method, int n is declared for how to count consecutive numbers in java. After that, a new Scanner class object is initialized and a reference variable sc is set to represent the object.
  2. This class is used to take user input in Java. Scanner class is part of the java.util package and hence the import statement, in the beginning, is stated to import the functionality of the specified class.
  3. The next line prints a statement to the console instructing the user to enter a number.
  4. The .nextInt() method is called by the Scanner object sc. This method reads an int value input by a user and stores it to the int variable n.
  5. Within the print statement in the last line, the method addUpTo is called and is passed in as the argument.

 

 

  1. In this method, a variable sum of type int is initialized to the value 0. This variable will store the value of the sum of the consecutive numbers till n.
  2. In the next line, the for loop iterates n+1 times (since i<=n) and adds to the current value of the variable sum. 
  3. After the loop is implemented, the sum of all numbers till is calculated and returned which is then printed out to the console.

 

 

  • In this case, the value of is 5. As the loop in the method addUpTo iterates, the value of for each iteration gets added.
  • From the first iteration to the 6th iteration : 0+1+2+3+4+5 =  15

 

Output – 2:

 

 

  • In this case, the value of is 100. As the loop in the method addUpTo iterates, the value of i for each iteration gets added.
  • From the first iteration to the 101st iteration : 0+1+2+3+4+ … + 100 =  5050
x

Check Also

Rhombus Star Pattern Program In Java – Patterns

Java program to print Rhombus star pattern program. We have written the below print/draw Rhombus ...

bakırköy escortşişli escortsightcareDeneme Bonusu Veren SitelerDeneme Bonusu Veren SitelerDeneme Bonusu Veren SitelerDeneme Bonusu Veren Sitelerdeneme bonusu veren sitelergrandpashabetgrandpashabetGrandpashabetdeneme bonusu veren sitelerdeneme bonusu veren sitelerdeneme bonusu veren sitelerdeneme bonusu veren sitelerdeneme bonusu veren sitelerdeneme bonusu veren sitelercasinofastbetwoondeneme bonusu veren sitelerdeneme bonusugrandpashabetdeneme bonusu veren sitelerdeneme bonusu veren sitelerdeneme bonusu veren sitelerdeneme bonusu veren sitelerdeneme bonusu veren sitelerdeneme bonusu veren sitelerromabetgrandpashabetgrandpashabetgrandpashabetbetgray güncel girişgrandpashagrandpashabetgrandpashabetgrandpashabet güncel girişgrandpashadeneme bonusu veren sitelerDeneme Bonusu Veren SitelerjojobetcasinowonkulisbetcasinowonDubai escortBetorder girişvaycasinopadişahbetjojobetbetgray güncelmatbetholiganbetgalabetjojobetgrandpashabetgrandpashabetgrandpashabet güncel adresgrandpashacasibompusulabetcasibom girişcasibom girişpadişahbetpadişahbetmeritkingsplashgrandpashaPadişahbetultrabetmatbethalkalı escortPusulabetroyalbetcasibomcasibom girişcasibomcasibommarsbahismarsbahiscasibomPadişahbetcasibom girişcasibomcasibomkalebetkalebetjojobet girişjojobetkulisbetcasibombetnanobetciomatbetzbahiszbahissakarya escortBetorderBetorder girişkingroyalkingroyalkalebetotobetotobetkralbetkralbetSheratonbetatlasbet girişdermocasibom giriş deneme bonusu veren sitelerdeneme bonusu veren sitelerdeneme bonusu veren sitelerromabetmarsbahissora watermark removermatbetdizipalSekabetjojobetgrandpashabet girişpusulabetmeritkingmatbetimajbetcasino levantgrandpashabetpusulabet girişgrandpashabetmeritking güncel girişmatbet girişmarsbahis giriş günceltarafbetlunabettipobetmatadorbet , matadorbet girişbahsegelvevobahis girişvevobahisholiganbetartemisbet girişmeritking girişlunabettipobet girişsafirbetlunabetmatadorbet giriştipobetbahsegel girişmatadorbetlunabetbahsegelmarsbahisjojobetcasibom girişcasibomholiganbet girişholiganbetjojobetjojobetbetnanomatbetpadişahbetjokerbettimebetwbahisartemisbetCasibom girişjojobet girişmarsbahisdinamobetrestbetimajbetultrabet girişjojobet girişcasibom girişgalabetbetciosolibetsolibet girişsolibet giriş adresisolibetsolibetjojobetjojobet girişmarsbahisdeneme bonusu veren sitelerjojobetbets10betgarjojobetcasibomcasibomCasibom Orjinal GirişCasibomultrabet