Programming 1 Workshop 4
Programming 1 Workshop 4
BU/17C/IT/2694
1a)package ng.edu.baze.com.com3131_2021Q2;
import java.util.Scanner;
import java.lang.Math;
1b)package ng.edu.baze.com.com3131_2021Q2;
import java.util.Scanner;
import java.lang.Math;
2a)package ng.edu.baze.com.com3131_2021Q2;
import java.util.Scanner;
import java.lang.Math;
}
}
2b)package ng.edu.baze.com.com3131_2021Q2;
import java.util.Scanner;
import java.lang.Math;
}
}
3)package ng.edu.baze.com.com3131_2021Q2;
import java.util.Scanner;
import java.lang.Math;
4)package ng.edu.baze.com.com3131_2021Q2;
import java.util.Scanner;
import java.lang.Math;
size = scan.nextInt();
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
if (i == 0 || j == 0 || i == size - 1 || j == size - 1)
System.out.print("*");
else
System.out.print(" ");
}
System.out.println();
}
}
5)package ng.edu.baze.com.com3131_2021Q2;
import java.util.Scanner;
import java.lang.Math;
if (number % 2 == 0){
System.out.println(number + " is an even number");
}
else System.out.println(number + " is an odd number");
}
}
6a)//6. Find the error in the following code segment and explain how to correct it.
//(a) i = 1;
//while ( i ≤ 5 );
//++i;
//}
int i = 1;
while (i <= 5) ;
++i;
6b)
6c)package ng.edu.baze.com.com3131_2021Q2;
import java.util.Scanner;
import java.lang.Math;
int n = 1;
while ( n < 11 )
System.out.println( n++ );
;
}
then increasing the iteration to less than 11 or less than or equal to 10 as less than 10 only prints 1 to 9
6d)package ng.edu.baze.com.com3131_2021Q2;
import java.util.Scanner;
import java.lang.Math;
}
}
You fix the code by initializing the the variable in in the switch statement and assigning it a data type
7)package ng.edu.baze.com.com3131_2021Q2;
import java.util.Scanner;
import java.lang.Math;
}
}
8)import java.io.*;
int i, j;
// n in this case, you can provide static values like 5 for printing 5 rows
{
// inner loop to handle number of columns
// printing stars
System.out.print("* ");
System.out.println();
// Main function
int n = 5;
printStars(n);
int i, j, k=2*n-2;
System.out.print(" ");
k = k - 2;
// printing stars
System.out.print("* ");
}
// print new line for next row
System.out.println();
// Main Function
int n = 5;
printStars(n);