You are on page 1of 7

Programming

Fundamentals
Lab-Assignment

Name : Kantesh Kumar


Reg no: FA20-BSE-101
Submitted To : Azfar Shakeel Khan

S SUBMITTED BY: KANTESH KUMAR


2

Q14: (Compute the greatest common divisor) Another solution for Listing 5.9 to find
the greatest common divisor of two integers n1 and n2 is as follows: First find d to be the
minimum of n1 and n2, then check whether d, d-1, d-2, . . . , 2, or 1 is a divisor for both n1
and n2 in this order. The first such common divisor is the greatest common divisor for n1 and
n2. Write a program that prompts the user to enter two positive integers and displays the gcd.

Code:
import java.util.*;

public class hello {   
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.print("Enter first integer: ");
3

        int n1 = input.nextInt();
        System.out.print("Enter second integer: ");
        int n2 = input.nextInt();
        int gcd = n1 < n2 ? n1 : n2;
        while (n1 % gcd != 0 || n2 % gcd != 0) {
            gcd--;
        }
        System.out.println("The greatest common divisor for " + n1 +
            " and " + n2 + " is " + gcd);
    }
}

OUTPUT:

Q17: (Display pyramid) Write a program that prompts the user to enter an integer from
1 to 15 and displays a pyramid, as shown in the following sample run:

CODE:
import java.util.*;

public class hello {    
    public static void main(String[] args) {
        Scanner ken = new Scanner(System.in);
        System.out.print("Enter the number of lines: ");
        int no_of_lines = ken.nextInt();
        for (int rows = 1; rows <= no_of_lines; rows++) {
            for (int s = no_of_lines - rows; s >= 1; s--) {
4

                System.out.print("  ");
}
            for (int l = rows; l >= 2; l--) {
                System.out.print(l + " ");
            }
            for (int r = 1; r <= rows; r++) {
                System.out.print(r + " ");
            }
            System.out.println();
        }
    }
}

OUTPUT:

Q41: (Occurrence of max numbers) Write a program that reads integers, finds the
largest of them, and counts its occurrences. Assume that the input ends with number 0.
Suppose that you entered 3 5 2 5 5 5 0; the program finds that the largest is 5 and the
occurrence count for 5 is 4.

CODE:
import java.util.*;

public class hello {    
    public static void main(String[] args) {
        int count = 0;
        int number = -1;
        int max = -1;
        Scanner input = new Scanner(System.in);
5

        System.out.println("Enter numbers (the input ends if it is 0): ");
        while (number != 0) {
        number = input.nextInt();
        if (number > max) {
        max = number;
        count = 1;
        }
        else if (number == max){
        count++;
        }
        }
        System.out.println("The largest number is: " +max);
        System.out.println("The occurrence count of the largest number is " +c
ount);
        }
        
        }
OUTPUT:

Q49: (Count vowels and consonants) Assume letters A, E, I, O, and U as the vowels. Write a
program that prompts the user to enter a string and displays the number of vowels and consonants
in the string

CODE:
import java.util.*;

public class hello {    
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        System.out.print("Enter a string: ");
        String string = input.nextLine();

        int vowels,                     
6

             consonants;                
        vowels = consonants = 0;
        for (int i = 0; i < string.length(); i++) {
            if (Character.isLetter(string.charAt(i))) {
                if (Character.toUpperCase(string.charAt(i)) == 'A' ||
                     Character.toUpperCase(string.charAt(i)) == 'E' ||
                     Character.toUpperCase(string.charAt(i)) == 'I' ||
                     Character.toUpperCase(string.charAt(i)) == 'O' ||
                     Character.toUpperCase(string.charAt(i)) == 'U') {
                    vowels++;
                }
                else
                    consonants++;
            }
        }   
        System.out.println("The number of vowels is " + vowels);
        System.out.println("The number of consonants is " + consonants);
    }
}

OUTPUT:
7

You might also like