Java - Util.scanner: Import Public Class Public Void New - Int New

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 21

Question 3

Write a program in Java to store 10 numbers (including positive and negative


numbers) in a Single Dimensional Array (SDA). Display all the negative numbers
followed by the positive numbers without changing the order of the numbers.
Sample Input:

n[0] n[1] n[2] n[3] n[4] n[5] n[6] n[7] n[8] n[9]

15 21 -32 -41 54 61 71 -19 -44 52

Sample Output: -32, -41, -19, 44, 15, 21, 54, 61, 71, 52
import java.util.Scanner;

public class Numbers


{
public void displayNumbers() {
Scanner in = new Scanner(System.in);
int arr[] = new int[10];

System.out.println("Enter 10 numbers");
for (int i = 0; i < arr.length; i++) {
arr[i] = in.nextInt();
}

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


if (arr[i] < 0)
System.out.print(arr[i] + ", ");
}

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


if (arr[i] >= 0)
System.out.print(arr[i] + ", ");
}
}
}

Output
Question 4
Write a program in Java to store 20 numbers in a Single Dimensional Array (SDA).
Display the numbers which are prime.
Sample Input:

n[0] n[1] n[2] n[3] n[4] n[5] ... n[16] n[17] n[18] n[19]

45 65 77 71 90 67 ... 82 19 31 52

Sample Output: 71, 67, 19, 31


import java.util.Scanner;

public class PrimeNumbers


{
public void displayPrimeNumbers() {
Scanner in = new Scanner(System.in);
int arr[] = new int[20];

System.out.println("Enter 20 numbers");
for (int i = 0; i < arr.length; i++) {
arr[i] = in.nextInt();
}

System.out.println("Prime Numbers:");
for (int i = 0; i < arr.length; i++) {

if (arr[i] == 1)
continue;

boolean isPrime = true;


for (int j = 2; j <= arr[i] / 2; j++) {
if (arr[i] % j == 0) {
isPrime = false;
break;
}
}

if (isPrime)
System.out.print(arr[i] + ", ");
}
}
}

Output
Question 5
Write a program to accept name and total marks of N number of students in two
single subscript arrays name[ ] and totalmarks[ ].
Calculate and print:
(i) The average of the total marks obtained by N number of students.
[average = (sum of total marks of all the students)/N]
(ii) Deviation of each student's total marks with the average.
[deviation = total marks of a student - average]
import java.util.Scanner;

public class Marks


{
public void computeAvgNDev() {
Scanner in = new Scanner(System.in);
System.out.print("Enter number of students: ");
int n = in.nextInt();

String name[] = new String[n];


int totalmarks[] = new int[n];
int grandTotal = 0;

for (int i = 0; i < n; i++) {


in.nextLine();
System.out.print("Enter name of student " + (i+1) + ": ");
name[i] = in.nextLine();
System.out.print("Enter total marks of student " + (i+1) + ": ");
totalmarks[i] = in.nextInt();
grandTotal += totalmarks[i];
}

double avg = grandTotal / (double)n;


System.out.println("Average = " + avg);

for (int i = 0; i < n; i++) {


System.out.println("Deviation for " + name[i] + " = "
+ (totalmarks[i] - avg));
}
}
}

Output
Question 7
Write a program in Java using arrays:
(a) To store the Roll No., Name and marks in six subjects for 100 students.
(b) Calculate the percentage of marks obtained by each candidate. The maximum
marks in each subject are 100.
(c) Calculate the Grade as per the given criteria:

Percentage Marks Grade

From 80 to 100 A

From 60 to 79 B

From 40 to 59 C

Less than 40 D

import java.util.Scanner;

public class Grades


{
public void computePercentageNGrade() {
final int TOTAL_STUDENTS = 100;
Scanner in = new Scanner(System.in);

int rollNo[] = new int[TOTAL_STUDENTS];


String name[] = new String[TOTAL_STUDENTS];
int s1[] = new int[TOTAL_STUDENTS];
int s2[] = new int[TOTAL_STUDENTS];
int s3[] = new int[TOTAL_STUDENTS];
int s4[] = new int[TOTAL_STUDENTS];
int s5[] = new int[TOTAL_STUDENTS];
int s6[] = new int[TOTAL_STUDENTS];
double p[] = new double[TOTAL_STUDENTS];
char g[] = new char[TOTAL_STUDENTS];

for (int i = 0; i < TOTAL_STUDENTS; i++) {

System.out.println("Enter student " + (i+1) + " details:");


System.out.print("Roll No: ");
rollNo[i] = in.nextInt();
in.nextLine();
System.out.print("Name: ");
name[i] = in.nextLine();
System.out.print("Subject 1 Marks: ");
s1[i] = in.nextInt();
System.out.print("Subject 2 Marks: ");
s2[i] = in.nextInt();
System.out.print("Subject 3 Marks: ");
s3[i] = in.nextInt();
System.out.print("Subject 4 Marks: ");
s4[i] = in.nextInt();
System.out.print("Subject 5 Marks: ");
s5[i] = in.nextInt();
System.out.print("Subject 6 Marks: ");
s6[i] = in.nextInt();

p[i] = (((s1[i] + s2[i] + s3[i] + s4[i]


+ s5[i] + s6[i]) / 600.0) * 100);

if (p[i] < 40)


g[i] = 'D';
else if (p[i] < 60)
g[i] = 'C';
else if (p[i] < 80)
g[i] = 'B';
else
g[i] = 'A';
}

System.out.println();

for (int i = 0; i < TOTAL_STUDENTS; i++) {


System.out.println(rollNo[i] + "\t"
+ name[i] + "\t"
+ p[i] + "\t"
+ g[i]);
}
}
}

