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

Name: Almosura Paolo R. Dr. Joan P.Lazaro Section: 1me DATE: 03-22-2021 Grade

This document contains 5 programming problems and their solutions using Java. It involves using loops like while and for loops. Problem 1 displays a string 20 times using a while loop. Problem 2 inputs and sums 10 integers, calculating their sum and product using a for loop. Problem 3 inputs an integer from 1 to 10 and calculates its factorial using a while loop. Problem 4 inputs N integers, calculates their sum and average using a for loop. Problem 5 loops inputting integers and displays if they are positive or negative, terminating if 0 is input.

Uploaded by

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

Name: Almosura Paolo R. Dr. Joan P.Lazaro Section: 1me DATE: 03-22-2021 Grade

This document contains 5 programming problems and their solutions using Java. It involves using loops like while and for loops. Problem 1 displays a string 20 times using a while loop. Problem 2 inputs and sums 10 integers, calculating their sum and product using a for loop. Problem 3 inputs an integer from 1 to 10 and calculates its factorial using a while loop. Problem 4 inputs N integers, calculates their sum and average using a for loop. Problem 5 loops inputting integers and displays if they are positive or negative, terminating if 0 is input.

Uploaded by

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

NCP 2210 (Computer Fundamentals and Programming)

MACHINE PROBLEM #3
LOOPS
NAME: ALMOSURA PAOLO R. DR. JOAN P.LAZARO
SECTION: 1ME DATE: 03-22-2021 GRADE:

Problem #1:
Write a program that will display “I will pass NME 2210” 20 times using any loops.
PROGRAM:
package Q;
/**
* @author PAOLO ALMOSURA
*/
public class Q
{
public static void main(String[] args)
{
int number = 1;
while (number <= 20)
{
System.out.println("I will pass NME 2210");
number++;
}
}
}

OUTPUT:

1 | Page
NCP 2210 (Computer Fundamentals and Programming)

Problem #2:
Design a program using Java that will input 10 integers and display its sum and product using
any loops.

PROGRAM:
package Q;
import java.util.Scanner;
/**
* @author PAOLO ALMOSURA

2 | Page
NCP 2210 (Computer Fundamentals and Programming)

*/
public class Q{
public static void main(String[] args)
{
int j, num = 0;
int product = 1, sum = 0;
Scanner keyboard = new Scanner(System.in);
{
System.out.println("Enter 10 numbers : ");
}
for ( j = 0 ;j <10; j++){
num = keyboard.nextInt();
sum += num;
product *= num;
}
System.out.println("The sum of 10 no is : " + sum);
System.out.println("The product of 10 no is : " + product);
}
}

OUTPUT:

3 | Page
NCP 2210 (Computer Fundamentals and Programming)

Problem #3:
Write a program that will input any integer from 1 to 10 and display its factorial using looping.

PROGRAM:
package Q;
import java.util.Scanner;
/**
* @author PAOLO ALMOSURA
*/
public class Q

4 | Page
NCP 2210 (Computer Fundamentals and Programming)

{
public static void main(String[] args)
{
int num, j = 1;
long fact = 1;
System.out.print("Enter any number: ");
Scanner keyboard = new Scanner(System.in);
num = keyboard.nextInt();

while(j <= num)


{
fact = fact * j;
j++;
}
System.out.println("Factorial of " + num + " is: "+ fact );
}
}

OUTPUT:

5 | Page
NCP 2210 (Computer Fundamentals and Programming)

Problem #4:
Write a program that will input N integers and will compute and display its sum and average.
PROGRAM:
package Q;
import java.util.Scanner;
/**
* @author PAOLO ALMOSURA
*/
public class Q
{
public static void main(String[] args)
{
int N, j;
double ave, num = 0, sum = 0;
Scanner keyboard = new Scanner(System.in);
{
System.out.print("Enter N: ");
N = keyboard.nextInt();
System.out.println("Enter " + N + " numbers:");
}
for (j = 0; j < N; j++)
{
num = keyboard.nextInt();
sum += num;
}
ave = sum / N;
System.out.println("The Sum of " + N + " Numbers is : " +
sum);
System.out.println("The Average is : " + ave);
}
}

6 | Page
NCP 2210 (Computer Fundamentals and Programming)

OUTPUT:

Problem #5:
Write a code that will loop by inputting any integer and displays whether the integer is
“POSITIVE” or “NEGATIVE”, and terminate the loop if the integer is equal to zero using any
loops.
PROGRAM:

7 | Page

You might also like