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

Java Programs for Number Operations

The document contains a collection of Java programming exercises focused on various mathematical concepts, including sum of digits, palindrome numbers, patterns, spy numbers, and more. Each exercise includes a description, code implementation, variable description table, input, and output examples. The exercises aim to help users practice and understand fundamental programming and mathematical principles.

Uploaded by

Prajwal Lolekar
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)
6 views19 pages

Java Programs for Number Operations

The document contains a collection of Java programming exercises focused on various mathematical concepts, including sum of digits, palindrome numbers, patterns, spy numbers, and more. Each exercise includes a description, code implementation, variable description table, input, and output examples. The exercises aim to help users practice and understand fundamental programming and mathematical principles.

Uploaded by

Prajwal Lolekar
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

INDEX PAGE:

[Link]. EXPERIMENT PAGE DATE OF DATE OF


EXPERIMENT SUBMISSION
1 Sum of digits
2 Palindrome
number
3 Pattern1
4 Pattern2
5 Spy Number
6 Day of Week
7 Duck Number
8 Buzz number
9 Leap year
10 Fibonacci series
11 Tribonacci series
12 Perfect number
13 Pronic number
14 Magic number
15 Quadratic
equation
1) Write a program to accept a number from the user and find the sum of digits.

import [Link].*;
class SumOfDigits
{
public static void main (String args[])
{
Scanner sc = new Scanner ([Link]);
[Link]("Enter a Number : ");
int n = [Link]();

int rem=0, sum=0, num =n;

while (num > 0)


{
rem = num % 10;
sum = sum + rem;
num = num / 10;
}

[Link]("Sum of digits = " + sum);


}
}

Variable Description Table:

Variable name Data Type Description


n , num int To store value from the user
rem int Used to find reminder value
sum int To calculate and store sum of digits

INPUT:
Enter a Number :
1234

OUTPUT:
Sum of digits = 10

2) Write a program to accept a number from the user and check whether it is a Palindrome
number or not. A number is said to be Palindrome if the reverse of the number is equal to
the original number.
import [Link].*;
class Palin
{
public static void main (String args[])
{
Scanner sc = new Scanner ([Link]);
[Link]("Enter a Number : ");
int n = [Link]();

int rem=0, rev=0, num =n;

while (num > 0)


{
rem = num % 10;
rev = (rev * 10) + rem;
num = num / 10;
}

if (rev == n)
{
[Link](n + " is Palindrome Number");
}
else
{
[Link](n + " is not Palindrome Number");
}
}
}

Variable Description Table:

Variable name Data Type Description


n , num int To store value from the user
rem int Used to find reminder value
rev int To calculate and store reverse value

INPUT:
Enter a Number :
121

OUTPUT:
121 is Palindrome Number

INPUT:
Enter a Number :
123

OUTPUT:
123 is not Palindrome Number

3) Write a program to display the following pattern:

1 1 1 1
1 1 1 1
1 1 1 1

class Pattern1
{
public static void main (String args[])
{
for (int i=1; i<= 3; i++)
{
for(int j=1; j<=4; j++)
{
[Link]("1 ");
}
[Link]();
}
}
}

Variable Description Table:

Variable name Data Type Description


i,j int Loop counter

OUTPUT:
1111
1111
1111

4) Write a program to display the following pattern:

1
1 2
1 2 3
1 2 3 4
1 2 3 4 5

class Pattern2
{
public static void main (String args[])
{
for (int i=1; i<= 5; i++)
{
for(int j=1; j<=i; j++)
{
[Link]( j + " ");
}
[Link]();
}
}
}

Variable Description Table:

Variable name Data Type Description


i,j int Loop counter

OUTPUT:

1
1 2
1 2 3
1 2 3 4
1 2 3 4 5

5) Write a program to check whether a number is a SPY number or not. A number is


said to be a Spy number if the sum of all the digits is equal to the product of
all digits.

import [Link];

public class SpyNumber


{
public static void main(String args[]) {
Scanner in = new Scanner([Link]);

[Link]("Enter Number: ");


int num = [Link]();
int orgNum = num;

int digit, sum = 0, prod = 1;

while (num > 0) {


digit = num % 10;

sum += digit;
prod *= digit;
num /= 10;
}

if (sum == prod)
[Link](orgNum + " is Spy Number");
else
[Link](orgNum + " is not Spy Number");

}
}

Variable Description Table:

Variable name Data Type Description


num int To store the number from the user
orgNum int To store value of num
digit int To store remainder value
sum int To calculate sum of digits
prod int To calculate product of digits
INPUT:
Enter Number: 121

OUTPUT:
121 is not Spy Number

INPUT:
Enter Number: 22

OUTPUT:
22 is Spy Number

6) Write a program to accept day number of the week and display


day of the week.

