logotransparent.png

Legion of Learners

www.lol-101.com

  • Home

  • Classrooms

  • Courses

  • About Us

    • Executive Team
    • Board Members
  • Resources

  • More

    Use tab to navigate through the menu items.
    To see this working, head to your live site.
    • Categories
    • All Posts
    • My Posts
    kevinzhao183
    Mar 07, 2021

    Intro to Java 3/6/21 Homework

    in Java Question Bank

    Write a program that prints out the sum of the numbers from 1 to n, where n can be any positive integer. Use a method to calculate this sum, with one parameter of one int n. You can also read in the int from the user to make the program more interactive.

    2 comments
    0
    lshirleys2008
    Mar 13, 2021

    I actually don't know how to do this one.

    0
    brendan.yen
    Mar 13, 2021

    import java.util.Scanner; class Main { public static void main(String[] args) { Scanner x = new Scanner(System.in); int n = x.nextInt(); int sum = 0; if (n >= 1){ sum = my_sum(n); System.out.println(sum); } } public static int my_sum(int n) { int sum = 0; for ( int a=1; a <=n; a++ ) { sum = sum + a; } return sum; } }

    0
    2 comments

    Questions? Email us at legionoflearners@gmail.com or join our WeChat group!

    • lol-101dotcom

    ©2020 Legion of Learners