Output
Question 8
Write a program to accept a list of 20 integers. Sort the first 10 numbers in
ascending order and next the 10 numbers in descending order by using 'Bubble
Sort' technique. Finally, print the complete list of integers.
import java.util.Scanner;

public class BubbleSort


{
public void sortAscDsc() {
Scanner in = new Scanner(System.in);
int arr[] = new int[20];
System.out.println("Enter 20 numbers:");

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


arr[i] = in.nextInt();
}

//Sort first half in ascending order


for (int i = 0; i < arr.length / 2 - 1; i++) {
for (int j = 0; j < arr.length / 2 - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int t = arr[j + 1];
arr[j + 1] = arr[j];
arr[j] = t;
}
}
}

//Sort second half in descending order


for (int i = 0; i < arr.length / 2 - 1; i++) {
for (int j = arr.length / 2; j < arr.length - i - 1; j++) {
if (arr[j] < arr[j + 1]) {
int t = arr[j + 1];
arr[j + 1] = arr[j];
arr[j] = t;
}
}
}

//Print the final sorted array


System.out.println("\nSorted Array:");
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
}
}

Output
Question 11
Write a program to store 20 numbers in a Single Dimensional Array (SDA). Now,
display only those numbers that are perfect squares.

n[0] n[1] n[2] n[3] n[4] n[5] ... n[16] n[17] n[18] n[19]

12 45 49 78 64 77 ... 81 99 45 33

Sample Output: 49, 64, 81


import java.util.Scanner;

public class Squares


{
public void displayPerfectSquares() {
Scanner in = new Scanner(System.in);
int arr[] = new int[20];

System.out.println("Enter 20 numbers");
for (int i = 0; i < arr.length; i++) {
arr[i] = in.nextInt();
}

System.out.println("Perfect Squares are:");


for (int i = 0; i < arr.length; i++) {
double sr = Math.sqrt(arr[i]);
if ((sr - Math.floor(sr)) == 0)
System.out.print(arr[i] + ", ");
}
}
}

Output
Question 14
The annual examination result of 50 students in a class is tabulated in a Single
Dimensional Array (SDA) is as follows:

Roll No. Subject A Subject B Subject C

....... ....... ....... .......

....... ....... ....... .......

....... ....... ....... .......

Write a program to read the data, calculate and display the following:
(a) Average marks obtained by each student.
(b) Print the roll number and the average marks of the students whose average is
above. 80.
(c) Print the roll number and the average marks of the students whose average is
below 40.
import java.util.Scanner;

public class ExamResult


{
public void computeResult() {
final int TOTAL_STUDENTS = 50;
Scanner in = new Scanner(System.in);

int rollNo[] = new int[TOTAL_STUDENTS];


int sA[] = new int[TOTAL_STUDENTS];
int sB[] = new int[TOTAL_STUDENTS];
int sC[] = new int[TOTAL_STUDENTS];
double avg[] = new double[TOTAL_STUDENTS];

for (int i = 0; i < TOTAL_STUDENTS; i++) {


System.out.println("Enter student " + (i+1) + " details:");
System.out.print("Roll No: ");
rollNo[i] = in.nextInt();
System.out.print("Subject A Marks: ");
sA[i] = in.nextInt();
System.out.print("Subject B Marks: ");
sB[i] = in.nextInt();
System.out.print("Subject C Marks: ");
sC[i] = in.nextInt();
avg[i] = (sA[i] + sB[i] + sC[i]) / 3.0;
}
System.out.println("\nRoll No\tAverage Marks");
for (int i = 0; i < TOTAL_STUDENTS; i++) {
System.out.println(rollNo[i] + "\t" + avg[i]);
}

System.out.println("\nStudents with Average above 80:");


for (int i = 0; i < TOTAL_STUDENTS; i++) {
if (avg[i] > 80)
System.out.println(rollNo[i] + "\t" + avg[i]);
}

System.out.println("\nStudents with Average below 40:");


for (int i = 0; i < TOTAL_STUDENTS; i++) {
if (avg[i] < 40)
System.out.println(rollNo[i] + "\t" + avg[i]);
}
}
}

