Wednesday, 29 February 2012

Looping - Practice Answers

Question 1 Solution:

import java.util.Scanner;

class FirstLooping{
public static void main(String args[]){
Scanner input = new Scanner(System.in);
int sum = 0;
for(int i = 0; i < 100; i++){
int number = input.nextInt();
sum = sum + number;
}
System.out.println("total sum is: " + sum);
}
}

Question 2 Solution:

import java.util.Scanner;

class SecondLooping{
public static void main(String args[]){
Scanner input = new Scanner(System.in);
int sum = 0;
int average;
for(int i = 0; i < 50; i++){
int number = input.nextInt();
sum = sum + number;
}
average = sum / 50;
System.out.println("The sum is: " + average);
}
}


Question 3 Solution:

import java.util.Scanner;

class ThirdLooping{
public static void main(String args[]){
Scanner input = new Scanner(System.in);
int sum = 0;
for(int i = 0; i < 20; i++){
int number = input.nextInt();
sum = sum + number;
}
for(int i = 0; i < 10; i++){
int number = input.nextInt();
sum = sum - number;
}
System.out.println("Your total is: " + sum);
}
}


I hope you found these problem sets fairly easy because I'm sure it's going to get much more difficult later on. If you have any questions or concerns, please leave a comment and I'll be sure to respond to it as quickly as possible. If you have any java questions, even if it doesn't relate to this current material, please leave a comment and I'll reply to you as fast as possible.

No comments:

Post a Comment