import [Link].*;
class dayofweek
{
public static void main(String arg[])
{
Scanner sc = new Scanner([Link]);
[Link]("Enter day number : ");
int day = [Link]();

switch (day)
{
case 1: [Link]("Sunday");
break;
case 2: [Link]("Monday");
break;
case 3: [Link]("Tuesday");
break;
case 4: [Link]("Wednesday");
break;
case 5: [Link]("Thursday");
break;
case 6: [Link]("Friday");
break;
case 7: [Link]("Saturday");
break;
default: [Link]("Not a valid input");
}
}
}

Variable Description Table:

Variable name Data Type Description


day int To store day number from the user

INPUT:
Enter day number :
5

OUTPUT:
Thursday

INPUT:
Enter day number :
10

OUTPUT:
Not a valid input

7) Write a Java program to check whether a number is a Duck Number or not.

Note: A Duck number is a number which has zeroes present in it, but there should be
no zero present in the beginning of the number. For example 3210, 7056, 8430709 are
all duck numbers whereas 08237, 04309 are not.

import [Link];

public class DuckNumber


{
public static void main(String args[])
{
Scanner sc = new Scanner([Link]);
[Link]("Input a number : ");
String nstr = [Link]();

int len = [Link]();


int ctr = 0;
char chr;

for(int i=1;i<len;i++)
{
chr = [Link](i);
if(chr=='0')
ctr++;
}

char f = [Link](0);

if(ctr>0 && f!='0')


[Link]("Duck number");
else
[Link]("Not a duck number");
}
}

Variable Description Table:

Variable name Data Type Description


nstr String To store number in string format from the user
len int To find length of the string
ctr int To count number of zeros
chr char Extract character from the string number
f char Used for comparing with ‘0’

INPUT:
Input a number : 12350

OUTPUT:
Duck number
INPUT:
Input a number : 0124

OUTPUT:
Not a duck number

8) Write a program to check whether a number is a BUZZ number or not. Buzz numbers
are those numbers that are divisible by seven or end with seven. For example, 57 is a buzz
number because the number ends with seven. Another example is 28 because the number
isdivisible by seven.

import [Link].*;
class buzz
{
public static void main (String args[])
{
Scanner sc = new Scanner ([Link]);
[Link]("Enter the number : ");
int num = [Link]();
int n = num;
int rem=0, flag=0;

while (num>0)
{
rem = num%10;
if (rem==7)
{
flag=1;
break;
}
num = num/10;
}

if (flag==1 || n%7 == 0)
{
[Link](n + " is a Buzz number");
}
else
{
[Link](n + " is not a Buzz number");
}
[Link]();
}
}

Variable Description Table:

Variable name Data Type Description


num int To store the number from the user
n int To store value of num
rem int To store remainder value
flag int Flag variable to hold value as 1(true) and 0(false)

INPUT:
Enter the number :
3047

OUTPUT:
3047 is a Buzz number

INPUT:
Enter the number :
121

OUTPUT:
121 is not a Buzz number

9) Write a program to input a year and check whether it is a Leap year or not.

import [Link].*;
class leapyear
{
public static void main (String args[])
{
Scanner sc = new Scanner ([Link]);
[Link]("Enter year : ");
int year = [Link]();

if (((year%4 == 0) && (year %100 !=0)) || ( year%400 == 0))


{
[Link](year + " is a Leap year");
}
else
{
[Link](year + " is not a Leap year");
}
[Link]();
}
}

Variable Description Table:

Variable name Data Type Description


year int To store the year from the user

INPUT:
Enter year :
2013
OUTPUT:
2013 is not a Leap year

INPUT:
Enter year :
2016

OUTPUT:
2016 is a Leap year

10) Write a program to display Fibonacci series for n terms.

import [Link].*;
class fibonacci
{
public static void main (String args[])
{
Scanner sc = new Scanner ([Link]);
[Link]("Enter n terms : ");
int n = [Link]();

int f1=0, f2=1, f=0;

[Link]("Fibonacci series : ");


[Link](f1 + " , " + f2 + " , " );

for (int i=3; i<=n; i++)


{
f = f1 + f2;
[Link](f + " , " );
f1 = f2;
f2 = f;
}
[Link]();
}
}

Variable Description Table:

Variable name Data Type Description


n int To store the number from the user
f1 , f2 int To store previous values of the series
f int To calculate last two values of the series

INPUT:
Enter n terms :
10

OUTPUT:
Fibonacci series :
0 , 1 , 1 , 2 , 3 , 5 , 8 , 13 , 21 , 34 ,

11) Write a program to display Tribonacci series for n terms.

import [Link].*;
class tribonacci
{
public static void main (String args[])
{
Scanner sc = new Scanner ([Link]);
[Link]("Enter n terms : ");
int n = [Link]();

int f1=0, f2=1, f3 =1, f=0;

[Link]("Tribonacci series : ");


[Link](f1 + " , " + f2 + " , " + f3 + " , " );

for (int i=4; i<=n; i++)


{
f = f1 + f2 + f3;
[Link](f + " , " );
f1 = f2;
f2 = f3;
f3 = f;
}
[Link]();
}
}

Variable Description Table:

Variable name Data Type Description


