0% found this document useful (0 votes)
11 views20 pages

Java Programs for Number Series and Checks

Helpful
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)
11 views20 pages

Java Programs for Number Series and Checks

Helpful
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

/* Print the series 1,3,5,7,9,..........

99 */
class Q1_i
{
public static void main()
{
int i;
for(i=1;i<=99;i=i+2)
{
if(i==99)
[Link](i);
else
[Link](i+",");

}
}
}

/* Print the series 20,18,16,........2 */


class Q1_ii
{
public static void main()
{
int i;
for(i=20;i>=2;i=i-2)
{
if(i==2)
[Link](i);
else
[Link](i+",");
}
}
}

/* Print the series 2,4,8,16,.........256 */


class Q1_iii
{
public static void main()
{
int i;
for(i=1;i<=8;i++)
{
if(i==8)
[Link]((int)[Link](2,i));
else
[Link]((int)[Link](2,i)+",");
}
}
}

/* Print the series 1/3, 2/6, 3/9,........10/30 */


class Q1_iv
{
public static void main()
{
int i;
for(i=1;i<=10;i++)
{
if(i==10)
[Link](i+"/"+i*3);
else
[Link](i+"/"+i*3+",");
}
}
}

/* Print the series 1,12,123,1234,12345 */


class Q1_v
{
public static void main()
{
int i,term=0;
for(i=1;i<=5;i++)
{
term=term*10+i;
if(i==5)
[Link](term);
else
[Link](term+",");
}
}
}

/* Sum of the series 1+4+9+16+25 */


class Q2_i
{
public static void main()
{
int i,sum=0;
for(i=1;i<=5;i++)
{
sum=sum+i*i;

}
[Link]("Sum of the series=" +sum);
}
}

/* Product of the series 10*9*8*7*6*5*4*3*2*1 */


class Q2_ii
{
public static void main()
{
int i,product=1;
for(i=10;i>=1;i--)
{
product=product*i;

}
[Link]("Sum of the series=" +product);
}
}

/* Sum of the series 1+11+111+1111+11111 */


class Q2_iii
{
public static void main()
{
int i,term=0,sum=0;
for(i=1;i<=5;i++)
{
term=term*10+1;
sum=sum+term;

}
[Link]("Sum of the series ="+sum);
}
}

/* Sum of the series 10+20+30+..........+100 */


class Q2_iv
{
public static void main()
{
int i,sum=0;
for(i=10;i<=100;i=i+10)
{
sum=sum+i;
}
[Link]("Sum of the series = "+sum);

}
}

/* Sum of series 1+(1*2)+(1*2*3)+(1*2*3*4)+(1*2*3*4*5) */


class Q2_v
{
public static void main()
{
int i,j,sum=0,fact=1;
for(i=1;i<=5;i++)
{
fact=1;
for(j=1;j<=i;j++)
{
fact=fact*j;
}
sum=sum+fact;

}
[Link]("Sum of the series = "+sum);

}
}

/* Product of the series 1*(1+2)*(1+2+3)*(1+2+3+4)*(1+2+3+4+5) */


class Q2_vi
{
public static void main()
{
int i,j,sum=1,fact=0;
for(i=1;i<=5;i++)
{
fact=0;
for(j=1;j<=i;j++)
{
fact=fact+j;
}
sum=sum*fact;

}
[Link]("Sum of the series = "+sum);

}
}

/* product of the series 1/2 * 2/3 * 3/4 * ........9/10 */


class Q2_vii
{
public static void main()
{
double i;
double product=1.0,term=0.0;
for(i=1.0;i<=9.0;i=i+1.0)
{
term=i/(i+1.0);
product=product*term;
}
[Link]("Product of the series = "+product);

}
}

/* Sum of the series 2+5+10+17+......nth term */


import [Link].*;
class Q2_viii
{
public static void main()
{
int i,n,sum=0
;
Scanner sc= new Scanner([Link]);
[Link]("Enter the number of terms");
n=[Link]();
for( i=1;i<=n;i++)
{
sum=sum+(i*i+1);
}
[Link]("Sum of the series = "+sum);
}
}

