Lab 5
LAB TASK 1
a) Given two integers A and B (A ≤ B). Print all numbers from A to B inclusively.
b) Given two integers A and B. Print all numbers from A to B inclusively, in ascending order, if A < B, or in descending order, if A ≥ B
c) Sum of N numbers: N numbers are given in the input. Read them and print their sum.The first line of input contains the integer N, which is the number of integers to follow. Each of the next N lines contains one integer. Print the sum of these N integers.
d) Sum of Cubes: For the given integer N calculate the following sum:
1 3+2 3+…+N 3
CODE (Part a & b):
import java.util.Scanner;
public class ab {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int a;
int b;
System.out.print("Enter the first number: ");
a = scanner.nextInt();
System.out.print("Enter the second number: ");
b = scanner.nextInt();
if(a<b){
while(a<b){
System.out.println(a);
a++;
}
}
else if(a<=b){
while(a<=b){
System.out.println(a);
a++;
}
}
else if(a>=b){
while(a>=b){
System.out.println(a);
a--;
}
}
scanner.close();
}
}
CODE (Part c):
import java.util.Scanner;
public class c {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n;
int m=0;
int num;
int total=0;
System.out.print("Enter number of integers you wanna add: ");
n = scanner.nextInt();
while(n>m){
System.out.print("Enter the numer to add: ");
num = scanner.nextInt();
total = total + num;
m++;
}
System.out.printf("The sum of the %d integers is: %d",n,total);
scanner.close();
}
}
CODE (Part d):
import java.util.Scanner;
public class d {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n;
int m=0;
int num=1;
int total=0;
System.out.print("Enter number upto which you wanna add cubes of integers: ");
n = scanner.nextInt();
while(n>m){
total = total + (num*num*num);
num++;
m++;
}
System.out.printf("The sum of the %d integers is: %d",n,total);
scanner.close();
}
}
LAB TASK 2
Factorial: In mathematics, the factorial of an integer n, denoted by n! is the following product:
n!=1×2×…×n
For the given integer n calculate the value n!
CODE:
import java.util.Scanner;
public class task2 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n;
int m=0;
int num=1;
int total=1;
System.out.print("Enter integer to calculate it's factorial: ");
n = scanner.nextInt();
while(m<n){
total = total*num;
num++;
m++;
}
System.out.printf("The sum of the %d integers is: %d",n,total);
scanner.close();
}
}
LAB TASK 3
Number of zeros: Given N numbers: the first number in the input is N, after that N integers are given. Count the number of zeros among the given integers and print it. You need to count the number of numbers that are equal to zero, not the number of zero digits
CODE:
import java.util.Scanner;
public class task3 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n;
int m;
int counter=0;
System.out.print("How many integers do you want to input: ");
n = scanner.nextInt();
for(int i=0 ; i<n ; i++){
System.out.print("Enter the integer: ");
m = scanner.nextInt();
if(m==0){
counter++;
}
}
System.out.printf("Out of %d integers there are %d zeros!",n,counter);
scanner.close();
}
}
LAB TASK 4
The length of Sequence: Given a sequence of non-negative integers, where each number is written in a separate line. Determine the length of the sequence, where the sequence ends when the integer is equal to 0. Print the length of the sequence (not counting the integer 0). The numbers following the number 0 should be omitted. Input: 1 2 3 4 5 6 7 0
CODE:
import java.util.Scanner;
public class task4 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int num;
int counter = 0;
while(true){
System.out.print("Enter number: ");
num = scanner.nextInt();
if(num==0){
break;
}
else{
counter++;
}
}
System.out.println("Number of integers before encounter of zero: "+counter);
scanner.close();
}
}
LAB TASK 5
The maximum of the Sequence: A sequence consists of integer numbers and ends with the number 0. Determine the largest element of the sequence.
CODE:
import java.util.Scanner;
public class task5 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int num_a;
int num_b=0;
int num_c=0;
while(true){
System.out.print("Enter number: ");
num_a = scanner.nextInt();
if(num_a==0){
break;
}
else if(num_a>num_b){
num_b = num_a;
}
else{
num_c = num_a;
}
}
System.out.println("Largest integer is: "+num_b);
scanner.close();
}
}
LAB TASK 6
The index of the maximum of a sequence: A sequence consists of integer numbers and ends with the number 0. Determine the index of the largest element of the sequence. If the highest element is not unique, print the index of the first of them
CODE:
import java.util.Scanner;
public class task6 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int num_a;
int num_b=0;
int counter=-1;
while(true){
System.out.print("Enter number: ");
num_a = scanner.nextInt();
if(num_a==0){
break;
}
else if(num_a>num_b){
num_b = num_a;
System.out.printf("%d is current largest\n",num_b);
counter++;
}
else{
System.out.printf("%d is smaller than %d\n",num_a,num_b);
}
}
System.out.println("Index of largest number is: "+counter);
scanner.close();
}
}
LAB TASK 7
The number of even elements of the sequence: Determine the number of even elements in the sequence ending with the number 0.
CODE:
import java.util.Scanner;
public class task7 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int num;
int counter=0;
while(true){
System.out.print("Enter number: ");
num = scanner.nextInt();
if(num==0){
break;
}
else if(num%2==0){
counter++;
}
}
System.out.printf("There are %d even numbers.",counter);
scanner.close();
}
}
LAB TASK 8
The number of elements that are greater than the previous one: A sequence consists of integer numbers and ends with the number 0. Determine how many elements of this sequence are greater than their neighbours above. Input: 1 5 2 4 3
CODE:
import java.util.Scanner;
public class task8 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int counter=0;
int n1;
int n2;
System.out.print("Enter number: ");
n1 = scanner.nextInt();
while(true){
System.out.print("Enter number: ");
n2 = scanner.nextInt();
if(n2==0){
break;
}
else if(n2>n1){
counter++;
}
n1=n2;
}
System.out.println("The elements of this sequence are greater than their neighbours above are: "+counter);
scanner.close();
}
}
LAB TASK 9
Write a program to print following :
The program asks the user to enter the number which pattern he/she wants to print. The loop should ask the user whether he or she wishes to perform the operation again. If so, the loop should repeat; otherwise it should terminate.
CODE:
import java.util.Scanner;
public class task9 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter number of rows: ");
int n = scanner.nextInt();
while(true){
System.out.println("------------------------\nPATTERN PRINTING PROGRAM\n------------------------");
System.out.println("1. Print rectange pattern of stars");
System.out.println("2. Print left oriented triangle pattern of stars");
System.out.println("3. Print right oriented triangle pattern of stars");
System.out.println("4. Print pyramid pattern of stars");
System.out.println("5. Print left oriented INVERTED triangle pattern of stars");
System.out.println("6. Print right oriented INVERTED triangle pattern of stars");
System.out.println("7. Print INVERTED pyramid pattern of stars");
System.out.println("8. Print DIAMOND pattern of stars");
System.out.println("9. Print left oriented triangle pattern of numbers");
System.out.println("10. Exit");
System.out.print("Select an option(1-10): ");
int option = scanner.nextInt();
System.out.println("OUTPUT:");
if(option==1){
for(int i=0 ; i<n ; i++){
for(int j=0 ; j<10 ; j++){
System.out.print("*");
}
System.out.println();
}
}
else if(option==2){
for(int i=0 ; i<n ; i++){
for(int j=0 ; j<=i ; j++){
System.out.print("*");
}
System.out.println();
}
}
else if(option==3){
for(int i=0 ; i<n ; i++){
for(int j=n-1 ; j>=i ; j--){
System.out.print(" ");
}
for(int k=0 ; k<=i ; k++){
System.out.print("*");
}
System.out.println();
}
}
else if(option==4){
for(int i=0 ; i<n ; i++){
for(int j=n-2 ; j>=i ; j--){
System.out.print(" ");
}
for(int k=0 ; k<=i ; k++){
System.out.print("*");
}
for(int l=1 ; l <=i ; l++){
System.out.print("*");
}
System.out.println();
}
}
else if(option==5){
for(int i=0 ; i<n ; i++){
for(int j=n-1 ; j>=i ; j--){
System.out.print("*");
}
System.out.println();
}
}
else if(option==6){
for(int i=0 ; i<n ; i++){
for(int j=1 ; j<=i ; j++){
System.out.print(" ");
}
for(int k=n-1 ; k>=i ; k--){
System.out.print("*");
}
System.out.println();
}
}
else if(option==7){
for(int i=0 ; i<n ; i++){
for(int j=1 ; j<=i ; j++){
System.out.print(" ");
}
for(int k=n-1 ; k>=i ; k--){
System.out.print("*");
}
for(int l=n-2 ; l>=i ; l--){
System.out.print("*");
}
System.out.println();
}
}
else if(option==8){
for(int i=0 ; i<n ; i++){
for(int j=n-2 ; j>=i ; j--){
System.out.print(" ");
}
for(int k=0 ; k<=i ; k++){
System.out.print("*");
}
for(int l=1 ; l<=i ; l++){
System.out.print("*");
}
System.out.println();
}
for(int i=0 ; i<n-1 ; i++){
for(int j=0 ; j<=i ; j++){
System.out.print(" ");
}
for(int k=n-2 ; k>=i ; k--){
System.out.print("*");
}
for(int l=n-3 ; l>=i ; l--){
System.out.print("*");
}
System.out.println();
}
}
else if(option==9){
for(int i=1 ; i<=n ; i++){
for(int j=n ; j>i ; j--){
System.out.print(" ");
}
for(int k=0 ; k<i ; k++){
System.out.print(i);
}
for(int l=1 ; l<i ; l++){
System.out.print(i);
}
System.out.println();
}
}
else if(option==10){
break;
}
else{
System.out.println("Invalid Entry!");
}
}
System.out.println("Thank you for using our program");
}
}