0% found this document useful (0 votes)
2 views

Java Lab 5

The document outlines a Java lab assignment focused on using for loops. Students are required to create a program that prints numbers between 1 and 10, odd numbers between 1 and 100, and sums of numbers up to 100 and a user-defined number. An advanced task involves allowing the user to select which part of the program to execute using if-else statements.

Uploaded by

arya.santosh2007
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Java Lab 5

The document outlines a Java lab assignment focused on using for loops. Students are required to create a program that prints numbers between 1 and 10, odd numbers between 1 and 100, and sums of numbers up to 100 and a user-defined number. An advanced task involves allowing the user to select which part of the program to execute using if-else statements.

Uploaded by

arya.santosh2007
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Java Lab 5 – For Loops

 Create a new Java file (empty Java file), save it to your H: drive, call it lab4
 Insert the following code to set up your program:

import java.util.Scanner;

public class lab5 {

public static void main (String args[]){

Scanner input = new Scanner(System.in);

85pt:

Write a program that accomplishes the following using for loops:

 Prints out every number between 1 and 10


 Prints out odd numbers between 1 and 100
 Prints out the sum of every number between 1 and 100.
 Prints out the sum of every number between 1 and a number that the user chooses!

100pt:

Expand the above program to allow the user to choose which part of the program to run, rather than
always running through every for loop (hint: place the for loops inside if-else statements).
Notes:

A loop that runs between 0 and 4:

for(int i = 0; i < 5; i++){

System.out.println(i);

 Remember that i++ is equivalent to i = i + 1;


 Pay attention to the location of the semi-colons
 Think about boundary cases – the edges of your counters.

You might also like