How to get sum of the numbers — Selenium and Java Challenge 3

I am sharing a Selenium and Java challenge below. These types of questions are commonly asked in Selenium- and Java-based assessments. Test your Selenium and Java knowledge and add comments for solution. Click the Solution button below to view the answer.

public class SampleProgram3{

@Test
public void testMySkills() {
    System.out.println(getTotal(10));
}

public int getTotal(int n) {
    int sum = 0;
    for (int i=1;i<=n;i++) {
        sum+=i;
    }
    return sum;
}

}


0 0 votes
Article Rating
Subscribe
Notify of
guest

2 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Deepika Karunanidhi

sum=sum+n means the program will return the answer as “110”. sum=sum+i means only it will return the answer as “55”.

Testingtalkslatest.com - A project by CreativeHub IT Solutions.
Contact Us At: support@testingtalkslatest.com
Our Partner websites - Classified Hub , CodesToolbox , Smart Fitness Guide , CodesToolbox , Testing Forum
Scroll to Top
2
0
Would love your thoughts, please comment.x
()
x