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

Visionacademy-2-833-Naif Pnu Java1 Lab 7 Sol-Fwf04

Uploaded by

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

Visionacademy-2-833-Naif Pnu Java1 Lab 7 Sol-Fwf04

Uploaded by

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

Princess Nora University VisionAcademy.

online
Java Programming I (CS110) 2023-2024
Lab #07 Solution ENG.NAIF ALSHEHRI

import java.util.Scanner;

public class SumOdd {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

int n;
int sum = 0;

do {
System.out.println("Enter a number or zero to exit:");
n = input.nextInt();

//sum if n is odd number


if (n % 2 == 1) {
sum += n;
}

} while (n != 0);

System.out.println("The summation of odd numbers is : " + sum);


}
}

1
Princess Nora University VisionAcademy.online
Java Programming I (CS110) 2023-2024
Lab #07 Solution ENG.NAIF ALSHEHRI

import java.util.Scanner;

public class EXE2 {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);


System.out.print("Enter the number of lines: ");
int N = input.nextInt();

for (int i = 1; i <= N; i++) {

int num = i;
for (int j = 1; j <= i; j++) {
System.out.print(num);
num++;
}

System.out.println();
}

}
}

• Note: You can use any type of loops such as while and do while or a mix of types.

2
Princess Nora University VisionAcademy.online
Java Programming I (CS110) 2023-2024
Lab #07 Solution ENG.NAIF ALSHEHRI

import java.util.Scanner;

public class EXE3 {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

int count = 1;
while (count <= 10) {
System.out.print("Enter even number: ");
int n = input.nextInt();

if (n % 2 == 0) {
int fact = 1;
for (int i = n; i >=1; i--) {
fact *= i;
}//end for

System.out.printf("Factorial of %d is %d\n", n, fact);

}//end if
count++;
}//end while
}
}

3
Princess Nora University VisionAcademy.online
Java Programming I (CS110) 2023-2024
Lab #07 Solution ENG.NAIF ALSHEHRI

Code Segment Errors Correction

int x=1; • Syntax Error: Missing int x=1;


while(x<=6){ semicolon after break while(x<=6){
if(x==3) keyword. if(x==3)
1 break break;
• Logical Error: loop
System.out.println(x); System.out.println(x);
counter(x) is not updated
} x++;
}

//print Hi 7 times. • Syntax 1: The variable (i) //print Hi 7 times.


int i; is NOT initialized. int i = 7;
do{ do{
2 • Syntax 2: Missing System.out.println("Hi");
System.out.println("Hi");
semicolon after while i--;
i--;
}while(i>=1);
}while(i>=1)

Scanner read=new Scanner(System.in);


//Get bonus only if you are excellent • Syntax: In for loop Scanner read=new Scanner(System.in);
//Get bonus only if you are excellent
int grade; header must use the int grade;
for(int counter=1:counter<=4:counter++){
System.out.println("Enter grade"); semicolon ; instead of for(int counter=1;counter<=4;counter++){
System.out.println("Enter grade");
grade=read.nextInt(); colon : grade=read.nextInt();
3
if(grade>=90) if(grade>=90){
System.out.println("Excellent"); • Logical: The two System.out.println("Excellent");
System.out.println("You get a bonus");
statements after if }
System.out.println("You get a bonus");

} statement must be in
}
block(curly braces).

for(int i=1;i<=10;i++){ • Syntax: The Variable (i) int i;


if(i==3){ is out of the scope. for(i=1;i<=10;i++){
System.out.println("Exiting loop..."); if(i==3){
continue; System.out.println("Exiting loop...");
4 break;
}
System.out.println(i); • Logical: to exit the loop }
} use break instead of System.out.println(i);
System.out.println("Loop exit when i="+i); continue. }
System.out.println("Loop exit when i="+i);

Written with
By ENG.NAIF ALSHEHRI
[email protected]
Good Luck !

4
♥︎

You might also like