0% found this document useful (0 votes)
6 views21 pages

Comp PRP 2

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)
6 views21 pages

Comp PRP 2

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/ 21

1.

WAP to create two arrays of integer type of size 5 and store the sum of both
the arrays in third array C

import java.util.Scanner;

class xyz {

public static void main() {

Scanner sc = new Scanner(System.in);

int A[] = new int [5];

int B[] = new int [5];

int C[] = new nt [5];

System.out.println(" Enter elements of [A] & [B] ");

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

A[i] = sc.nextInt();

B[i] = sc.nextInt();

C[i] = A[i] + B[i];

System.out.println("printing [i]");

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

System.out.println( C[i]);

}
OUTPUT

VARIABLE DESCRIPTION TABLE


VARIABLE DATA TYPE PURPOSE
A[] Int To store the elements
of first array
B[] Int To store the elements
of second array
C[] Int To store the sum of
the elements of the
two arrays

I Int Counter variable


2.WAP to create an array of size 10 and set it in ascending order using bubble sort
technique
import java.util.Scanner;
class sorting {
public static void main() {
Scanner sc = new Scanner(System.in);
int A[] = new int [10];
System.out.println(" Enter elements");
for ( int i = 0; i<10; i++) {
A[i] = sc.nextInt(); }
for(int j=0;j<9;j++) {
for(int k=0; k<9-j;k++) {
if(A[k]>A[k+1]){
int temp = A[k];
A[k] = A[k+1];
A[k+1] = temp;
}
}

}
System.out.println("printing sorted array");
for (int j = 0; j<=9; j++){
System.out.println( A[j]);
}
}
}
OUTPUT

VARIABLE DESCRIPTION TABLE


VARIABLE DATA TYPE PURPOSE
A[] int To store elements of
the array
I int Counter variable
J int Counter variable
K int Counter variable
temp int To interchange
values
3.WAP to create three arrays A and B of size 5 and C of size 10 . Merge A and B into
C. Print the elements of array C.

import java.util.Scanner;
class merging {
public static void main() {
Scanner sc = new Scanner(System.in);
int A []= new int [5] ;
int B []= new int [5] ;
int C []= new int [10] ;
System.out.println("Enter elements");
for(int i = 0; i<5; i++) {
A[i] = sc.nextInt();
B[i] = sc.nextInt();
}
for(int i = 0; i<5; i++) {
C[i] = A[i];
C[i+5 ] = B[i];
}
System.out.println("Printing C" );
for(int i = 0; i<10; i++) {
System.out.println(C[i]);
}
}
}
OUTPUT

VARIABLE DESCRIPTION TABLE


VARIABLE DATA TYPE PURPOSE
A[] int To store the
elements of first
array
B[] int To store the
elements of second
array
C[] int To store the
elements of both
the arrays
I int Counter variable
4. WAP to create 2d array of size 3*3. Create an array and print the array
in matrix form.

import java.util.Scanner;
class abc {
public static void main() {
Scanner sc = new Scanner(System.in);
int A [][]= new int [3][3] ;
System.out.println("Enter elements");

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


for(int j = 0; j<3; j++) {
A[i][j] = sc.nextInt();
}}
System.out.println("Printing matrix" );
for(int i = 0; i<3; i++) {
for(int j = 0; j<3; j++) {
System.out.print(A[i][j]+ " ");
}
System.out.println();
}
}
OUTPUT

VARIABLE DESCRIPTION TABLE


VARIABLE DATA TYPE PURPOSE
A[][] int To store
the
elements
of the
array
i int Counter
variable
j int Counter
variable
5.Define a class to accept values into an integer array of order 4*4 and check
whether it is a diagonal array or not.

import java.util.Scanner;
class diagonalarray {
public static void main() {
Scanner sc = new Scanner(System.in);
int A [][]= new int [4][4] ;
System.out.println("Enter elements");
int rtd = 0, ltd = 0;
for(int i = 0; i<4; i++) {
for(int j = 0; j<4; j++) {
A[i][j] = sc.nextInt(); }}
for(int i = 0; i<4; i++) {
for(int j = 0; j<4; j++) {
if ( i == j){
ltd= ltd + A[i][j];
System.out.println("Sum of left diagonal "+ ltd);}
if((i+j)==3){
rtd = rtd+A[i][j];
System.out.println("sum of right diagonal "+ rtd );}}
if(ltd == rtd)
System.out.println("An array is diagonal");
else
System.out.println("An array is not diagoanl");
}}}
OUTPUT
VARIABLE DESCRIPTION TABLE
VARIABLE DATA TYPE PURPOSE
A[][] int To store
elements of
the array
i int Counter
variable
j int Counter
variable
ltd int To store the
sum of left
diagonal
elements
rtd int To store the
sum of right
diagonal
elements
6.WAP to input a number and check whether if it is a pronic number or not.
class pronic
{
public static void main(int n)
{
int c = 0;
for( int i = 0; i<=n; i++)
{
if(n==i*(i+i))
{
c++;
break;
}}
if(c>0)
System.out.println(n + " is a pronic number");
else
System.out.println(n + " is not a pronic number");
}
}

OUTPUT
VARIABLE DESCRIPTION TABLE
VARIABLE DATA TYPE PURPOSE
n int To store a number
c int Counter variable to
check whether a
number is a pronic
number or not
i int Counter variable

7.WAP to enter a number and check whether it is a palindrome number or not.

class palindrome
{
public static void main(int n)
{
int x = n;
int rev = 0;
while(x>0)
{
int d = x%10;
rev= rev * 10+d;
x = x/10;
}
if(rev == n)
System.out.println(n + " is a palindrome number");
else
System.out.println(n + " is not a palindrome number");
}
}

OUTPUT

VARIABLE DESCRIPTION TABLE


VARIABLE DATA TYPE PURPOSE
n int To store a number
x int To store duplicate
value
rev int To store reverse of
number
d int To store the digits
8.WAP to input a number and check if it is an armstrong number or not.

class armstrong
{
public static void main(int n)
{
int x = n;
int sum = 0;
while(x>0)
{
int d = x%10;
sum = sum + (d*d*d);
x = x/10; }
if(sum == n)
System.out.println(n + " is an armstrong number");
else
System.out.println(n + " is not an armstrong number");
}
}
OUTPUT

VARIABLE DESCRIPTION TABLE


VARIABLE DATA TYPE PURPOSE
n int To store a number
x int To store the
duplicate value of the
number
sum int To store sum of the
cube of the digits
d int To store the digits
9.Define a class taximeter with the following specification
1. Member variables
 int taxinumber
 String name
 int km, bill
2.Member methods
 taximeter()
 void input() – to input taxinumber,name,bill
 void calculate () – to calculate bill
km travelled rate/km
<= 1 km 25
>1 to <= 6km 10
>6 to <= 12km 15
>12 to <= 18 km 20
>18km 25
4. void display()
5. void main()

import java.util.Scanner;
class taximeter {
int km,bill,taxinumber;
String name;
taximeter() {
km= 0;
bill = 0;
taxinumber = 0;
name = " "; }

void input () {
Scanner sc = new Scanner(System.in);
System.out.println(" input details");
km = sc.nextInt() ;
taxinumber = sc.nextInt();
name = sc.next(); }
void calculate() {
if(km<=1)
bill = 25;
else if ( km>1 && km <=6)
bill = km * 10;
else if (km >6 && km<=12)
bill = km * 15;
else if ( km>12 && km<= 18)
bill = km * 20;
else
bill = km * 25; }
void display() {
System.out.println("Name = " + name);
System.out.println("Taxinumber = " + taxinumber);
System.out.println("km travelled = " + km);
System.out.println("bill= " + bill); }
void main() {
taximeter ob = new taximeter ();
ob.input();
ob.calculate();
ob.display();

}
OUTPUT

VARIABLE DESCRIPTION TABLE


VARIABLE DATA TYPE PURPOSE
taxinumber int To store
taxinumber
name string To store name
km int To store total km
travelled
bill int To store the bill

10.Define a class showroom with the following specifications


1. Member variables
 String name;
 double cost, discount, amount
2. Member methods
 showroom()
 void input () – to input cost , name
 void calculate() – to calculate amount
Cost Discount%
<=10000 5
>10000 to <=20000 10
>20000 to <= 35000 15
>35000 20
3. void display()
4. void main()
import java.util.Scanner;
class showroom {
double cost,discount,amount;
String name;
showroom() {
cost= 0.0;
discount = 0.0;

amount = 0.0;
name = " "; }
void input () {
Scanner sc = new Scanner(System.in);
System.out.println(" input details");
cost = sc.nextDouble();
name = sc.next(); }
void calculate() {
if(cost<=10000)
discount = 0.05 * cost;
else if ( cost>10000 && cost<=20000)
discount = 0.10 * cost;
else if (cost >20000 && cost<=35000)
discount = 0.15 * cost;
else
discount = 0.20* cost;
amount = cost - discount; }
void display() {
System.out.println("Name = " + name);
System.out.println("cost = " + cost);
System.out.println("Discount = " + discount);
System.out.println("Amount= " + amount); }
void main() {
showroom ob = new showroom ();
ob.input();
ob.calculate();
ob.display();
}}

OUTPUT
VARIABLE DESCRIPTION TABLE
VARIABLE DATA TYPE PURPOSE
name String To store name
cost double To store cost
discount double To store discount
amount double To store the total
amount

You might also like