/* Write a program to input a number and check whether it is a neon number or not. if a number such
that the sum of digits of the
number is equal to the number , then it is called a Neon number.
Example : 9
9 * 9 =81 , 8+1=9
So 9 is a neon number
*/
import [Link].*;
class Q3
{
public static void main()
{
int number,square,sum=0,rem;
Scanner sc=new Scanner([Link]);
[Link]("Enter a number");
number= [Link]();
square=number*number;
while(square>0)
{
rem=square%10;
sum=sum+rem;
square=square/10;
}
if(sum==number)
[Link](number+" is a neon number");
else
[Link](number+" is not a neon number");

}
}

/* Write a program to input a number and print the digits seperated by comma(,).
* Example 3456
* output : 6,5,4,3
*/

import [Link].*;
class Q4
{
public static void main()
{
int number,rem;
Scanner sc=new Scanner([Link]);
[Link]("Enter a number");
number= [Link]();
while(number>0)
{
rem=number%10;
number=number/10;
if(number>0)
[Link](rem+",");
else
[Link](rem);
}

}
}

/* Write a program to input a number and display the new number after reversing the digits of the
original number. The
program also displays the absolute difference between the original number and the reversed number.
Sample Input : 194
Sample output : 491
Absolute difference = 297
*/
import [Link].*;
class Q5
{
public static void main()
{
int number,reverse=0,difference,rem,temp;
Scanner sc = new Scanner([Link]);
[Link]("Enter a number");
number=[Link]();
temp=number;
while(temp>0)
{
rem=temp%10;
reverse=reverse*10+rem;
temp=temp/10;
}
difference=[Link](number-reverse);
[Link]("Reverse number is = "+reverse);
[Link]("Absolute difference is ="+difference);
}
}

/* Write a program to input a number and check whether the number is a Disarium Number or not.
* A number will be called Disarium if the sum of its digits powered by their respective position is equal
to the original
* number. Example 135
* 1pow 1 + 3 pow 2 + 5 pow 3 =1+9+125=135
*/
import [Link].*;
class Q6
{
public static void main()
{
int number,rem, sum=0, temp,digits=0;
[Link]("Enter a number");
Scanner sc= new Scanner([Link]);
number=[Link]();
temp=number;
while(temp>0) // counting the digits in number
{
rem=temp%10;
temp=temp/10;
digits++;
}
temp=number;
while(temp>0)
{
rem=temp%10;
sum=sum+(int)[Link](rem,digits);
digits--;
temp=temp/10;
}
if(sum==number)
[Link](number+" is a Disarium number");
else
[Link](number+" is not a Disarium number");

}
}

/* Write a program to input a number and print whether the number is a palindrome number or not. A
number is said to be
* Palindrome when its reverse is equal to the number itself.
* Example 141, 414, 595 etc.
*/
import [Link].*;
class Q7
{
public static void main()
{
int number,reverse=0,rem,temp;
Scanner sc = new Scanner([Link]);
[Link]("Enter a number");
number=[Link]();
temp=number;
while(temp>0)
{
rem=temp%10;
reverse=reverse*10+rem;
temp=temp/10;
}
if(reverse==number)
[Link](number+" is a palindrome number");
else
[Link](number+" is not a palindrome number");
}
}

/* Print sum of series a pow 1+ a pow 2 + a pow 3+.........+ a pow n where a and n entered by user */
import [Link].*;
class Q8_i
{
public static void main()
{
int a,n,sum=0,i;
Scanner sc=new Scanner([Link]);
[Link](" Enter the value of a");
a=[Link]();
[Link](" Enter the value of n");
n=[Link]();

for(i=1;i<=n;i++)
{
sum=sum+(int)[Link](a,i);
}
[Link]("Sum of series ="+sum);

}
}

/* Print the sum of the series 1* x pow 2 + 2* x pow 2+ 3* x pow 2 + 4* x pow 2 + .......+ n* x pow 2 */
import [Link].*;
class Q8_ii
{
public static void main()
{
int x,n,sum=0,i;
Scanner sc=new Scanner([Link]);
[Link](" Enter the value of x");
x=[Link]();
[Link](" Enter the value of n");
n=[Link]();

for(i=1;i<=n;i++)
{
sum=sum+i*(int)[Link](x,2);
}
[Link]("Sum of series ="+sum);

}
}

