0% found this document useful (0 votes)
12 views22 pages

Java Programs for Basic Math Operations

The document contains multiple Java programming assignments, including tasks to calculate the perimeter and area of a circle, find the largest of three numbers, check for Armstrong numbers, and generate a payslip. Each assignment includes code snippets and expected outcomes. The document serves as a guide for practicing Java programming concepts and problem-solving skills.

Uploaded by

Ayush Srivastava
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views22 pages

Java Programs for Basic Math Operations

The document contains multiple Java programming assignments, including tasks to calculate the perimeter and area of a circle, find the largest of three numbers, check for Armstrong numbers, and generate a payslip. Each assignment includes code snippets and expected outcomes. The document serves as a guide for practicing Java programming concepts and problem-solving skills.

Uploaded by

Ayush Srivastava
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Lab1

1. WAP to find the perimeter and area of a circle given a value of radius. Use [Link] constant in program. If
radius is zero or less than zero then print " please enter non zero positive number ".
Code:
import [Link].*;

class perimeterarea
{
public static void main(String args[])
{
Scanner in = new Scanner([Link]);
int r;
double p, a;

[Link]("Please enter the value of the radius");


r = [Link]();

if(r <= 0)
{
[Link]("Please enter a non zero positive number");
}

p = 2 * [Link] * r;
a = [Link] * r * r;

[Link]("The perimeter of the circle is " + p);


[Link]("The area of the circle is " + a);

[Link]();
}
}

2. WAP to find the largest among three numbers x,y, and z. use if-then-else construct in Java.
Code:
import [Link].*;
class largest
{

public static void main(String args[])


{
int x,y,z;
Scanner in= new Scanner([Link]);
[Link]("Please enter the value of x");
x=[Link]();
[Link]("Please enter the value of y");
y=[Link]();
[Link]("Please enter the value of z");
z=[Link]();
if(x>z&&x>y)
{
[Link]("The largest number is"+ x);
}
else if(z>x&&z>y)
{
[Link]("The largest number is"+ z);
}
else
{
[Link]("The largest number is"+ y);
}
[Link]();

}
}

3. Consider First n even numbers starting from zero(0). WAP to calculate sum of all the numbers divisible
by 3 from 0 to n. Print the sum.

Code:
import [Link].*;
class divisible
{
public static void main(String args[])
{
int n,sum=0;
Scanner in=new Scanner([Link]);
[Link]("Please enter the value of n");
n=[Link]();
for(int i=0;i<=n;i++)
{
if(i%2==0&&i%3==0)
{
[Link](i);
sum=sum+i;
}
}
[Link]("The sum of the number is "+sum);

[Link]();
}
}

4. WAP to check whether the number is an Armstrong number or not.


Code:
import [Link].*;
class armstrong
{
public static void main(String args[])
{
Scanner in=new Scanner([Link]);
int a,count=0,c,sum=0,temp;
[Link]("Please enter a number");
a=[Link]();
temp=a;
while(temp>0)
{
count++;
temp=temp/10;
}
temp=a;
while(temp>0)
{
c=temp%10;
sum=sum+(int)[Link](c,count);
temp=temp/10;
}
if(sum==a)
{
[Link]("It is an armstrong number");
}
else
{
[Link]("It is not an amrstrong number");
}
[Link]();
}
}
Lab 2
1. Find the sum of the digits of a number.
CODE:
import [Link].*;
class sum_of_digits
{
public static void main()
{
int num,sum=0,a;
Scanner in=new Scanner([Link]);
[Link]("Please enter any number");
num=[Link]();
if(num!=0)
{
while(num>0)
{
sum=sum+num%10;
num=num/10;
}
[Link]("The sum of the digits is "+sum);
}
else
{
[Link]("Invalid input");
}
}
}
OUTCOME:

2. Program to print Armstrong number between 1 to 1000.


