[go: up one dir, main page]

0% found this document useful (0 votes)
107 views17 pages

Lab Assignment - (1-5)

The document describes a program that accepts the number of quiz participants between 3 and 10. It stores participant answers in a double array and calculates scores by matching answers to correct ones. It outputs total and highest scores.

Uploaded by

Rishitha Reddy
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)
107 views17 pages

Lab Assignment - (1-5)

The document describes a program that accepts the number of quiz participants between 3 and 10. It stores participant answers in a double array and calculates scores by matching answers to correct ones. It outputs total and highest scores.

Uploaded by

Rishitha Reddy
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/ 17

COMPUTER

LAB
ASSIGNMENTS
BY: K RISHITHA REDDY
CLASS: XII
SECTION: D
ROLL NO:24
LAB ASSIGNMENT-1

Write a program to input a number. Count and print the frequency of each digit present in that number. The
output should be given as:

Sample Input: 44514621


Sample Output:
=====================
Digit Frequency
=====================
1 2
2 1
4 3
5 1
6 1

ALGORITHM

1. Start
2. Define a class
3. Define the main method.
4. Create a Scanner object to take input from the user.
5. Prompt the user to enter a number.
6. While number is greater than zero increment the digit
7. Extract the last digit and remove last digit from the number
8. Check whether frequency stored in array is not zero and print the frequency
9. End
PROGRAM
import java.util.*;
class lab1 //class starts
{
public static void main(String [] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter a number: ");
int n = sc.nextInt();//accepting the number
int a[] = {0,0,0,0,0,0,0,0,0,0};
System.out.println("OUTPUT: \n======================\nDigit\t\tFrequency\
n======================");
//count frequency of each digit
while(n>0)
{
int rem = n%10;
a[rem]++;
n/=10;
}
//print frequency
for(int i=0;i<10;i++)
{
if(a[i]!=0)//checks whether the frequency stored in a[i] is not zero
System.out.println(i + "\t\t"+a[i]); }
}
}//class ends
OUTPUT
LAB ASSIGNMENT-2

A bank intends to design a program to display the denomination of an input amount, upto 5 digits.The
available denomination with the bank are of rupees 1000, 500, 100, 50, 20, 10, 5, 2 and 1.

Design a program to accept the amount from the user and display the break-up in descending order of
denomination. (i.e. preference should be given to the highest denomination available) along with the total
number of notes.[Note: Only the denomination used should be displayed. Also print the amount in words
according to the digits.

Example-1

INPUT:
14856
OUTPUT:
1000 * 14 = 14000
500 * 1 = 500
100 * 3 = 300
50 * 1 = 50
5 * 1=5
1*1=1
TOTAL =14856
TOTAL NUMBER OF NOTES=21

Example-2

INPUT:
6043

OUTPUT:

1000*6=6000
20*2=40
2*1=2
1*1=1
TOTAL=6043
TOTAL NUMBER OF NOTES=10

Example-3

INPUT:
235001
OUTPUT:
INVALID AMOUNT
ALGORITHM

1)Start a class.

2) Initialize an array.

3) Ask user to enter a number

4) Create a for loop to set the limit for the currency.

5) Extract the last digits of the number.

6) If extracted digit is not equal to 0 then print the currency.

7) Close the method.

8) Close the class.

PROGRAM

import java.util.*;

class lab2 //class begins

Scanner sc = new Scanner(System.in );

void main()

System.out.println("Enter amount: ");

int amt = sc.nextInt();//accepting the amount

if(amt>100000)

System.out.println("Invalid Input");

return;

int arr[] = {1000, 500, 100, 50, 20, 10, 5, 2,1}; //array to store denominations

int note = amt;


int sum=0;

for(int i=0;i<arr.length;i++)

int note1 = note/arr[i];

if(note1!=0)

System.out.println(arr[i]+"*" +note1+ "=" +(arr[i]*note1));

note = note%arr[i];

sum+=note1;

System.out.println("TOTAL="+amt);

System.out.println("TOTAL NUMBER OF NOTES="+sum);

}//class ends

OUTPUT:
LAB ASSIGNMENT-3

A company manufactures packing cartons in four sizes, i.e. cartons to accommodate 6 boxes, 12 boxes, 24
boxes and 48 boxes. Design a program to accept the number of boxes to be packed (N) by the user
(maximum up to 1000 boxes) and display the break-up of the cartons used in descending order of capacity
(i.e. preference should be given to the highest capacity available, and if boxes left are less than 6, an extra
carton of capacity 6 should be used.)

