Skip to content

CoderSales/MSc-Y1-S1-W10-Java-review-simple-programs

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 

Repository files navigation

MSc-Y1-S1-W10-Java-review-simple-programs

Description

MSc-Y1-S1-W10-Java-review-simple-programs

Content

for loop

Q/ product from 1 to n

A/ (body of program)

int result=1;
for (int n=10;n>=1;n--) {
  result = result*n;
}
System.out.println(result);

Q/ print sum of all odd numbers from 1 to 50:

hint1: start at 1

hint2: use i+=2

A/ (body of program)

int result = 0;
for(int i=1;i<50;i+=2) {
  result = result+i;
}

References

PSLG Tutorials