Code:
import [Link].*;
class arm1_1000
{
public static void main()
{
int i;
Scanner in=new Scanner ([Link]);
[Link]("The armstrong numbers between 1 and 1000 are:");
for(i=1;i<=1000;i++)
{
int a=i;
int sum=0;
int temp=i;
int count=0;
while(temp>0)
{
count++;
temp=temp/10;
}
while(i>0)
{
int b=i%10;
sum=sum+(int)[Link](b,count);
i=i/10;
}
if(sum==a)
{
[Link](a);
}
i=a;
}
}
}

Outcome:

3. WAP in Java to taka a set of numbers from the user as input and find the HCF of those numbers scanned
as input from the user.
Code:
import [Link].*;
class hcf
{
public static void main()
{
Scanner in=new Scanner([Link]);
int n,num;
[Link]("How many numbers do you want to enter");
n=[Link]();
[Link]("Please enter "+n+" numbers");
int hcf=[Link]();
for(int i=1;i<n;i++)
{
num=[Link]();
int a=hcf;
int b=num;
while(b!=0)
{
int temp=b;
b=a%b;
a=temp;
}
hcf=a;
}
[Link]("HCF of all numbers is:-" +hcf);
}
}
Outcome:

4. Print numbers in triangle and pyramid vice


1
121
12321
1234321
123454321
Code:
import [Link].*;
class pattern
{
public static void main()
{
Scanner in=new Scanner([Link]);
int row;
[Link]("Please enter the number of rows");
row=[Link]();
[Link]("The triangle pattern is");
for(int i=1;i<=row;i++)
{
for(int j=1;j<=i;j++)
{
[Link](j);
}
[Link]();
}
[Link]("The pyramid pattern is");
for(int i=1;i<=row;i++)
{
for(int j=1;j<=row-i;j++)
{
[Link](" ");
}
for(int j=1;j<=i;j++)
{
[Link](j);
}
for(int j=i-1;j>=1;j--)
{
[Link](j);
}
[Link]();
}
}
}

Outcome:
5. WAP to generate a payslip of an employee.
The structure should be as follows:
Basic pay should be scanned as an input from the user.
DA= 50% of Basic pay
HRA= 20 % of Basic pay
PF= 14% of Basic
Gross pay= Basic Pay + + DA + HRA – PF

import [Link].*;
class payslip
{
public static void main()
{
double da,hra,pf,bp,gp;
Scanner in=new Scanner([Link]);
[Link]("Please enter the basic pay");
bp=[Link]();
da=0.5*bp;
hra=0.2*bp;
pf=0.14*bp;
gp=bp+da+hra-pf;
[Link]("The payslip is as follows\nDA "+da+"\nHRA "+hra+"\nPF "+pf+"\nGross Pay "+gp);
}
}
Outcome:
Lab 3
1. WAP in Java to sort numbers in an array.
Code:
import [Link].*;
class sorting
{
public static void main()
{
int s;
int a[]=new int[10];
Scanner in=new Scanner([Link]);
[Link]("Please enter all the elements in the array");
for(int i=0;i<10;i++)
{
a[i]=[Link]();
}
for(int i=0;i<10;i++)
{
for(int j=0;j<9;j++)
{
if(a[j]>a[j+1])
{
s=a[j];
a[j]=a[j+1];
a[j+1]=s;
}
}
}
for(int i=0;i<10;i++)
{
[Link](a[i]+" ");
}
}
}
Outcome:

2. Wap to find highest marks and average marks secured by a student in 5 Subjects.
Code:
import [Link].*;
class highest_average_array
{
public static void main()
{
Scanner in=new Scanner([Link]);
int m[]=new int[5];
[Link]("Please enter the marks of all 5 subjects");
for(int i=0;i<5;i++)
{
m[i]=[Link]();
}
int highest=m[0];
for(int i=1;i<5;i++)
{
if(m[i]>highest)
{
highest=m[i];
}
}
int total=0;
for(int i=0;i<5;i++)
{
total=total+m[i];
}
double avg=total/5;
[Link]("Highest Marks"+ highest);
[Link]("Average Marks"+ avg);
}
}
Outcome:

