0% found this document useful (0 votes)
33 views11 pages

Programming 1 Workshop 4

The document contains Java code snippets that demonstrate various conditional statements and loops. Specifically, it includes: 1. Code to check if a student passed or failed based on their score using if/else and ternary operators. 2. Code to assign a letter grade based on exam score using if/else if and switch statements. 3. Code to determine the largest of two numbers input by the user using if/else. 4. Code to print a square of asterisks based on user input size using nested for loops. 5. Code to check if a number is even or odd using the modulo operator. 6. Code examples with errors to be fixed related to data types

Uploaded by

Okegbe Akpofure
Copyright
© © All Rights Reserved
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
Download as rtf, pdf, or txt
0% found this document useful (0 votes)
33 views11 pages

Programming 1 Workshop 4

The document contains Java code snippets that demonstrate various conditional statements and loops. Specifically, it includes: 1. Code to check if a student passed or failed based on their score using if/else and ternary operators. 2. Code to assign a letter grade based on exam score using if/else if and switch statements. 3. Code to determine the largest of two numbers input by the user using if/else. 4. Code to print a square of asterisks based on user input size using nested for loops. 5. Code to check if a number is even or odd using the modulo operator. 6. Code examples with errors to be fixed related to data types

Uploaded by

Okegbe Akpofure
Copyright
© © All Rights Reserved
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
Download as rtf, pdf, or txt
Download as rtf, pdf, or txt
You are on page 1/ 11

Okegbe Akpofure Kelvin-Faith

BU/17C/IT/2694

1a)package ng.edu.baze.com.com3131_2021Q2;

import java.util.Scanner;
import java.lang.Math;

public class Main {

public static void main(String[] args) {


//Write a program that prints ”You have passed” if a student gets a score of 50 and above, and
//prints ”You have failed” otherwise.
//Using if...else statement
//b. Using conditional operator
System.out.println("Enter the students score ::");
Scanner sc = new Scanner(System.in);
int score = sc.nextInt();
if (score >= 50){
System.out.println("You have passed");
}
else System.out.println("You have failed");
}
}

1b)package ng.edu.baze.com.com3131_2021Q2;

import java.util.Scanner;
import java.lang.Math;

public class Main {

public static void main(String[] args) {


//Write a program that prints ”You have passed” if a student gets a score of 50 and above, and
//prints ”You have failed” otherwise.
//Using if...else statement
//b. Using conditional operator
Scanner sc = new Scanner(System.in);
System.out.println("Enter students score ::");
int score = sc.nextInt();
String result = (score >= 50) ? "You have passed": "You have failed";
System.out.println(result);
}
}

2a)package ng.edu.baze.com.com3131_2021Q2;

import java.util.Scanner;
import java.lang.Math;

public class Main {

public static void main(String[] args) {


//Write a program that prints A for exam score greater than or equal to 90, B for score 80 to 89, C
//for score 70 to 79, D for score 60 to 69, E for score 50 to 59 and F score less than 50.
//a. Using if else if statement
//b. Using switch statement
System.out.println("Enter exam score::");
Scanner sc = new Scanner(System.in);
int score = sc.nextInt();
if (score >= 90) {
System.out.println("A");
} else if (score >= 80 & score <= 89) {
System.out.println("B");
} else if (score >= 70 & score <= 79) {
System.out.println("C");
} else if (score >= 60 & score <= 69) {
System.out.println("D");
} else if (score >= 50 & score <= 59) {
System.out.println("E");
} else if (score < 50) {
System.out.println("F");
}

}
}

2b)package ng.edu.baze.com.com3131_2021Q2;
import java.util.Scanner;
import java.lang.Math;

public class Main {

public static void main(String[] args) {


//Write a program that prints A for exam score greater than or equal to 90, B for score 80 to 89, C
//for score 70 to 79, D for score 60 to 69, E for score 50 to 59 and F score less than 50.
//a. Using if else if statement
//b. Using switch statement
System.out.println("Enter exam score::");
Scanner sc = new Scanner(System.in);
int score = sc.nextInt();
switch (score/10){
case 10:
case 9:
System.out.println("Grade: A");
break;
case 8:
System.out.println("Grade: B");
break;
case 7:
System.out.println("Grade: C");
break;
case 6:
System.out.println("Grade: D");
break;
case 5:
System.out.println("Grade: E");
break;
default:
System.out.println("Grade: F");
break;
}

}
}

3)package ng.edu.baze.com.com3131_2021Q2;

import java.util.Scanner;
import java.lang.Math;