n int To store the number from the user
f1 , f2, f3 int To store previous values of the series
f int To calculate last three values of the series

INPUT:
Enter n terms :
10

OUTPUT:
Tribonacci series :
0 , 1 , 1 , 2 , 4 , 7 , 13 , 24 , 44 , 81 ,

12) Write a program to input a number and check whether it is a perfect number or not. A
number whose sum of factors (excluding the number itself) is equal to the number is called
aperfect number.

import [Link].*;
class Perfect
{
void number(int n)
{
int num = n; int sum =0, rem=0;
for(int i=1; i<n; i++)
{
if (n%i == 0)
sum = sum + i;

}
if (n==sum)
[Link](n + " is a Perfect number");
else
[Link](n + " is not a Perfect number");
}

public static void main(String args[])


{
Scanner sc = new Scanner([Link]);
[Link]("Enter number : ");
int n = [Link]();

Perfect obj = new Perfect();


[Link](n);

}
}

Variable Description Table:

Variable name Data Type Description


n int To store the number from the user
num int To store value of n
rem int To store remainder value
sum int To calculate sum of factors
i int Loop counter

INPUT:
Enter number :
6

OUTPUT:
6 is a Perfect number

13) Write a program to input a number and check whether it is a Pronic number or not. A
pronic number is a number which is the product of two consecutive integers, that is, a
number of the form n(n + 1). The first few pronic numbers are: 0, 2, 6, 12, 20, 30, 42, 56,
72,90, 110, 132, 156, 182, 210, 240, 272, 306, 342, 380,etc.
import [Link];

class pronic
{
public static void main(String args[])
{
Scanner sc = new Scanner([Link]);
[Link]("Input a number : ");
int num = [Link]();
int ans = 0;

for(int i=0; i<num; i++)


{
if(i*(i+1) == num)
{
ans = 1;
break;
}
}

if(ans == 1)
[Link]("Pronic Number.");
else
[Link]("Not a Pronic Number.");
}
}
INPUT:
Input a number : 110

OUTPUT:
Pronic Number.

Variable Description Table:

Variable name Data Type Description


num int To store the number from the user
ans int To store value
i int Loop counter
14) Write a program to input a number and check whether it is a Magic number or not. If
thesingle digit is 1 then the number is called a magic number. For example, 325 is a magic
number because the sum of its digits (3+2+5) is 10, and again sum up the resultant (1+0),
we get a single digit (1) as the result. Hence, the number 325 is a magic number.

import [Link];
class MagicNumber
{
public static void main(String args[])
{
int n, remainder = 1, number, sum = 0;
Scanner sc = new Scanner([Link]);
[Link]("Enter a number you want to check: ");

n = [Link]();
number = n;
for (;number > 9;)
{
for (;number > 0;)
{
remainder = number % 10;
sum = sum + remainder;
number = number / 10;
}
number = sum;
sum = 0;
}

if (number == 1)
{
[Link]("The given number is a magic number.");
}
else
{
[Link]("The given number is not a magic number.");
}
}
}

Variable Description Table:


Variable name Data Type Description
n int To store the number from the user
number int To store value of n
remainder int To store remainder value
sum int To calculate sum
i int Loop counter

INPUT
Enter a number you want to check: 10

OUTPUT:
The given number is a magic number.

15) Write a program to display roots of quadratic equation ax2 + bx + c = 0.


(i) If b2 - 4ac < 0 , print “IMAGINARY ROOTS”

(ii) If b2 – 4ac > 0, print “ROOTS ARE REAL AND DISTINCT AND THE ROOT VALUES ARE”
root1 = b + √(b²-4ac))/(2a)
root2 = b - √(b²-4ac))/(2a)

(iii) If b2 – 4ac = 0 , print ‘Equal roots’ and ‘value of the root is –b/2a’.

import [Link].*;
class quadratic
{
public static void main (String args[])
{
Scanner sc = new Scanner ([Link]);
[Link]("Enter the value for a : ");
double a = [Link]();
[Link]("Enter the value for b : ");
double b = [Link]();
[Link]("Enter the value for c : ");
double c = [Link]();

double s = ([Link](b,2) - (4*a*c));

if (s<0)
{
[Link]("IMAGINARY ROOTS");
}
else if (s>0)
{
[Link]("ROOTS ARE REAL AND DISTINCT AND THE ROOT VALUES ARE ");
double root1 = (-b + [Link](s))/(2*a);
double root2 = (-b - [Link](s))/(2*a);
[Link]("Root1 = " + root1);
[Link]("Root2 = " + root2);
}
else
{
[Link]("EQUALS ROOTS AND VALUE OF THE ROOT IS " + ((-b/(2*a))));
}
[Link]();
}
}

Variable Description Table:

Variable name Data Type Description


a, b, c double To store the number from the user
s double To calculate equation
root1, root2 double To calculate root equation

INPUT:

Enter the value for a :


15
Enter the value for b :
20
Enter the value for c :
8

OUTPUT:
IMAGINARY ROOTS

You might also like