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

Midterm Data Structure

The document outlines 6 midterm requirements involving algorithms and Java code to solve problems using repetition, selection, and combining the two. The requirements are to: 1) Print numbers 1 to 10, 2) Print numbers 10 to 1, 3) Print the nth triangular number based on user input, 4) Test a number between 0-100 and print pass/fail, 5) Print letter grades for 5 numbers between 0-100, and 6) Print 20 random numbers between 0-100. The response provides algorithms and sample Java code to meet each requirement in 3 sentences or less per requirement.

Uploaded by

Torres, Luigi P.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
67 views

Midterm Data Structure

The document outlines 6 midterm requirements involving algorithms and Java code to solve problems using repetition, selection, and combining the two. The requirements are to: 1) Print numbers 1 to 10, 2) Print numbers 10 to 1, 3) Print the nth triangular number based on user input, 4) Test a number between 0-100 and print pass/fail, 5) Print letter grades for 5 numbers between 0-100, and 6) Print 20 random numbers between 0-100. The response provides algorithms and sample Java code to meet each requirement in 3 sentences or less per requirement.

Uploaded by

Torres, Luigi P.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Midterm Requirements:

Create an algorithm for the following problems/tasks and execute it using Java Programming
Language. Use the space provided for your algorithms and codes.

REPETITION

1. Write a program that prints the numbers from 1 to 10.


2. Write a program that prints the numbers from 10 to 1.
3. Write a program that lets you enter a positive number n and prints the nth triangular
number.

Sample Output:

SELECTION

4. Write a program the let you enter a mark between 0 and 100 and that will test its value and
print appropriately. “PASS” or “FAIL”.
5. Write a program that let you enter 5 marks between 0 and 100 and that prints out in each
case the letter grade.

Sample output:

F= 0-49, D= 50-59, C= 60-69, B= 70-79, A=80-89, A+=90-100

COMBINING SELECTION AND REPETITION

6. Write a program that let you print 20 random real numbers between 0 to 100.

OUTPUT

ANSWER NO. 1:

ALGORITHM

I=1
If i<=10
Print i
I++

________________________________________

CODES

package com.Algorithm;
public class Numbersonetoten {
public static void main(String[] args) {
System.out.println("Printing 1 to 10 numbers!");

int i = 10;

for (i = 1;i <= 10; i++)

{
System.out.println( i + ",");

}
}
}
__________________________________

ANSWER NO. 2:

ALGORITHM

Int i=10
If i>=1
Print i
i—

________________________________________

CODES

package com.cc103.com.cc104bsit2a;

public class Reverse10to1 {

public static void main(String[] args) {


System.out.println("Printing 10 to 1!");
int i = 10;

for (i = 10; i > 0; i--){


System.out.println( i + ",");
}
}

}
_________________________________________

ANSWER NO. 3:

ALGORITHM
Ask User to enter number
That will result rows in making triangle of “ * ”
________________________________________

CODES

package com.Algorithm;

import java.util.Scanner;

public class Triangular {

public static void main(String[] args) {


Scanner data= new Scanner(System.in) ;
System.out.print("Enter Number: ");
int rows =data.nextInt();

int k = 0;

for(int i = 1; i <= rows; ++i, k = 0){

for (int j = 1; j <= rows - i; ++j){


System.out.print(" ");
}
while (k !=2* i-1){
System.out.print("* ");
++k;
}
System.out.println();
}

}
}_________________________________________

ANSWER NO. 4

ALGORITHM

Ask User to enter a number

If >=75 and <=100


Print “Passed”
If <=74 and >=0
Print “Failed”

________________________________________

CODES

import java.util.Scanner;

public class algo {


public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
System.out.print("Enter a number: ");

int number = reader.nextInt();

if (number >= 75) {

System.out.println("Passed");

else{

System.out.println("FAILED");
}

}
}

_________________________________________

ANSWER NO. 5:

ALGORITHM

Ask User to put 5 Grades

If grade is between 1 – 49

Print F

If grade is between 50 – 59

Print D

If grade is between 60 – 69

Print C

If grade is between 70 – 79

Print B

If grade is between 80 – 89

Print A

If grade is between 90 – 100

Print A+
________________________________________

CODES
package com.Algorithm;

import java.util.Scanner;

public class GradeCalcu {


public static void main (String[]args){

Scanner console = new Scanner(System.in);

int Score[] =new int [5] ;

System.out.println("Enter 5 Grades");

for(int i=0;i<Score.length;i++)
{
System.out.print("Grade "+(i+1)+" :");
Score[i]= console.nextInt();

System.out.println("Remarkings");
for(int i=0;i<Score.length;i++)

if (Score[i] >= 0 && Score[i] <= 49)


{
System.out.println("F grade");
}

else if (Score[i] >= 50 && Score[i] <=59)

{
System.out.println("D grade");

}
else if (Score[i] >= 60 && Score[i] <=69)

{
System.out.println("C grade");

}
else if (Score[i] >= 70 && Score[i] <=79)

{
System.out.println("B grade");

}
else if (Score[i] >= 80 && Score[i] <=89)
{
System.out.println("A grade");

}
else if (Score[i] >= 90 && Score[i] <=100)

{
System.out.println("A+ grade");

}
}

_________________________________________

ANSWER NO. 6:

ALGORITHM

Use loop that contains only 1 to 20


Print 20x different numbers
________________________________________

CODES

package com.Algorithm;

import java.util.Random;

public class RandomNumbers {


public static void main(String[] args) {

int random;

Random rnum = new Random();

System.out.println("Random Numbers:");

for (random = 1; random <= 20; random++) {


System.out.println(rnum.nextInt(100));
}
}
}
_________________________________________

You might also like