Lab Assignment3
Lab Assignment3
class ArmstrongExample{
public static void main(String[] args) {
int c=0,a,temp;
int n=153;//It is the number to check armstrong
temp=n;
while(n>0)
{
a=n%10;
n=n/10;
c=c+1
}
System .out. println (“Number of digits in given number is”+c); }
}
Output:
Explanation: In every iteration, when we take the remainder, it will give the least significant bit
and store it in a variable. Then we divide the value by 10 to remove the least significant bit, since
int will only store the integer part.
Syntax:
while(Boolean expression)
Statement1;
Statement2;
…..
publicstaticvoidmain(String args[]){
intnumber=10;
do
{
inti=1,fact=1;
Output:
Statement1;
Statement2;
…..
} while(Boolean expression)
Objective-5: Use of do while loop.
Problem Statement: Write a java program to check whether number is strong number or not
using do- while loop.
Note:Strong number is a special number whose sum of factorial of digits is equal to the original number.
145=!1+!4+!5
!5=5*4*3*2*1=120
!4=4*3*2*1=24
!1=1
So 145 is a Strong Number.
inti, sum=0; {
System.out.println("The first n natural numbers are :);
for(i=1;i<=20;i++) {
System.out.println(i);
sum+=i;}
System.out.println("The Sum of 20 Natural Number are" +sum);
}}
Output
Explanation:Here we have used a simple for loop demo to print sum of natural numbers in its
each iteration.For that inside for loop, the value of i in each iteration itself is the natural number
starting from 1 to range(20), and we keep on adding into sum variable to get the final sum up-to
20 terms.
Syntax
for(initialization; boolean expression; update)
Statement1;
Statement2;
…..
2. Let d(n) be defined as the sum of proper divisors of n (numbers less than n which divide
evenly into n).
If d(x) = y and d(y) = x, where x ≠ y, then x and y are an amicable pair and each of a and b are
called amicable numbers.
For example, the proper divisors of 220 are 1, 2, 4, 5, 10, 11, 20, 22, 44, 55 and 110;
therefore d(220) = 284. The proper divisors of 284 are 1, 2, 4, 71 and 142; so d(284) = 220.
Evaluate all pair of the amicable numbers less than 1000.