/* Print the product of the series 1pow 1 * 2 pow 2 * 3 pow 3 * ........ * n pow n */
import [Link].*;
class Q8_iii
{
public static void main()
{
int i,n,product=1;
Scanner sc= new Scanner([Link]);
[Link]("Enter the value of n");
n=[Link]();

for(i=1;i<=n;i++)
{
product=product*(int)[Link](i,i);
}
[Link]("Product of the series = "+product);
}
}

/* Print the sum of the series 1+2+4+7+11+16+......+n */


import [Link].*;
class Q8_iv
{
public static void main()
{
int i,n,sum=0,term=1;
Scanner sc= new Scanner([Link]);
[Link]("Enter the value of n");
n=[Link]();
for(i=0;i<=n;i++)
{
term=term+i;
[Link](term);
sum=sum+term;
}
[Link]("Sum of the series = "+sum);

}
}

/* Print the sum of the series 2/2! + 4/4! + 6/6! + 8/8! + 10/10! */
class Q8_v
{
public static void main()
{
double i,j,fact=1.0,sum=0.0;
for(i=2.0;i<=10.0;i=i+2.0)
{
fact=1.0;
for(j=i;j>=1.0;j--)
{
fact=fact*j;

}
[Link](fact);
sum=sum+(i/fact);
}
[Link]("Sum of the series = "+sum);
}
}

/* Print the sum of the series a/sqrt 2 + a/sqrt 4 + a/sqrt 6 +.........+ a/sqrt 10 */
import [Link].*;
class Q8_vi
{
public static void main()
{
double i,sum=0.0,a;
Scanner sc= new Scanner([Link]);
[Link]("Enter the value of a ");
a=[Link]();
for(i=2.0;i<=10.0;i=i+2.0)
{
sum=sum+(a/[Link](i));
}
[Link](" Sum of the series = "+sum);
}
}

/* print the sum of the series a pow 1 - a pow 2 + a pow 3 - a pow 4 + a pow 5 ....... a pow n */
import [Link].*;
class Q8_vii
{
public static void main()
{
double a,n,sum=0,i;
int sign=-1;
Scanner sc= new Scanner([Link]);
[Link]("Enter the value of a");
a=[Link]();
[Link]("Enter the value of n");
n=[Link]();

for(i=1.0;i<=n;i++)
{
sign=sign*-1;
sum=sum+sign*[Link](a,i);
}
[Link]("Sum of the series = "+sum);

}
}

/* Print the product of the series (x+1)* (x+3)*(x+5)*.......*(x+n) */


import [Link].*;
class Q8_viii
{
public static void main()
{
int x,n,i,product=1;
Scanner sc= new Scanner([Link]);
[Link]("Enter the value of x");
x=[Link]();
[Link]("Enter the value of n");
n=[Link]();
for(i=1;i<=n;i=i+2)
{
product=product*(x+i);
}
[Link]("Product of the series = "+product);
}
}

/* Write a program to input a number and print whether the number is Niven number or not.
A niven number is the number which is divisible by the sum of its digits.
Example 111 is a Niven number because it is divisible by sum of its digits i.e 1+1+1=3
*/
import [Link].*;
class Q9
{
public static void main()
{
int number,sum=0,rem,temp;
Scanner sc= new Scanner([Link]);
[Link]("Enter a number");
number=[Link]();
temp=number;
while(temp>0)
{
rem=temp%10;
sum=sum+rem;
temp=temp/10;
}
if(number%sum==0)
[Link](number+" is a Niven number");
else
[Link](number+" is not a Niven number");
}
}

