In: Computer Science
public void printNumbers(int low, int high) { // using a for loop, print all numbers from low to high } public int sumOfNumbers(int n) { // using a for loop, calculate and return the sum of first n numbers // i.e n = 5, answer = 5+4+3+2+1 = 15 } public void printMultiplicationTable(int num) { // using a for loop, print the multiplication table of num (up to first 10!) // i.e. num = 5, 5*1=5, 5*2=10, 5*3=15, 5*4=20, 5*5=25, 5*6=30, 5*7=35... }
public void printNumbers(int low, int high) { for (int i = low; i <= high; i++) { System.out.println(i); } } public int sumOfNumbers(int n) { int sum = 0; for (int i = 1; i <= n; i++) { sum += i; } return sum; } public void printMultiplicationTable(int num) { // using a for loop, print the multiplication table of num (up to first 10!) // i.e. num = 5, 5*1=5, 5*2=10, 5*3=15, 5*4=20, 5*5=25, 5*6=30, 5*7=35... for (int i = 1; i <= 10; i++) { System.out.println(num + "*" + i + "=" + (num * i)); } }
public class LoopNumbers { public void printNumbers(int low, int high) { for (int i = low; i <= high; i++) { System.out.println(i); } } public int sumOfNumbers(int n) { int sum = 0; for (int i = 1; i <= n; i++) { sum += i; } return sum; } public void printMultiplicationTable(int num) { // using a for loop, print the multiplication table of num (up to first 10!) // i.e. num = 5, 5*1=5, 5*2=10, 5*3=15, 5*4=20, 5*5=25, 5*6=30, 5*7=35... for (int i = 1; i <= 10; i++) { System.out.println(num + "*" + i + "=" + (num * i)); } } public static void main(String[] args) { new LoopNumbers().printNumbers(5, 9); System.out.println(new LoopNumbers().sumOfNumbers(100)); new LoopNumbers().printMultiplicationTable(5); } }