Test your program with the following data and some random data:

Example 1

INPUT:
N = 726

OUTPUT:
48 * 15 = 720
6*1=6
Remaining boxes = 0
Total number of boxes = 726
Total number of cartons = 16

Example 2

INPUT:
N = 140

OUTPUT:
48 * 2 = 96
24 * 1 = 24
12 * 1 = 12
6*1=6
Remaining boxes = 2 * 1 = 2
Total number of boxes = 140
Total number of cartons = 6

Example 3

INPUT:
N = 4296

OUTPUT:
INVALID INPUT
ALGORITHM

1. Start
2. Define a class named lab3
3. Define the main method.
4. Create a Scanner object to take input from the user.
5. Prompt the user to enter the number of boxes (N).
6. Initialize an integer array arr containing box capacities:
7. Initialize variables carton and box to keep track of the number of cartons and remaining boxes,
respectively. Set box equal to n.
8. Iterate through the array arr.
9. Print the total number of boxes (n) and the total number of cartons (carton).
10. End

PROGRAM

import java.util.Scanner;

public class lab3 //class begins

public static void main(String args[])

Scanner sc = new Scanner(System.in);

System.out.print("Enter number of boxes (N): ");

int n = sc.nextInt(); //accepting number of boxes

if (n < 1 || n > 1000)

System.out.println("INVALID INPUT");

return;

int arr[] = {48, 24, 12, 6};

int carton=0;

int box=n;
for(int i=0;i<arr.length;i++)

int box1 = box/arr[i];

box=box%arr[i];

carton+=box1;

if(box1!=0)

System.out.println(arr[i] + "*" +box1+ "="+(arr[i]*box1));

if(box!=0)

System.out.println("Remaining boxes" +box+ "*1=" +box);

carton++; //incrementing cartons

else

System.out.println("Remaining boxes = 0");

System.out.println("Total number of boxes: "+n);

System.out.println("Total number of cartons: "+carton); //displaying no of cartons

}//class ends

OUTPUT:
LAB ASSIGNMENT-4

Hamming numbers are positive integer numbers whose prime factors include 2,3 and 5 only. Example:n=6
is an hamming number as 6=2x3 .So its prime factors are limited to 2 ,3 n=8 is an hamming number as
8=2x2x2 and it has only 2 as its prime factors
n=90 is an hamming number as 90=2x3x3x5 which has only 2,3,5 as prime factors n=14 is not a hamming
number as 14=2x7 .It has 7 as one of its prime factor n=44 is not a hamming number as 44=2x2x11. It has
11 as one of its prime factors
Design a program to accept any positive integer number and check if it is a Hamming number or not.
Display the result with an appropriate message in the format specified below. The program should also
generate error message if a negative number is entered.
Test your program for the following data and some random data.
Example 1
INPUT: Enter any number: 3600
OUTPUT: 3600= 2 x 2 x 2 x 2 x 3 x 3 x 5 x 5
3600 IS A HAMMING NUMBER

Example 2
INPUT: Enter any number: 5832
OUTPUT: 5832= 2 x 2 x 2 x 3 x 3 x 3 x 3 x 3 x 3
5832 IS A HAMMING NUMBER
Example 3
INPUT: Enter any number: 7854
OUTPUT: 7854= 2 x 3 x 7 x 11 x 17
7854 IS NOT A HAMMING NUMBER
Example 4
INPUT: Enter a number: -120
OUTPUT: NEGATIVE NUMBER ENTERED

ALGORITHM
1. Start
2. Define a class named lab4
3. Define the main method.
4. Create a Scanner object to take input from the user.
5. Prompt the user to enter any number.
6. If the input number is negative: Print invalid input
7. Call the isHammingNumber method to check if the input number is a Hamming number.
8. Use while loops to divide num by 2, 3, and 5 until it is no longer divisible by them.
9. Check if num is equal to 1 after these divisions.
10. Print the prime factors and display whether the number is hamming or not
11. End.

