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

Numbers

The document provides a comprehensive overview of various programming concepts and examples, including loops, conditionals, and functions in Java. It covers topics such as output generation, iterations, and specific algorithms for tasks like checking for Armstrong numbers, prime numbers, and calculating factorials. Additionally, it includes multiple coding exercises demonstrating the implementation of these concepts.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views38 pages

Numbers

The document provides a comprehensive overview of various programming concepts and examples, including loops, conditionals, and functions in Java. It covers topics such as output generation, iterations, and specific algorithms for tasks like checking for Armstrong numbers, prime numbers, and calculating factorials. Additionally, it includes multiple coding exercises demonstrating the implementation of these concepts.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 38

Numbers

% rem
/ quo

567 %10= 7
567 /10= 56
Iterations –Section A
1. System.exit(0): used to terminate the program.
2. break: used to stop execution of a
block ( loop , switch or if )
3. continue: when the condition matches the control skips rest of the
statements for that value and go to the next iteration.
for( i=1;i<=4 ;i++) I i<=4 i==3 sopln(i)
{ if( i==3) continue; 1 1<=4 1==3f 1
2 2<=4 2==3f 2
Sopln(i);} 3 3<=4 3==3t
4 4<=4 4==3 4
4. delay loop: These are loops that have no other function than to kill
time. Delay loops can be created by specifying an empty target
statement.
Detect the output a b while(a>++b) a-- sopln(a +” “+b)
1. a=10; b=3; 10 3 10>4 10 94
while(a>++b) 9 4 9>5 9 8 5
{ a--; Sopln(a+” “+b);} 8 5 8>6 8 7 6
int i=1; 7 6 7>7f
loop executes 3 times
2. for( i=1; i<=5; i++) output is 9 4
12345
Sop(i); 6 85
Sopln(); 76
Sop(i); x y while(x<=y) y=y/x
5 50 5<=50 t 50/5=10
3. x=5; y=50 ; 5 10 5<=10 t 10/5=2
while(x<=y) 5 2 5<=2f
y/=x; output is 2
sopln(y); loop executes 2 times
Write the output of the following program code :
char ch;
int x=97;
do x ch sop( ch +” “) x%10==0
97 a a b c d
x<=100
97%10==0f 98<=100
{ 98 b 98%10==0 f 99<=100
99 c 99%10==0 f 100<=100
ch=(char)x; 100 d 100%10==0 t
System.out.print(ch+" ");
if(x%10==0) Output a b c d
break; Loop executes 4 times

++x;
} while(x<=100);
Analyze the given program segment and answer the following
questions:
(i) Write the output of the program segment
(ii) How many times does the body of the loop gets executed?
for (int m=5; m<=20; m+=5)
m m<=20 m%3==0 m%5==0
{ if (m%3==0) 5 5<=20 5%3==0f 5%5==0 t 5
break; 10 10<=20 10%3==0 f 10%5==0 t 10
15 15<=20 15%3==0 t
else
if (m%5==0) Output 5
10
System.out.println(m); Loop executes 3 times
continue;
}
Give the output of the following program segment and also mention
the number of times the loop is executed:
int a,b;
for (a = 6, b = 4; a <= 24; a = a + 6)
{
if (a%b ==0) a b a%b ==0
break; 6 4 6%4==0 f
} 12 4 12%4==0 t
output 12
System.out.println(a); loop executes 2 times
Give the output of the following program segment and also mention how
many times the loop is executed:
int i;
for ( i = 5 ; i > 10; i ++ )
System.out.println( i ); I i>10 sopln(i) sopln(i*4)
5 5>10 5*4=20
System.out.println( i * 4 );

loop runs 0 times