/* Write a program to input a number and check whether it is an automorphic number or not. An
automorphic number is a number
whose square ends in the same digits as the original number.
Example 76 , square of 76 = 5776
25, square of 25 = 625
5 , square of 5 = 25
6 , square of 6=36
examples 0, 1, 5, 6, 25, 76, 376, 625, 9376, 90625, 109376, 890625, 2890625, 7109376, 12890625,
87109376,
212890625, 787109376, 1787109376, 8212890625, 18212890625, 81787109376, 918212890625,
9918212890625, 40081787109376
*/
import [Link].*;
class Q10
{
public static void main()
{
long number, temp, digits=0,rem,square;
Scanner sc= new Scanner([Link]);
[Link]("Enter the number");
number=[Link]();
temp=number;
while(temp>0) // digits count
{
rem=temp%10;
digits++;
temp=temp/10;
}
temp=number;
square=temp*temp;
if(square%[Link](10,digits)==number)
[Link](number+" is an automorphic number");
else
[Link](number+" is not an automorphic number");

}
}

/* Write a program to input a number and print whether the number is a Krishnamurthy number or not.
A krishnamurthy number is a number whose sum of the factorial of its digits is equal to the number
itself.
Example 1,2,145,40585
*/
import [Link].*;
class Q11
{
public static void main()
{
long number, temp,rem,fact=1,sum=0;
Scanner sc= new Scanner([Link]);
[Link]("Enter a number");
number=[Link]();
temp=number;
while(temp>0)
{
rem=temp%10;
for(long i=rem;i>=1;i--)
{
fact=fact*i;
}
sum=sum+fact;
fact=1;
temp=temp/10;
}
if(sum==number)
[Link](number+" is a Krishnamurthy number");
else
[Link](number+" is not a Krishnamurthy number");
}
}

/* Write a menu driven program to print the following series according to user's choice using switch
statement
(i) 1+12+123+1234+12345
(ii) 1/1 * 2/4 * 3/9 * ......*nth term
*/
import [Link].*;
class Q12
{
public static void main()
{
int choice ,i,n;
Scanner sc=new Scanner([Link]);
[Link]("Enter your choice 1 or 2");
choice=[Link]();
switch(choice)
{
case 1:
int term=0;
for(i=1;i<=5;i++)
{
term=term*10+i;
if(i==5)
[Link](term);
else
[Link](term+"+");
}
break;
case 2:
[Link]("Enter the number of terms");
n=[Link]();
for(i=1;i<=n;i++)
{
if(i==n)
[Link](i+"/"+i*i);
else
[Link](i+"/"+i*i+" * ");
}
}

}
}

/* Write a menu driven program to input n and print the sum of the following series
(i) s= x+ x pow 2 / !2 + x pow 3 / !3 + ........n
(ii) s= 1/ 1 pow 3 - 1/ 2 pow 3 + 1/ 3 pow 3 ..........1/ n pow 3
*/
import [Link].*;
class Q13
{
public static void main()
{
Scanner sc= new Scanner([Link]);
[Link]("Enter your choice 1 OR 2");
int choice=[Link]();
switch(choice)
{
case 1:
double sum=0.0;
int n,x;
long fact=1;
[Link]("Enter the value of x");
x=[Link]();
[Link]("Enter the number of terms");
n=[Link]();
for(int i=1;i<=n;i++)
{
fact=1;
for(int k=i;k>=1;k--)
{
fact=fact*k;
}
sum=sum+([Link](x,i)/fact);

}
[Link]("Sum of series =" +sum);
break;

case 2:
int terms,sign=-1;
double total=0.0;
[Link]("Enter the number of terms");
terms=[Link]();
for(int i=1;i<=terms;i++)
{
sign=sign*(-1);
total=total+sign*(1/[Link](i,3));
}
[Link]("Sum of series ="+total);
break;
default :
[Link]("Wrong Choice !");

}
}
}

/* Write a menu driven program to perform the following(use switch case statement)
(a) To print the series 0,3,8,15,24,....n terms (Value of n is to be input by the user)
(b) To find the sum of the series given below :
S = 1/2 + 3/4 + 5/6 + 7/8 + ....... + 19/20
*/
import [Link].*;
class Q14
{
public static void main()
{
Scanner sc= new Scanner([Link]);
[Link]("Enter your choice 1 OR 2");
int choice = [Link]();
switch(choice)
{
case 1:
int n;
[Link]("Enter the number of terms");
n=[Link]();
for(int i=1;i<=n;i++)
{
if(i==n)
[Link]((i*i)-1);
else
[Link]((i*i)-1+",");
}
break;

case 2 :
double S=0.0;
for(double i=1;i<=19;i=i+2)
{
S=S+(i/(i+1));
}
[Link]("Sum of the series =" +S);
break;

default :
[Link]("Wrong Choice !!!!");

}
}
}