3. Two one dimensional arrays A and B are given, which are sorted in ascending order. Write a program to
merge them into a single sorted array c that contains every item from arrays A and B in ascending order.

Code:
import [Link].*;
public class Merge {
public static void main()
{
Scanner in= new Scanner([Link]);
[Link]("Size A");
int m = [Link]();
int[] A = new int[m];
for (int i = 0; i < m; i++)
{
A[i] = [Link]();
}
[Link]("Size B: ");
int n = [Link]();
int[] B = new int[n];
for (int i = 0; i < n; i++)
{
B[i] = [Link]();
}
int[] C = new int[m + n];
int i = 0, j = 0, k = 0;
while (i < m && j < n)
C[k++] = A[i] <= B[j] ? A[i++] : B[j++];
while (i < m)
C[k++] = A[i++];
while (j < n)
C[k++] = B[j++];
for (i = 0; i < [Link]; i++)
{
[Link](C[i] + " ");
}

}
}
Outcome:

4. Write a program that reads 16 integers from the user, stores them in 5 by 4 matrix, the last row of the
matrix should contain the sums of the columns calculated by your program. Then the whole matrix is
printed.

Code:
import [Link].*;
public class Matrix {
public static void main()
{
Scanner in= new Scanner([Link]);
int[][] matrix = new int[5][4];
[Link]("Enter 16 integers:");
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
matrix[i][j] = [Link]();
}
}
for (int j = 0; j < 4; j++) {
int sum = 0;
for (int i = 0; i < 4; i++) {
sum = sum + matrix[i][j];
}
matrix[4][j] = sum;
}
[Link]("\nMatrix with column sums:");
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 4; j++) {
[Link](matrix[i][j] + "\t");
}
[Link]();
}
}
}
Outcome:
import [Link].*;
public class Matrix {
public static void main()
{
Scanner in= new Scanner([Link]);
int[][] matrix = new int[5][4];
[Link]("Enter 16 integers:");
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
matrix[i][j] = [Link]();
}
}
for (int j = 0; j < 4; j++) {
int sum = 0;
for (int i = 0; i < 4; i++) {
sum = sum + matrix[i][j];
}
matrix[4][j] = sum;
}
[Link]("\nMatrix with column sums:");
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 4; j++) {
[Link](matrix[i][j] + "\t");
}
[Link]();
}
}
}
Outcome:

5. Write a method that returns a new array by eliminating the duplicate values in the array. Write a JAVA
program that reads in at least ten integers, invokes the method, and displays the results.

Code:
import [Link];

public class ArrayDuplicateRemoverSimple {

public static int[] eliminateDuplicates(int[] list) {


int[] uniqueArray = new int[[Link]];
int uniqueCount = 0;

for (int i = 0; i < [Link]; i++) {


boolean isDuplicate = false;
for (int j = 0; j < uniqueCount; j++) {
if (list[i] == uniqueArray[j]) {
isDuplicate = true;
break;
}
}
if (!isDuplicate) {
uniqueArray[uniqueCount] = list[i];
uniqueCount++;
}
}

int[] finalArray = new int[uniqueCount];


for (int i = 0; i < uniqueCount; i++) {
finalArray[i] = uniqueArray[i];
}

return finalArray;
}

public static void main(String[] args) {


Scanner scanner = new Scanner([Link]);
final int arraySize = 10;
int[] originalArray = new int[arraySize];

[Link]("Please enter " + arraySize + " integers:");

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


[Link]("Enter integer #" + (i + 1) + ": ");
originalArray[i] = [Link]();
}

int[] newArray = eliminateDuplicates(originalArray);

[Link]("\nOriginal array: ");


for (int value : originalArray) {
[Link](value + " ");
}

[Link]("\nNew array with duplicates removed: ");


for (int value : newArray) {
[Link](value + " ");
}

[Link]();
}
}
Outcome:

You might also like