output 20
give the output of following code and mention how many
times the loop will execute?
int i;
I i>=1 i%2==1 Sop( i+” “)
for( i=5 ; i>=1 ;i--) 5 5>=1 5%2==1t
{ 4 4>=1 4%2==1f 4 2
3 3>=1 3%2==1t
if(i%2 ==1) 2 2>=1 2%2==1f
continue; 1 1>=1 1%2==1t
0 0>=1 f
System.out.print( i+ '' ''); output 4 2
} Loop runs 5 times
WAP to read a number and find the sum of digits
456 4+5+6=15
Sopln(“enter a no”);
int n=sc.nextInt(); N n>0 rem=n%10 sum+rem n=n/10
456 456>0 6 0+6=6 45
int sum=0; int rem=0; 457 45>0 5 6+5=11 4
458 4>0 4 11+4=15 0
while( n>0)
0 0>0 f
{ rem=n%10; 6 5 4
sum=sum+rem; 0+6+5+4
n=n/10; 45 4 0
}
Sopln(“Sum is “+sum);}}
Using String 345
Scanner sc = new Scanner(System.in);
System.out.println("Enter a number");
String s=sc.next();String s1="";
int l=s.length(); int sum=0;
for(int i=0;i<l;i++)
{
s1=""+s.charAt(i);
int x=Interger.parseInt(s1);
sum=sum+x;}
int n=Integer.toString(s);
If( n%sum==0)
WAP to read a number and check if it is a Armstrong number
Eg= 153=13+53+33=153
int n=sc.nextInt();
int sum=0; int rem=0; int n1=n;
while( n>0)
{ rem=n%10;
sum=sum+(rem*rem*rem);
n=n/10;}
if ( n1==sum) Sopln(“Armstrong no”);
else Sopln(“Not”);
WAP to read a number and print the reverse of the number
234 reverse 432
Sopln(“Enter a number”);
int n=sc.nextInt();
n n>0 rem=n%10 rev*10+rem n/10
234 234>0 4 0*10+4=4 23
int rev=0; int rem=0;
235 23>0 3 4*10+3=43 2
while(n>0) 2362>0 2 43*10+2=432 0
{ rem=n%10; 0 0>0
rev=rev*10+rem;
n=n/10;
}
Sopln(rev) ;}}
WAP to read a number and check if it a palindrome number.
Eg= 121 reverse of 121 is 121 it is palindrome
int n=sc.nextInt(); int rev=0; int rem=0; int n1=n;
While(n>0)
{ rem=n%10;
rev=rev*10+rem;
n=n/10;
} if( n1==rev) Sopln(“Palindrome”); else Sopln(“not”);
WAP to read a number find if it is niven number.
Eg 81= 8+1=9 if 9 completely divides n then niven.
int n=sc.nextInt();
int sum=0; int rem=0; n1=n; n n>0 rem sum n
81 81>0 1 0+1=1 8
While(n >0) 8 8>0 8 1+8=9 0
0 0>0 f
{ rem=n%10;
sum=sum+rem;
n=n/10;}
If( n1% sum==0) Sopln(“Niven”);
else Sopln(“Not”);}}

WAP to read number and check it is neon number.


Eg 9 square of 9 is 81… 8+1 is 9
WAP to read number and check it is neon number.
Eg 9 square of 9 is 81… 8+1 is 9
int n=sc.nextInt();
int n1=n*n;
int sum=0; int rem=0;
while( n1>0)
{ rem=n1%10;
sum=sum+rem;
n1=n1/10;
}
If( sum==n ) Sopln(“Neon no”); else Sopln(“Not neon”);}}
WAP to read a number and check if it is buzz number
Eg 49 it should be divisible by 7 or the number should have
7 at the end
int n=sc.nextInt();
If(n%10==7 || n%7==0)
Sopln(“Buzz no”);
Else
Sopln(“Not”);}}
WAP to read a number and find its factorial
Eg 5 = 1*2*3*4*5
int n=sc.nextInt(); n f i<=n f=f*i
5 1 1<=5 1*1=1
int f=1; 2<=5 1*2=2
For( int i=1; i<=n; i++) 3 <=5 2*3=6
4<=5 6*4=24
{ f=f*i; 5<=5 24*5=120
} sopln(“Factorial is “+ f);}} 6<=5 f
WAP to read a number and find its factors

