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

Lab 5 - Sol

Uploaded by

jbb12191
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Lab 5 - Sol

Uploaded by

jbb12191
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Lab 6 - for loop

1. (Using for loop) Write a program in Java to


display the n terms of even natural numbers and their
sum.

Test Data:
Input number of terms: 5
Expected Output:
The even numbers are :2 4 6 8 10
The Sum of even Natural Number upto 5 terms : 30

import java.util.*;
public class Example {
public static void main(String[] args) {

Scanner input = new Scanner(System.in);


System.out.print("Input number of terms: ");
int n = input.nextInt();

System.out.print("The even numbers are: ");


int sum=0;
for(int i=1;i<=n;i++) {
System.out.print(" "+i*2);
sum+=i*2;
}
System.out.println("\nThe Sum of even Natural
Number upto "+n+" terms :" + sum);
}
}
2. (Using for loop) Write a program that computes the
following sum:
sum = 1.0/1 + 1.0/2 + 1.0/3 + 1.0/4 + 1.0/5
+ .... + 1.0/N
N is an integer limit that the user enters.

Test Data:
Enter N: 4
Expected Output:
Sum is: 2.08333333333

import java.util.*;
public class Example {
public static void main(String[] args) {

Scanner input=new Scanner(System.in);


System.out.print("Enter N:");
int n=input.nextInt();

double sum=0;
for(int i=1; i<=n;i++)
sum+=1.0/i;
System.out.println("Sum is: "+sum);
}
}

3. (Using for loop) Write a Java program that computes XN where X is a


floating point number and N is a positive integer. The program informs the
user that N must be positive.
XN = X * X * X * ... * X <-- N of these

The user dialog will look something like this:


Test Data:
Enter X: 1.3
Enter N: 5
Expected Output:
1.3 raised to the power 5 is: 3.7129292
import java.util.*;
public class Example {
public static void main(String[] args) {

Scanner input=new Scanner(System.in);


//input.useLocale(Locale.US);
System.out.print("Enter floating point number
- X: ");
float x=input.nextFloat();

System.out.print("Enter positive integer -


N:");
int n=input.nextInt();

if(n<0)
System.out.println("Please enter a
postive number");
else {
float p=1;
for(int i=1; i<=n;i++)
p=p*x;
System.out.println(x+" raised to the
power "+ n+" is:"+p);
}
}
}

Lab 6 – Nested for loop


1. Write a program in Java to display the pattern like a right angle triangle
with a number. The pattern like :
1
12
123
1234

public class Example {


public static void main(String[] args) {
for(int i=1;i<=4;i++){
for (int j=1; j<=i;j++)
System.out.print(j);
System.out.println();
}
}
}

2. Write a program in Java to display the pattern like a right angle triangle
with a number. The pattern like :
1
22
333
4444
55555

public class Example {


public static void main(String[] args) {
for(int i=1;i<=5;i++){
for (int j=1; j<=i;j++)
System.out.print(i);
System.out.println();
}
}
}

3. Write a Java program that writes a wedge of stars. The user enters the
initial number of stars, and the program writes out lines of stars. Each line
has one few star than the previous line:

Test Data:
Initial number of stars: 7
Expected output:
*******
******
*****
****
***
**
*

import java.util.*;
public class Example {
public static void main(String[] args) {

Scanner input=new Scanner(System.in);


System.out.print("Initial number of stars: ");
int n=input.nextInt();

for(int i=1;i<=n;i++){
for (int j=n; j>=i;j--)
System.out.print("*");
System.out.println();
}
}
}

Assignment #3: (using while loop and for loop)

A college offers a course that prepares students for a state licensing exam. You’ve
been given a list of 10 students. Next to each name is written a 1 if the student
passed the exam or a 2 if the student failed. Write a Java program to:
- Count the number of test results of each type.
- Display a summary of the test results.
- If more than eight students passed the exam,
display “Raise tuition.”
While loop:
import java.util.*;
public class Example {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
int passes = 0; // number of passes
int failures = 0; // number of failures
int counter = 1; // student counter
int result; // one exam result from user
while ( counter <= 10 ) {
System.out.print("Enter result (1 = pass, 2 = fail): ");
result=input.nextInt();

if ( result == 1 )
passes = passes + 1;
else
failures = failures + 1;
counter++;
}
System.out.println( "Passed: "+passes+"\nFailed: "+ failures );
if ( passes > 8 )
System.out.println( "Raise Tuition" );

}
}

For loop:
import java.util.*;
public class Example {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
int passes = 0; // number of passes
int failures = 0; // number of failures
int result; // one exam result from user
for (int counter =1; counter <= 10; counter++ ) {
System.out.print("Enter result (1 = pass, 2 = fail): ");
result=input.nextInt();

if ( result == 1 )
passes = passes + 1;
else
failures = failures + 1;
}
System.out.println( "Passed: "+passes+"\nFailed: "+ failures );
if ( passes > 8 )
System.out.println( "Raise Tuition" );

}
}

You might also like