PROGRAM
import java.util.*;
public class lab4 //class begins
{
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
System.out.print("Enter any number: ");
int num = scanner.nextInt(); //accepting the number
if(num<0)
{
System.out.println("Invalid input");
return;
}
if(isHammingNumber(num)) //method to check hamming number
{
printPrimeFactors(num);
System.out.println(num+"is a Hamming Number");
}
else
{
System.out.println(num+ "is not a hamming number");
}
}
public static boolean isHammingNumber(int num)
{
while(num%2==0)
{
num/=2;
}
while(num%3==0)
{
num/=3;
}
while(num%5==0)
{
num/=5;
}
return num==1;
}
public static void printPrimeFactors(int num) //method to print prime factors
{
System.out.print(num+ "=");
for(int i=2;i<=num;i++)
{
while(num%i==0)
{
System.out.print(i);
num/=i;
if(num!=1)
{
System.out.print(" X ");
}
}
}
System.out.println();
}
}//class ends

OUTPUT:
LAB ASSIGNMENT-5

The result of a quiz competition is to be prepared as follows:

The quiz has five questions with four multiple choices (A, B, C, D), with each question carrying 1 mark for
the correct answer. Design a program to accept the number of participants N such that N must be greater
than 3 and less than 11. Create a double-dimensional array of size (Nx5) to store the answers of each
participant row-wise. Calculate the marks for each participant by matching the correct answer stored in a
single-dimensional array of size 5. Display the scores for each participant and also the participant(s) having
the highest score.

Example: If the value of N = 4, then the array would be:

Q1 Q2 Q3 Q4 Q5

Participant
A B B C A
1

Participant
D A D C B
2

Participant
A A B A C
3

Participant
D C C A B
4

Key to the
D C C B A
question:

Note: Array entries are line fed (i.e. one entry per line)

Test your program for the following data and some random data.
Example 1

INPUT:
N=5
Participant 1 D A B C C
Participant 2 A A D C B
Participant 3 B A C D B
Participant 4 D A D C B
Participant 5 B C A D D
Key: B C D A A

OUTPUT:
Scores:
Participant 1 = 0
Participant 2 = 1
Participant 3 = 1
Participant 4 = 1
Participant 5 = 2
Highest Score:
Participant 5

Example 2

INPUT:
N=4
Participant 1 A C C B D
Participant 2 B C A A C
Participant 3 B C B A A
Participant 4 C C D D B
Key: A C D B B

OUTPUT:
Scores:
Participant 1 = 3
Participant 2 = 1
Participant 3 = 1
Participant 4 = 3
Highest Score:
Participant 1
Participant 4

Example 3

INPUT:
N = 12

OUTPUT:
INPUT SIZE OUT OF RANGE

ALGORITHM

1. Start
2. Import the Scanner class for user input.
3. Prompt the user to enter the number of participants (N).
4. Create a 2D array named "answers" to store the answers of each participant.
5. Create an array named "key" to store the answer key provided by the user.
6. Iterate through each participant's answers and compare them with the answer key.
7. Increment the score for each participant based on the correct answers.
8. Update the highest score if a participant's score surpasses the current highest score.
9. Display the scores of each participant.
10. Display the participant(s) with the highest score.
11. End the program.
PROGRAM
import java.util.Scanner;
public class lab5 //class begins
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter the Number of Participants (N): ");
int n = sc.nextInt(); //accepting number of participants
if (n <= 3 || n >= 11)
{
System.out.println("INPUT SIZE OUT OF RANGE.");
return;
}
int answers[][] = new int[n][5];//dda to store answers of each partiicpant
System.out.println("Enter answers of participants");
for (int i = 0; i < n; i++)
{
System.out.print("Participant " + (i+1));
for (int j = 0; j < 5; j++)
{
answers[i][j] = sc.next().charAt(0);
}
}
int key[] = new int[5];
System.out.print("Enter Answer Key:");
for (int i = 0; i < 5; i++) //displaying answer key
{
key[i] = sc.next().charAt(0);
}
int hScore = 0;
int score[] = new int[n];
System.out.println("Scores:");
for (int i = 0; i < n; i++)
{
for (int j = 0; j < 5; j++)
{
if (answers[i][j] == key[j])//if marks stored in da is eaul to marks in sda
{
score[i]++; //increment score
}
}
if (score[i] > hScore)
{
hScore = score[i];
}
System.out.println("Participant " + (i+1) + " = " + score[i]);
}
System.out.println("Highest Score:");
for (int i = 0; i < n; i++)
{
if (score[i] == hScore)
{
System.out.println("Participant " + (i+1));
}
}
}
} //class ends
OUTPUT:

You might also like