int n=sc.nextInt(); n i i<=n n%i==0 sopln (i)


5 1 1<=5 5%1==0 1
for(int i=1;i<=n ;i++) 2 2<=5 5%2=1
3 3<=5 5%3=2
{ if( n%i==0) 4 4<=5 5%4=1
sopln(“Factor is ”+ i); 5 5<=5 5%5=0 5

}
}}
WAP to read a number and check if it prime number
Sopln(“Enter a number”);
int n=sc.nextInt(); int ctr=0;
n I i<=n n%i==0 ctr
for( int i=1; i<=n;i++) 5
{ if( n%i==0)
ctr++;}
if(ctr==2)
Sopln(“Prime”); else
Sopln(“ Not Prime”);}}
WAP to read a number and check if it perfect number
Eg 6 factor of 6 is 1,2,3 sum of factors is 6 . 6 is perfect number

int n=sc.nextInt();
Int sum=0;
for( int i=1;i<n ;i++)
{ if ( n%i==0)
sum=sum+i;
}

If( sum==n) Sopln(“Perfect no”); else Sopln(“ not perfect”);}}


WAP to read a 2 digit number and check if it special 2 digit number
Eg 59 sum of digit=5+9=14 product of digit=5*9= 45 sum+product =n
class number12
{
psvm()
n q p sum prod
{ Scanner sc=new Scanner (System.in);
Sopln(“Enter a 2 digit number”);
int n=sc.nextInt(); int sum=0; int prod=0;
int q=n/10;
int p=n%10;
sum=p+q;
prod=p*q;
if((sum+prod)==n)
Sopln(“ Special 2 Digit ”); else Sopln(“Not Special 2 Digit”);}}
WAP to read a number and check if it is spy number.
Eg 1124 sum=1+1+2+4=8 prod=1*1*2*4=8 sum and prod is equal

int n=sc.nextInt(); int sum=0; int prod=1; int rem=0;


While(n>0)
N n>0 rem sum+rem prod*rem
{ rem=n%10; 1124

sum=sum+rem;
prod=prod*rem;
n=n/10;
} if( sum==prod)
Sopln(“ Spy no”);
else
Sopln(“not”); }}
WAP to read a four digit number and check if it a tech number.
Eg 3025 divide number in 2 halves 30 and 25 add them 30+25 and find its
square. If square is equals to number then tech number.
int n =sc.nextInt();
int q= n/100; // 30
int r= n%100; //25
int sum= q+r; // 30+25
int sq= sum*sum; 55*55
if( sq== n)
Sopln(“ Tech no”);
else
Sopln(“ not”);
WAP to print all Armstrong numbers from 1 to 1000;
Eg= 153=13+53+33=153
Int sum=0; int rem=0; int n1=0;
For( int i=1 ;i<=1000;i++)
{ n1=i; sum=0; rem=0;
while( n1>0)
{ rem= n1%10;
sum=sum+( rem*rem*rem);
n1=n1/10;
} if ( sum==i ) Sopln(“ Armstrong no is”+i);
}
WAP to print all 4 digit tech numbers.
Eg 3025 divide number in 2 halves 30 and 25 add them 30+25 and find
its square. If square is equals to number then tech number.

for( int i=1000; i<=9999;i++)


{
int q= i/100;
int r= i%100;
int sum= q+r;
int sq= sum*sum;
if( sq== i) Sopln(“ Tech no is ”+i);}
WAP to read a number and check if it is Duck Number
Eg 2045 number should have 0 but not start with 0.

int n=sc.nextInt();
int rem=0; int flag=0;
while( n>0)
{ rem=n%10;
if( rem==0)
{ flag=1;
break;
} n=n/10;
} if( flag==1) Sopln( “ Duck no”); else Sopln(“ not “);}}
WAP to read a number and find the greatest and smallest digit in the
number. 6734 s=3 g=7
int n=sc.nextInt(); n min max n>0 rem=n%10 rem<min rem>max n=n/10
int min= 9 ; 6734 4 4 6734>0t 4 4<9 t 4>0t 673
673 3 673>0t 3 3<4t 3>4f 67
int max=0; 67 7 67>0t 7 7<3f 7>4t 6
int rem=0; 6 6>0 t 6 6<3f 6>7f 0
while( n>0) 0 0>0f
{ rem=n%10;
if( rem < min)
min=rem;
if( rem > max)
max=rem;
n=n/10;
}