Question 15
Write a program to store 6 elements in an array P and 4 elements in an array Q.
Now, produce a third array R, containing all the elements of array P and Q. Display
the resultant array.

Input Input Output

P[ ] Q[ ] R[ ]

4 19 4

6 23 6

1 7 1

2 8 2

3   3

10   10

    19
Input Input Output

    23

    7

    8

import java.util.Scanner;

public class Arrays


{
public void copyArrays() {

Scanner in = new Scanner(System.in);

int P[] = new int[6];


int Q[] = new int[4];
int R[] = new int[10];
int i = 0;

System.out.println("Enter 6 elements of array P:");


for (i = 0; i < P.length; i++) {
P[i] = in.nextInt();
}

System.out.println("Enter 4 elements of array Q:");


for (i = 0; i < Q.length; i++) {
Q[i] = in.nextInt();
}

i = 0;
while(i < P.length) {
R[i] = P[i];
i++;
}

int j = 0;
while(j < Q.length) {
R[i++] = Q[j++];
}
System.out.println("Elements of Array R:");
for (i = 0; i < R.length; i++) {
System.out.print(R[i] + " ");
}
}
}

Give the output of the following


Question 1

int m[] = {2,4,6,8};


System.out.println(m[1] + " " + m[2]);

Output

4 6

Question 2
int a[] ={2,4,6,8,10};
a[0]=23;
a[3]=a[1];
int c= a[0]+a[1];
System.out.println("Sum = "+c);

Output

Sum = 27

Question 3
int a[]=new int [5];
a[0]=4; a[1]=8; a[2]=7; a[3]=12; a[4]=3;
System.out.println(a[2+1]);

Output

12

Question 4
int a[4]={2,4,6,8};
for(i=0;i<=1;i++)
{
s=a[i]+a[3-i];
System.out.println(s);
}

Output

10
10

Write short answers


Question 1

What is meant by Dimensional Array?

A dimensional array is a structure created in the memory to represent a number of values


of the same data type with the variables having the same variable name along with
different subscripts.

Question 2

Name the two types of Dimensional Array.

1. Single Dimensional Array


2. Double Dimensional Array

Question 3

What is the need of Dimensional Array? Explain.

Variables are useful for keeping track of a single piece of information but as we collect
more and more information, keeping the variables organized can be complicated. In such
situations, we need arrays to solve the problems in a much better and efficient way.

Question 4

'Array is a composite data type'. Explain this statement.

The data type that represents a number of similar or different data under single
declaration is called as composite data type. An array is a group or a collection of same
type of variables. Hence, Array is a composite data type.

Question 5

Define the following with their constructs:

(a) Single Dimensional Array


A Single Dimensional Array contains one row and one or more columns. The syntax of
declaring a Single Dimensional Array is:
<type> <array-variable>[] = new <type>[<size>];
OR
<type> [] <array-variable> = new <type>[<size>];
(b) Double Dimensional Array

Double Dimensional Array contains multiple rows and multiple columns. The syntax of
declaring a Double Dimensional Array is:
<type> <array-variable>[][] = new <type>[<rows>][<columns>];
OR
<type> [][] <array-variable> = new <type>[<rows>][<columns>];
Differentiate between the following
Question 1

Subscript and Subscripted variable

Subscript is the index of the element in the array whereas Subscripted variable is the name
of the array when it is used with a subscript to access a single element of the array.

Question 2

char a[5] and int a[5]

char a[5] is an array of char data type that can hold 5 characters whereas int a[5] is an
array of int data type that can hold 5 integer values.

Question 3

Ordinary variable and array variable

An ordinary variable can hold only one value whereas an array variable can refer to a
group of values of the same data type by using a subscript.
Question 4

Sorting and Searching

Sorting Searching

Sorting means to arrange the elements of the Searching means to search for a term or
array in ascending or descending order. value in an array.

Bubble sort and Selection sort are examples of Linear search and Binary search are
sorting techniques. examples of search techniques.

Question 5

Linear search and Binary search

Linear Search Binary Search

Linear search works on sorted and unsorted Binary search works on only sorted arrays
arrays (ascending or descending)

Each element of the array is checked Array is successively divided into 2 halves and
against the target value until the element is the target element is searched either in the first
found or end of the array is reached half or in the second half

Linear Search is slower Binary Search is faster

Question 6

Selection sort and Bubble sort

Selection sort Bubble sort

Selection Sort selects the smallest element from Bubble Sort compares adjacent
unsorted sub-array and swaps it with the leftmost elements and swaps them if they are in
unsorted element. wrong order.

Performs lesser number of swaps to sort the same Performs more number of swaps to
array relative to Bubble Sort sort the array

Selection Sort is faster Bubble Sort is slower


Question 7

length and length()

length length()

length() is a member method of


length is an attribute i.e. a data member of array.
String class.

It gives the length of an array i.e. the number of It gives the number of characters
elements stored in an array. present in a string

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy