Number Programs
Number Programs
A
number is called a Lead number if the sum of the even digits is
equal to the sum of the odd digits. For example, 1452. [15]
Answer:
import java.util.*;
public class Q9
{
void main( )
{
Scanner sc = new Scanner (System.in);
int n = 0, sed=0, sod=0, d;
System.out.println("The number is :");
n = sc.nextlnt( );;
int cn=n;
while(cn > 0)
{
d=cn%10;
if ( d % 2== 0)
sed+=d;
else
sod+=d;
cn=cn/10;
}
if( sod==sed)
System.out.println("IT IS A LEAD NUMBER");
else
System.out.println("IT IS NOT A LEAD NUMBER");
}
}
Question 1
(a) State one difference between primitive literals float and double.
(b) What is an infinite loop. Write a statement for infinite loop.
(c) Arrange the operators in the order of higher precedence.
(1)++ (2)&& (3)>= (4)%
(d) What is final variable and static variable?
(e) What is number of bytes char occupies. Write its range also.
Answer:
(a)
Float Double
(b) An infinite loop occurs when there is no end to the iteration and loop
continues to run indefinitely.
E.g. for
System.out.println(“10”);
(d) When any variable is declared using final keyword it makes the value
of that variable as constant. It can’t be changed throughout the program.
E.g. final int temp = 90;
Static variable are those which can be used only by static methods. It has
only single copy throughout the class.
(e) 2 bytes and its range is 0 to 65,536
Question 2
(a) What is the difference between keyword parse and function valueOf( ).
Give example of each.
Answer:
Parse ValueOf
(b) Write a program code to accept a character input and check whether
its digit using its ASCII code. Print character using suitable message.
Answer:
class abc
char ch = sc.next( ).
charAt(0);
}
(c) What do you mean by block. Give one example.
Answer:
Block is a set of statements in a program.
Eg
{
System.out.printlnC’example of block statement”);
System.out.println(“End of block statement”);
}
(d) State the difference between entry controlled loop and exit controlled
loop.
Answer:
(e) Write two advantages of using function in the program. And explain
role of void in declaring functions?
Answer:
Advantages:
It makes the program divides into multiple modules.
Each module is independent in executing its dedicated task.
It helps in reusability of the code once written.
void makes sure that function doesn’t return anything.
Question 3
(a) What do you mean by function overloading? Explain with example.
Answer:
Function overloading is called when function with the same name is used
multiple times with different arguments.
Example:
class abc
{
public: area(int a);
(ii) Write a valid java program code to print the following array in matrix
form with 2 rows and 2 columns.
int mat[ ][ ] = {{2,6},{10,20}};
Answer:
for(int i =0;i<=2;i++)
for(int j =0;j<=2;j++)
System.out.print(mat[i][j]);
System.out.println( );
(iii) State the total size in bytes of the array a[4] of char data type and
p[4] of float data type.
Answer:
char a[4] it occupies 8 bytes.
float p[4] it occupies 16 bytes.
(iv) String abc = “helloR”;
StringBuffer str = new StringBuffer(abc);
int num = str.capacity( );
what will variable num stores the value?
Answer:
22
(d) Write a java statement for finding and displaying the position of last
space in string’str’.
Answer:
String str = “Blank”;
int a = str.lastlndexOf(‘ ‘));
System.out.println(a +” is the last position of”);
(e) Which one of the following returns the corresponding primitive data
type object from string object?
(i) Integer
(ii) readLine( )
(iii) valueOf( )
(iv) endsWith( )
Answer:
(i) int
(ii) String
(iii) int
(iv) boolean
Question 4
Write a program to input three sides of a triangle (s1, s2, s3). Using switch
case print whether a triangle is Equilateral, Isosceles, Right angled
triangle or scalene. The program should be used with menu and switch-
case.
Answer:
import java.util.*;
class Triangle
double s1 = sc.nextDouble( );
double s2 = sc.nextDouble( );
double s3 = sc.nextDouble( );
char ch ='0';
if(s1 == s2 &&s2 == s3 )
ch = 'E';
if(s1 == s2 || s2 == s3 || s1 == s3)
ch = I;
ch = 'R';
default: break;
Question 5
Write a program to find the sum of the following series:
x+x22!+x23!+x44!+…. nterms
Answer:
import java.util.*;
class series
int x = sc.nextlnt( );
int n = sc.nextlnt( );
int a = 1; int f;
f = 1;
for(int j = 1; j<=i;j++)
f = f*j;
sum = sum+(Math.pow(x,a)/f);
a++;
System.out.printlnC'sum: "+sum);
}
Question 6
A number is said to be NEON number if sum of digits of square of a
number is equal t:o the number itself.
Example:
INPUT N = 9, Output Square: 81 (where 8 + 1 = 9 so 9 is NEON number)
Write a program to find such numbers between 10 and 10000.
Answer:
import java.util.*;
class Neon
int a = 10;
while(sq >=0)
sum = sum + k;
sq = sq/10;
if(sum == a)
System.out.println(a);
a++,
}
Question 7
Write a program to perform binary search on the list of 10 integers
entered by user in ascending order to search for an element input by user,
if it’s found display the element along with its position else display the
message “search element not found”.
Answer:
import java.util.*;
class binsearch
{
public static void main( )
{
Scanner sc = new Scanner(System.in);
System.out.priintlnfEnter the 10 no.s in ascending order to be put in the
array");
int[ ] a = new int[10];
for(int i = 0; i< 10; i++)
a[i] = sc.nextlnt( );
System.out.println('Enter the no. to be searched in the array");
int n = sc.nextlnt( );
int start =0,pos=0;
int last =a.length - 1;
int mid = (start + last) / 2;
int found = 0;
while(start <= last)
{
if(n == a[mid])
{
pos = mid; found = 1; break;
}
if(n < a[mid])
mid = last - 1;
if(n > a[mid])
mid = start + 1;
}
if(found == 1)
System.out.println(n+" found at position "+pos);
else
System.out.println(n+" not found");
}
}
Question 8
Write a program to accept a word and convert into lowercase if it is in
uppercase and display the new word by replacing the VOWELS with the
character following it.
ex: Sample intput: VOWEL
Sample output: vpwfi
Answer:
import java.util.*;
class strlol
{
public static void main( )
{
Scanner sc = new Scanner(System.in);
Systenn.out.println("Enter a word");
String strl = sc.next( );
String str = strl .toLowerCase( );
String word = " ";
for(int i = 0; i <- str.length( )-1; i++)
{
char ch = str.charAt(i);
if (ch != 'a' && ch !='e' && ch != T && ch != 'o' && ch!='u')
word = word + ch;
if(ch=='a' || ch == 'e'|| ch == 'i' || ch == 'o' || ch == 'u')
{
ch ++;
word = word + ch;
}
}
System.out.println(word);
}
}
Question 9
Write a program in java to print the following output.
Answer:
import java.util.*;
class pattern
for(int i = 5; i>= 1; i- -)
int a = i-1;
System.out.print(" ");
System.out.println(j+" ");
if(a > 0)
System.out.println("*");
a- -;
System.out.println ( );
}
Variable Data Type Description
import java.util.*;
public class Q 8
void main( )
int n = 0, d = 0, p = 1;
n = sc.nextlnt( );
cn = cn/10;
if ( d == cn)
else
{
System.out.print(n + " is NOT a Cyclo Number " );
import java.util.Scanner;
int n,
fact = 1;
n = sc.nextInt();
fact = fact * i;
}
System.out.println("Factorial=" + fact);
(1*1*1)+(5*5*5)+(3*3*3) = 153
import java.util.Scanner;
/**
* @author AFAQ
*/
public class ArmstrongNumber
{
public static void main(String[] args)
{
int n,
cubeSum = 0, num, r;
Scanner sc = new Scanner(System.in);
System.out.print("Enter number=");
n = sc.nextInt();
num = n;
while (num > 0)
{
r = num % 10;
cubeSum = cubeSum + (r * r * r);
num = num / 10;
}
if (n == cubeSum)
{
System.out.println("Armstrong Number");
}
else
{
System.out.println("Not Armstrong Number");
}
}
}
Output:
Enter number=153
Armstrong Number
import java.util.Scanner;
}
}
Output:
Enter number=25
Automorphic Number
import java.util.Scanner;
public class BuzzNumber
{
public static void main(String[] args)
{
int n;
Scanner sc = new Scanner(System.in);
System.out.print("Enter n=");
n = sc.nextInt();
if (n % 10 == 7 || n % 7 == 0)
{
System.out.println("Buzz number");
}
else
{
System.out.println("Not Buzz number");
}
}
}
Output:
Enter n=147
Buzz number
import java.util.Scanner;
if(flag)
{
System.out.println("Circular Prime");
}
else
{
System.out.println("Not Circular Prime");
}
}
static boolean prime(int n)
{
// TODO code application logic here
int i = 2;
boolean flag = true;
while (n > i)
{
if (n % 2 == 0)
{
flag = false;
break;
}
i++;
}
return flag;
}
}
Output:
Enter number=137
Circular Prime
import java.util.Scanner;
Output:
Enter number=1234
Digit=1234
Words=One Two Three Four
import java.util.Scanner;
}
}
Output:
Enter number=205
Duck Number
import java.util.Scanner;
Output:
1
23
456
7 8 9 10
11 12 13 14 15
the greatest common divisor (gcd) of two or more integers, which
are not all zero, is the largest positive integer that divides each
of the integers. For example, the gcd of 8 and 12 is 4.
import java.util.Scanner;
import java.util.Scanner;
Output:
Enter number=31
Happy Number
What is Happy Number?
A happy number is a natural number in a given number base that
eventually reaches 1 when iterated over the perfect digital invariant
function for. Those numbers that do not end in 1 are -unhappy numbers.
import java.util.Scanner;
import java.util.Scanner;
Output:
Enter number=9
Neon Number
What is Neon Number ?
A neon number is a number where the sum of digits of square of the
number is equal to the number. For example if the input number is 9, its
square is 9*9 = 81 and sum of the digits is 9. i.e. 9 is a neon number.
import java.util.Scanner;
Output:
Enter number=204
Niven Number
What is Niven Number?
In mathematics, a Niven number (or harshad number) in a given
number base is an integer that is divisible by the sum of its digits
when written in that base.
A palindromic number is a number that remains the same when its digits
are reversed. Like 16461, for example,
import java.util.Scanner;
Output:
Enter number=12321
Palindrome Number
What is Palindrome Number?
A palindromic number is a number that remains the same when its digits
are reversed. Like 16461, for example,
A perfect number is a positive integer that is equal to the sum of
its positive divisors, excluding the number itself. For instance, 6
has divisors 1, 2 and 3, and 1 + 2 + 3 = 6, so 6 is a perfect
number.
import java.util.Scanner;
Output:
Enter number=6
Perfect Number
What is Perfect Number ?
A perfect number is a positive integer that is equal to the sum of its
positive divisors, excluding the number itself. For instance, 6 has divisors
1, 2 and 3, and 1 + 2 + 3 = 6, so 6 is a perfect number.
import java.util.Scanner;
Output:
Enter number=17
Number is prime.
What is Prime Number ?
A prime number is a natural number greater than 1 that cannot be formed
by multiplying two smaller natural numbers. A natural number greater
than 1 that is not prime is called a composite number. For example, 5 is
prime because the only ways of writing it as a product, 1 × 5 or 5 × 1,
involve 5 itself.
Output:
Enter number=1234
Reverce of Number=4321
What is Reverse Number?
If a number=1234, then reverse of number is 4321.
import java.util.Scanner;
Output:
Enter number=124
Not Special Number
What is Special Number?
A number is said to be special number when the sum of factorial of its
digits is equal to the number itself. Example- 145 is a Special Number as
1!+4!+5!=145.
A spy number is a number where the sum of its digits equals the product
of its digits. For example, 1124 is a spy number, the sum of its digits is
1+1+2+4=8 and the product of its digits is 1*1*2*4=8.
import java.util.Scanner;
Output:
Enter number=123
Spy Number
What is Spy Number?
A spy number is a number where the sum of its digits equals the product
of its digits. For example, 1124 is a spy number, the sum of its digits is
1+1+2+4=8 and the product of its digits is 1*1*2*4=8.
import java.util.Scanner;
Output:
Enter a=5
Enter b=7
Twin Prime
What is Twin Prime ?
A twin prime is a prime number that is either 2 less or 2 more than
another prime number—for example, either member of the twin prime
pair (41, 43). In other words, a twin prime is a prime that has a prime gap
of two.
import java.util.Scanner;
Output:
Enter number=23456
Unique Number
What is Unique Number?
A number is said to be unique , if the digits in it are not repeated. for
example, 12345 is a unique number. 123445 is not a unique number.
___________________________________________________________________________
____________
A number is called Disarium number if the sum of its power of the
positions from left to right is equal to the number.
Example:
11 + 32 + 53 = 1 + 9 + 125 = 135
import java.util.Scanner;
if(n==sum)
{
System.out.println("Disarium Number");
}
else
{
System.out.println("Not Disarium Number");
}
}
}
Output:
Enter number=135
Disarium Number
What is Disarium Number?
A number is called Disarium number if the sum of its power of the
positions from left to right is equal to the number.
___________________________________________________________________________
_______________
A tech number can be tech number if its digits are even and the number
of digits split into two number from middle then add these number if the
added number’s square would be the same with the number it will called a
Tech Number.
If the number is split in two equal halves,then the square of sum of these
halves is equal to the number itself. Write a program to generate and
print all four digits tech numbers.
Example:
import java.util.Scanner;
Output:
Enter number=2025
Tech Number
What is Tech Number ?
A tech number can be tech number if its digits are even and the number
of digits split into two number from middle then add these number if the
added number’s square would be the same with the number it will called a
Tech Number. If the number is split in two equal halves,then the square of
sum of these halves is equal to the number itself. Write a program to
generate and print all four digits tech numbers.
___________________________________________________________________________
_________
import java.util.Scanner;
Output:
Enter number=226
Magic Number
What is Magic Number?
Magic number is the if the sum of its digits recursively are calculated till a
single digit If the single digit is 1 then the number is a magic number.
Magic number is very similar with Happy Number.
import java.util.Scanner;
public class PronicNumber
{
public static void main(String[] args)
{
int n;
boolean flag=false;
Scanner sc = new Scanner(System.in);
System.out.print("Enter number=");
n = sc.nextInt();
for(int i=0; i < n; i++)
{
if(i*(i+1) == n)
{
flag =true;
break;
}
}
if(flag)
{
System.out.println("Pronic Number");
}
else
{
System.out.println("Not Pronic Number");
}
}
}
Output:
Enter number=42
Pronic Number
What isPronic Number?
A number is said to be a pronic number if product of two consecutive
integers is equal to the number, is called a pronic number.
For example, 6(2×3), 8(2x2x2), 15(3×5) are ugly numbers while 14(2×7)
is not ugly since it includes another prime factor 7. Note that 1 is typically
treated as an ugly number.
import java.util.Scanner;
Output:
Enter number=15
Ugly Number
What is Ugly Number?
A number is said to be an Ugly number if positive numbers whose prime
factors only include 2, 3, 5.
A number is called a Cyclo number if its first and last digit is same.
Input a number and verify whether it is a Cyclo Number or not. For example
52145
Answer:
import java.util.*;
public class Q 8
{
void main( )
{
Scanner sc new Scanner (System.in);
int n = 0, d = 0, p = 1;
System.out.print("Enter a number : ");
n = sc.nextlnt( );
d = n%10; int cn = n; while ( cn>=10)
{
cn = cn/10;
}
if ( d == cn)
{
System.out.print(n + " is a Cyclo Number " );
}
else
{
System.out.print(n + " is NOT a Cyclo Number " );
}
}
}