Subject PROGRAMMING
FUNDAMENTALS
Name ALINA RAZA
Reg.no. FA23-BSE-015
Lab Practice lab
Date 1-5-24
Moderator:
SIR MUZAFFAR IQBAL
Fa23-bse-015
PRACTICE LAB:
ACTIVITY 1:
Keep accepting registration no, name and marks of students until 0 is
entered against rno. Display name of the student having maximum marks.
CODE:
package practlab1may;
import java.util.Scanner;
public class Act1 {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
String maxName="";
int maxMarks=0;
while(true){
System.out.print("enter the student name:");
String name=input.next();
System.out.print("enter the marks");
int marks=input.nextInt();
System.out.print("enter the reg no.:");
int rnum=input.nextInt();
if(marks>maxMarks){
maxMarks=marks;
2
Fa23-bse-015
maxName=name;
if (rnum==0)
break;
System.out.println(maxName);
OUTPUT:
3
Fa23-bse-015
ACTIVITY 2:
Keep accepting strings from user until STOP is entered,
- Find largest string according to size
- Find largest string according to ASCII code
- Find largest string according to vowels.
CODE:
package assignment2;
import java.util.Scanner;
public class Ques2 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int highestScore=0;
String highestName= "";
int secondHighestScore=0;
String secondHighestName= "";
System.out.print("enter the number of students:");
int numOfStudents=input.nextInt();
System.out.print("enter each students name and score: ");
for(int i=0;i<numOfStudents;i++){
System.out.print("student:" + (i+1) + " \n name: ");
String name=input.next();
System.out.print("score:");
int score=input.nextInt();
4
Fa23-bse-015
if (i == 0) {
highestScore = score;
highestName = name;
else if (i == 1 && score > highestScore){
secondHighestScore = highestScore;
highestScore = score;
highestName = secondHighestName;
highestName = name;
else if (i == 1){
secondHighestScore = score;
secondHighestName = name;
else if (i > 1 && score > highestScore && score >
secondHighestScore) {
secondHighestScore = highestScore;
secondHighestName = highestName;
highestScore = score;
highestName = name;
5
Fa23-bse-015
else if (i > 1 && score > secondHighestScore) {
secondHighestName = name;
secondHighestScore = score;
System.out.println("\n" + highestName + " has the highest score: " +
highestScore);
System.out.println(secondHighestName + " has the second highest score: " +
secondHighestScore);
6
Fa23-bse-015
OUTPUT:
7
Fa23-bse-015
QUESTION 3:
Write a Java program to keep accepting integer values from user until a
prime number is entered.
CODE:
package assignment2;
import java.util.Scanner;
public class Ques3 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
boolean isPrime = false;
System.out.println("Enter a prime number:");
while (!isPrime) {
int number = input.nextInt();
if (number <= 1) {
System.out.println(number + " is not a prime number. Try again.");
continue;
isPrime=true;
8
Fa23-bse-015
for (int i = 2; i <=number/2; i++) {
if (number % i == 0) {
isPrime = false;
break;
if (!isPrime) {
System.out.println(number + " is not a prime number. Try again.");
} else {
System.out.println(number + " is a prime number.");
9
Fa23-bse-015
OUTPUT:
QUESTION 4:
10
Fa23-bse-015
Write a program that prompts the user to enter an integer from 1 to 10
and displays a pyramid.
CODE:
package assignment2;
import java.util.Scanner;
public class Practice {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter the number of lines: ");
int numOfLines = input.nextInt();
for(int rows=1;rows<=numOfLines;rows++){
for(int spaces=numOfLines-rows;spaces>=1;spaces--){
System.out.print(" ");
for(int j=rows;j>=2;j--){
System.out.print(j + " ");
for(int i=1;i<=rows;i++){
System.out.print(i + " ");
System.out.println();
11
Fa23-bse-015
OUTPUT:
12
Fa23-bse-015
QUESTION 5:
Use nested loops that display the following patterns.
Pattern (A):
CODE:
package assignment2;
public class Ques5A {
public static void main(String[] args) {
int n=6;
for(int i=1;i<=n;i++){
for(int j=1;j<=i;j++){
System.out.print(j+ " ");
System.out.println();
13
Fa23-bse-015
OUTPUT:
Pattern (B):
CODE:
package assignment2;
public class Ques5B {
public static void main(String[] args) {
int n=6;
for(int i=n;i>=1;i--){
for(int j=1;j<=i;j++){
System.out.print(j+ " ");
System.out.println();
14
Fa23-bse-015
OUTPUT:
Pattern (C):
CODE:
package assignment2;
public class Ques5C {
public static void main(String[] args) {
int n=6;
for(int i=1;i<=n;i++){
for(int j=n-i;j>0;j--){
System.out.print(" ");
for(int j=i;j>0;j--){
System.out.print(j+" ");
}System.out.println();
15
Fa23-bse-015
OUTPUT:
Pattern (D):
CODE:
package assignment2;
public class Ques5D {
public static void main(String[] args) {
int n=6;
for(int i=1;i<=n;i++){
for(int j=1;j<i;j++){
System.out.print(" ");
for(int j = 1; j <= n - i + 1; j++){
System.out.print(j+" ");
}System.out.println();
16
Fa23-bse-015
OUTPUT:
17
Fa23-bse-015
QUESTION 6:
Write a nested for loop.
CODE:
package assignment2;
public class Ques6 {
public static void main(String[] args) {
int n = 30;
for(int i = 1; i<=128; i=i*2){
for (int j = n; j>=0; j--){
System.out.print(" ");
for (int j =1; j<=i-1; j=j*2){
System.out.print(" ");
System.out.print(j);
for (int j = i; j>=1; j=j/2){
System.out.print(" ");
System.out.print(j);
n=n-4;
System.out.println();
18
Fa23-bse-015
OUTPUT:
19
Fa23-bse-015
QUESTION 7:
A positive integer is called a perfect number if it is equal to the sum of all
of its positive divisors, excluding itself. For example, 6 is the first perfect
number because 6 = 3 + 2 + 1. The next is 28 = 14 + 7 + 4 + 2 + 1. There are
four perfect numbers less than 10,000. Write a program to find all these
four numbers.
CODE:
package assignment2;
public class Ques7 {
public static void main(String[] args) {
System.out.println("the perfect numbers ranging from 0 to 10,000 are: ");
int n=10000;
int sum;
for(int i=1;i<=n;i++){
sum=0;
for(int j=1;j<i;j++){
if(i%j==0)
sum +=j;
if(i==sum)
System.out.println(i);
20
Fa23-bse-015
OUTPUT:
21
Fa23-bse-015
QUESTION 8:
Write a program that prompts the user to enter a decimal integer and
displays its corresponding binary value. Don’t use Java’s
Integer.toBinaryString (int) in this program.
CODE:
package assignment2;
import java.util.Scanner;
public class Ques8{
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter the decimal integer: ");
int decimal = input.nextInt();
System.out.print("Binary value: "+binary(decimal));
public static String binary(int num) {
String binary="";
int digit;
while(true){
if(num>0){
digit = num%2;
binary=digit+binary;
num/=2;
22
Fa23-bse-015
else
break;
return binary;
OUTPUT:
23
Fa23-bse-015
QUESTION 9:
Write a program that prompts the user to enter a string and displays the
string in reverse order.
CODE:
package assignment2;
import java.util.Scanner;
public class Ques9{
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter the string without spaces: ");
String str = input.next();
String reverse="";
for(int i=str.length()-1;i>=0;i--){
reverse+=str.charAt(i);
System.out.println("the reversed string is " + reverse);
24
Fa23-bse-015
OUTPUT:
25
Fa23-bse-015
QUESTION 10:
Write a program to accept a sentence from user and find the number of
vowels in it.
CODE:
package assignment2;
mport java.util.Scanner;
public class Ques10 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter a string: ");
String userInput = input.nextLine();
int vowelCount = countsVowels(userInput);
System.out.println("Number of vowels in the string are: " + vowelCount);
input.close();
public static int countsVowels(String s) {
int count = 0;
for (int i = 0; i < s.length(); i++) {
char ch = s.charAt(i);
if (ch == 'a' || ch == 'A' || ch == 'e' || ch == 'E' || ch == 'i' || ch == 'I' ||
ch == 'o' || ch == 'O' || ch == 'u' || ch == 'U') {
count++;
26
Fa23-bse-015
return count;
OUTPUT:
27