Numbers
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 );
}
}}
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;
}
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.
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;
}
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++}