Sopln( “ Largest “+ max);


Sopln( “Smallest”+ min);}}
WAP to read a number and check if it is a special number.
EG 145 = 1!+4!+5!= 145
int n=sc.nextInt(); int sum=0; int fact=1; int x=n; int rem=0;
while( x>0) x x>0 rem I i<=rem fact*I sum+fact
145 145>0 5 1 1<=5 1*1=1
{ rem=x%10; fact=1;
2 2<=5 1*2=2
for( int i=1;i<=rem; i++) 3 3<=5 2*3=6
4 4<=5 6*4=24
{ fact=fact*i; } //for 5 5<=5 24*5=120
x=x/10; 6 6<=5 f
0+120=120
sum=sum+fact; 14 14>0 4 1 1<=4 1*1=1
}//while
if( sum==n) Sopln(“Special no”); else
Sopln(“Not”);}}
WAP to read number and check if it is Disarum number
Eg 135= 11+32+53 sum of digits powered with its respective position equal to
number int n=sc.nextInt(); int rem=0 , int sum=0; int n1=n; int n2=n;
while( n>0) int n=sc.nextInt(); 135
{ rem=n%10; String s=Integer.toString(n); “135”
int len= s.length(); 3
ctr++; while( n>0)
{ rem=n%10;
n=n/10;} // count no of digits
sum=sum+( int)Math.pow( rem,len);
while( n1>0) n=n/10; len--;}
if( sum==n1)
{ rem=n1%10; Sopln(“Disarum no”); else
sum=sum+( int)Math.pow( rem,ctr); Sopln(“Not”);}}
n1=n1/10; ctr--;}
if( sum== n2) Sopln(“ Disarum no”); else Sopln(“Not”);}}
Emirp number - Java Program. An Emirp number is
a number which is prime backwards and forwards.
WAP to read a number and check if it is pronic number
Eg 0,2,6,20 product of two consecutive integer
12= 3*4 20=4*5 42=6*7 12= 2*6 X 3*4 4*3 6*2
int n=sc.nextInt(); int flag=0;
n=12
for( int i=0; i<n;i++) I i< 12 i*(i+1)==12 flag
0 0<12t 0*(0+1)==12 0
{ if( i*(i+1)==n) 1 1<12 t 1*(1+1)==12 f 0
2 2<12t 2*(2+1)==12 f 0
{ flag=1;
3 3<12t 3*(3+1)==12 t 1
break;
} }
If(flag==1)
Sopln(“Pronic number”);
else Sopln(“Not Pronic number”);}}
WAP to read a number and check if it is int n= sc.nextInt()
automorphic number int n1=n*n;
int rem=0 ; int rem1=0; int flag=0;
Eg 5 square of 5 is 25 so number 5 is at last.
while( n>0)
Eg 25 square is 625 so number 25 is at the last. { rem=n%10;
int n=sc.nextInt(); rem1=n1%10;
if( rem!= rem1)
int n1=n*n;
{ flag=1;
String s=Integer.toString(n); break;
String s1=Integer.toString(n1); }
n=n/10;
if( s1.endsWith(s))
n1=n1/10; } //while
Sopln(“ auto…”); else sopln(“ not”);
If( flag==1) Sopln(“NOT”); else
n n1 rem rem1 rem!=rem1 flag Sopln(“Automorphic”);}}
12 144 2 4 2!=4 t 1
25 625 5 5 5!=5 f
2 62 2 2 2!=2 f
0 6
WAP to read a number and check if it is ISBN Number.
The International Standard Book Number (ISBN) is a unique numeric book identifier which is printed on
every book. The ISBN is based upon a 10-digit code. The ISBN is legal if: 1 × digit1 + 2 × digit2 + 3 ×
digit3 + 4 × digit4 + 5 × digit5 + 6 × digit6 + 7 × digit7 + 8 × digit8 + 9 × digit9 + 10 × digit10 is divisible by
11.
Example: For an ISBN 1401601499, sum = 1 × 1 + 2 × 4 + 3 × 0 + 4 × 1 + 5 × 6 + 6 × 0 + 7 × 1 + 8 × 4 + 9 × 9
+ 10 × 9 = 253 which is divisible by 11.
Write a program to:
(i) Input the ISBN code as a 10-digit integer.
(ii) If the ISBN is not a 10-digit integer, output the message “Illegal ISBN” and terminate the program.
(iii) If the sum is divisible by 11, output the message “Legal ISBN”. If the sum is not divisible by 11,
output the message “Illegal ISBN”.
Sopln(“Enter number”);
int n=sc.nextInt(); int sum=0; int rem=0;
String s=Integer.toString(n);
int len=s.length();
if(len!=10)
{ Sopln(“Invalid input”); System.exit(0);}
else
{ while( n>0)
{ rem=n%10; sum=sum+(rem*len)
len--; n=n/10;
} }
if(sum%11==0)
Sopln(“Valid ISBN”); else Sopln(“Illegal ISBN”);}}
WAP to read a number and check if it magic number
Eg sum of digits should be one . 67 = 6+7=13= 1+3=4 not
magic number
199 =19=1+9=10=1+0=1 Sum=n sum>9 n=sum n>0 rem sum+rem
int n=sc.nextInt(); 67 67>0 67 67>0 7 0+7=7
int sum=0; int rem=0; 6>0 6 7+6=13
sum=n; 0>0
while(sum>9) 13>9 13 13>0 3 0+3=3
1>0 1 3+1=4
{ n=sum;
0>0
sum=0; 4>9 f
while(n>0)
{ rem=n%10;
sum=sum+rem;
n=n/10;}}

