Using for loops:
- write a Java program that adds numbers between 1 to 100 and prints the result.
- write a Java program that adds odd numbers between 1 to 100 and prints the result
- write a Java program that averages even numbers between 5 to 30 and prints the result
//TestCode.java public class TestCode { public static void main(String[] args) { int sum = 0; for(int i = 1;i<=100;i++){ sum += i; } System.out.println(sum); } }
////////////////////////////////////////////////////////////////////
//TestCode.java public class TestCode { public static void main(String[] args) { int sum = 0; for(int i = 1;i<=100;i+=2){ sum += i; } System.out.println(sum); } }
////////////////////////////////////////////////////////////////////
//TestCode.java public class TestCode { public static void main(String[] args) { int sum = 0, count = 0; for(int i = 6;i<=30;i+=2){ sum += i; count += 1; } System.out.println((1.0*sum)/count); } }
Get Answers For Free
Most questions answered within 1 hours.