public class Main {


public static void main(String[] args) {
//3.Largest number
//Write a program that reads two numbers(first number and second number), determines and display
//the larger of the two numbers.
Scanner sc = new Scanner(System.in);
System.out.println("Enter first number");
int FirstNumber = sc.nextInt();

System.out.println("Enter second number");


int SecondNumber = sc.nextInt();

if (FirstNumber > SecondNumber) {


System.out.println("The largest number is " + FirstNumber);
}
else if (FirstNumber < SecondNumber){
System.out.println("The largest number is " + SecondNumber);
}
else System.out.println("Both numbers are equal");

4)package ng.edu.baze.com.com3131_2021Q2;

import java.util.Scanner;
import java.lang.Math;

public class Main {

public static void main(String[] args) {


//4. Square of Asterik
//Write an application that prompts the user to enter the size of the side of a square, then displays a
//square of that size made of asterisks. For example if the user inputs a 5, the program should display.
//*****
//*****
//*****
//*****
//*****
Scanner scan = new Scanner(System.in);
int size;
System.out.print("Enter the size of square : ");

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;

public class Main {

public static void main(String[] args) {


//5. Write an application that determines if a number is odd or even. Hint: Use the modulo operator.
System.out.println("Enter number");
Scanner sc = new Scanner(System.in);
int number = sc.nextInt();

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;

You correct the error by assigning a data type to the variable i

6b)

6c)package ng.edu.baze.com.com3131_2021Q2;

import java.util.Scanner;
import java.lang.Math;

public class Main {

public static void main(String[] args) {


//(c) The following code should print 1 to 10.
//n = 1;
//while ( n < 10 )
//System.out.println( n++ );

int n = 1;
while ( n < 11 )
System.out.println( n++ );

;
}

You corrrect this code by assigning a data type to variable 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;

public class Main {

public static void main(String[] args) {


//(d) switch ( n )
//{
//case 1:
//System.out.println( ”The number is 1” );
//case 2:
//System.out.println( ”The number is 2” );
//break;
//default:
//System.out.println( ”The number is not 1 or 2” );
//break;
//}
int n;
System.out.println("Enter a number ::");
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
switch (n) {
case 1:
System.out.println("The number is 1");
case 2:
System.out.println("The number is 2");
break;
default:
System.out.println("The number is not 1 or 2");
break;
}

}
}

You fix the code by initializing the the variable in in the switch statement and assigning it a data type

Then you create a scanner to take input

7)package ng.edu.baze.com.com3131_2021Q2;

import java.util.Scanner;
import java.lang.Math;

public class Main {

public static void main(String[] args) {


//7. Write an program that calculates the product of even numbers from 1 to 10.
int i,num; //declare variable i,num
int evenSum=0;
//declare and initialize variables oddSum,evenSum
Scanner scan=new Scanner(System.in);
//create a scanner object for input
System.out.print("Enter the number for num: \n");
num=scan.nextInt();//reads num1 from user
for(i=1; i<=num; i++) {
if (i % 2 == 0)
evenSum = evenSum * i;
}

System.out.println("Sum of all even numbers are: "+evenSum);

}
}

8)import java.io.*;

// Java code to demonstrate star pyramid pattern

public class JavaWithQAWithExperts

// Function to demonstrate printing pattern

public static void printStars(int n)

//Declare two vairables

int i, j;

// outer loop to handle number of rows

// n in this case, you can provide static values like 5 for printing 5 rows

for(i=0; i<n; i++)

{
// inner loop to handle number of columns

for(j=0; j<=i; j++)

// printing stars

System.out.print("* ");

// just to break line, after loop for each row is completed

System.out.println();

// Main function

public static void main(String args[])

int n = 5;

//Call the printStars function by passing integer value, here it is 5

printStars(n);

public class JavaWithQAWithExperts

// Function to demonstrate printing pattern

public static void printStars(int n)


{

// declare int variable, here k is to include number of space

int i, j, k=2*n-2;

// outer loop to handle number of rows

for(i=0; i<n; i++)

// inner loop to handle number spaces

for(j=0; j<k; j++)

// printing spaces first

System.out.print(" ");

// decrementing k after each loop

k = k - 2;

// inner loop to handle number of columns

// values changing acc. to outer loop

for(j=0; j<=i; j++)

// printing stars

System.out.print("* ");

}
// print new line for next row

System.out.println();

// Main Function

public static void main(String args[])

int n = 5;

//Call printStars with to print pyramid with 5 rows

printStars(n);

You might also like