/* Write a program to input a number and print whether the number is a Duck Number or not.
* A duck number is a number that has at least one zero present in it.
* Example : 3210 , 7056, 8430709 are all duc numbers.
*/
import [Link].*;
class Q15
{
public static void main()
{
long number,count=0,rem,temp;
Scanner sc=new Scanner([Link]);
[Link]("Enter a number");
number=[Link]();
temp=number;
while(temp>0)
{
rem=temp%10;
if(rem==0)
{
count++;
}
temp=temp/10;
}
if(count==0)
[Link](number+" is not a duck number");
else
[Link](number+" is a duck number");
}
}

/* Using the switch statement , write a menu driven program to


(i) To display all the factors of a number input by the user (including 1 and excluding the number itself)
number = 15
output : 1,3,5

(ii) To find and display the factorial of a number input by the user
number = 5
output : 120
*/
import [Link].*;
class Q16
{
public static void main()
{
Scanner sc= new Scanner([Link]);
[Link]("Enter your choice 1 OR 2 ");
int choice = [Link]();

switch(choice)
{
case 1:
[Link]("Enter a number");
int n=[Link]();
for(int i=1;i<n;i++)
{
if(n%i==0)
[Link](" "+i);
}
break;

case 2:
[Link]("Enter a number");
int number = [Link]();
long fact=1;
if(number==0)
[Link]("Factorial value of 0 is always 1");
if(number<0)
[Link]("No factorial value of a negative number");
if(number>0)
{
for(int i=number;i>=1;i--)
{
fact=fact*i;
}
[Link]("Factorial value of "+number+" is "+fact);
}
break;
default :
[Link]("Wrong Choice !!!!");
}
}
}

/* Write a program to input a number and find the smallest digit in the number
example : number =6524
smallest digit =2
*/
import [Link].*;
class Q17
{
public static void main()
{
Scanner sc= new Scanner([Link]);
[Link]("Enter a number");
int number=[Link]();
int temp=number;
int smallest=9,rem=0;
while(temp>0)
{
rem=temp%10;
if(rem<smallest)
smallest=rem;
temp=temp/10;
}
[Link]("Smallest digit in "+number+" is " +smallest);
}
}

/* Write a program to input a number and check whether it is a prime palindrome number or not.
A prime palindrome number is a positive integer that is prime as well as palindrome.
Example : 131 , 151, 181 , 191 etc.
*/
import [Link].*;
class Q18
{
public static void main()
{
Scanner sc= new Scanner([Link]);
[Link]("Enter a number");
int number = [Link]();
int i,count=0,reverse=0, rem=0,temp=number;
// Checking prime number or not
for(i=1;i<=number;i++)
{
if(number%i==0)
{
count++;
}
}
// Checking palindrome number or not
while(temp>0)
{
rem=temp%10;
reverse=reverse*10+rem;
temp=temp/10;
}
if(count==2&&reverse==number)
[Link](number+" is a prime palindrome number");
else
[Link](number+" is not a prime palindrome number");
}
}

/* Using a switch statement , write a menu driven program to


(a) Generate and display the first nterms of the fibonacci seriesv
0,1,1,2,3,5,8,....n th term
(b) Find the product of the even digits of an integer that is input
number = 29485
output = 64
*/
import [Link].*;
class Q19
{
public static void main()
{
Scanner sc= new Scanner([Link]);
[Link]("Enter your choice 1 OR 2");
int choice=[Link]();
switch(choice)
{
case 1:
[Link]("Enter the number of terms");
int n=[Link]();
int first_term=0,second_term=1;
[Link](first_term+","+second_term+",");
for(int i=1;i<=n-2;i++)
{
int new_term=first_term+second_term;
first_term=second_term;
second_term=new_term;

if(i==n-2)
[Link](new_term);
else
[Link](new_term+",");

}
break;

case 2:
[Link]("Enter the number");
int number=[Link]();
int product=1;
while(number>0)
{
int rem=number%10;
if(rem%2==0)
product=product*rem;
number=number/10;
}
[Link]("Product of even digits ="+product);
break;
default :
[Link]("Wrong Choice !!!!");
}
}
}