if(sum==1)
System.out.println("Magic Number");
else System.out.println("Not Magic number");}}
WAP to read a number and check if it is happy number
Eg 19 = 1 2+9 2=82=82+22=68= 62+82=100=12+02+02=1
int n=sc.nextInt();
int sum=0; int rem=0;
sum=n;
while(sum>9)
{ n=sum;
sum=0;
while(n>0)
{ rem=n%10;
sum=sum+(rem *rem);
n=n/10;}}

if(sum==1)
System.out.println(“Happy Number");
else System.out.println("Not happy number");}}
Emirp number - Java Program. An Emirp number is a number which is prime backwards and
forwards.
int n =sc.nextInt(); int rem=; int rev=0; int n1=n; int ctr=0; int ctr1=0; int flag=0; int flag1=0
while(n>0)
{ rem=n%10;
rev=rev*10+rem;
n=n/10;}

for( int i=1;i<=n1; i++) for( int i=1;i<=rev; i++) If( flag ==1 && flag1==1)
{ if( rev%i==0) Sopln(“Emirp No”);
{ if( n1%i==0)
else Sopln(“Not”);}}
ctr++; } ctr1++; }
if( ctr==2) if( ctr1==2)
flag=1; flag1=1;
Palindromic Prime Number in JAVA
If a number is simultaneously palindromic and prime then it is said to be a
Palindromic Prime Number. Example: Number 313, 353
int n=sc.nextInt();
Int n1=n; int rem=0; int rev=0; int ctr=0; int flag=0; int flag1=0;
while(n>0)
{ rem=n%10; if( ctr==2)
rev=rev*10+rem; flag1=1;
If( flag==1 && flag1==1)
n=n/10;
Sopln( “Palindromic Prime Number “);
} if( n1==rev) else
flag=1; sopln(“Not Palindromic Prime Number”);}}
for ( int i=1; i<=n1;i++)
{ if( n1%i==0) ctr++}

You might also like