/* Write a program to input n whole numbers and do the following task


(a) Find the number of positive numbers
(b) Add the negative numbers
*/
import [Link].*;
class Q20
{
public static void main()
{
int count=0,sum=0;
Scanner sc= new Scanner([Link]);
[Link]("How many numbers you want to enter");
int n=[Link]();
for(int i=1;i<=n;i++)
{
[Link]("Enter a number");
int number=[Link]();
if(number>0)
count++;
if(number<0)
sum=sum+number;
}
[Link]("positive nubers are " +count);
[Link]("Sum of negative numbers is"+sum);
}
}

/* Write a program to accept a number and check whether it is a trimorphic number.


A trimorphic number is a number which is contained in the last digit(s) of its cube.
Example : 0, 1, 4, 5, 6, 9, 24, 25, 49, 51, 75, 76, 99, 125, 249, 251, 375, 376, 499, 501, 624, 625, 749, 751,
875, 999,
1249, 3751, 4375, 4999, 5001, 5625, 6249, 8751, 9375, 9376, 9999
*/
import [Link].*;
class Q21
{
public static void main()
{
long number,rem,cube,digit=0,temp;
Scanner sc=new Scanner([Link]);
[Link]("Enter a number");
number=[Link]();
cube=number*number*number;
temp=number;
while(number>0)
{
digit++;
number=number/10;
}
rem=cube%(long)[Link](10,digit);
if(rem==temp)
[Link](temp+" is a trimorphic number");
else
[Link](temp+" is not a trimorphic number");

}
}

Common questions

Powered by AI

A number is considered a Neon number if the sum of the digits of its square is equal to the number itself. For example, for the number 9, its square is 81, and the sum of the digits of 81 is 8 + 1 = 9, which is equal to the original number 9 .

A number is a Trimorphic number if its cube ends with the number itself. For example, if the cube of 5 is calculated as 125, since 125 ends with 5, the number 5 is a Trimorphic number .

A Disarium number is one where the sum of its digits powered by their respective positions equals the number itself. For example, for the number 135: 1 to the power of 1 is 1, 3 to the power of 2 is 9, and 5 to the power of 3 is 125. The sum of these values is 135, which matches the original number, confirming it is a Disarium number .

An Automorphic number is a number whose square ends with the same digits as the number itself. For example, the square of 76 is 5776, and it ends with 76, so 76 is an Automorphic number .

A Duck number contains at least one zero in its digits, but the zero cannot be the leading digit. For example, the number 3210 is a Duck number because it contains a zero (3, 2, 1, and 0), ensuring it meets this criterion .

To find the smallest digit in a number, repeatedly extract each digit by taking modulus 10, and compare it to a stored smallest value, updating the smallest value if the current digit is smaller. Continue this process until all digits have been checked. For example, in the number 6524, the smallest digit is 2 .

To reverse a number, extract each digit using modulus division, construct the reversed number by multiplying the current reversed number by 10 and adding the extracted digit. Once reversed, calculate the absolute difference by subtracting the reversed number from the original number and taking the absolute value of the result. For instance, if the original number is 194, its reverse is 491, resulting in an absolute difference of 297 .

A Niven number, also known as a Harshad number, is a number that is divisible by the sum of its digits without leaving a remainder. For example, 111 is a Niven number because the sum of its digits (1 + 1 + 1 = 3) divides 111 evenly .

A number is a Krishnamurthy number if the sum of the factorials of its digits is equal to the number itself. For instance, the number 145 is a Krishnamurthy number because 1! + 4! + 5! equals 1 + 24 + 120, which is 145 .

A prime palindrome is a number that is both a palindrome (reads the same forwards and backwards) and a prime number (divisible only by 1 and itself). For example, 131 and 151 are prime palindromes because they satisfy the conditions for both properties .

You might also like