Chapter 8 Programs Loop
Chapter 8 Programs Loop
Program 1:
/** Write a Program to print 1 , 4 , 9 , 16 , 25 , 36 , 49 , 64, 81, 100
*/
public class Pg_230_Q_1a
{
public static void main()
{
int i, x;
for ( i= 1; i<=10;i++)
{
x=i*i;
System.out.print(x+" , ");
} // end of for
} // end of main
} // end of class
Program 2:
/** Write a Program to print 1 , 2 , 4 , 7 , 11 , 16 , 22 , 29 , 37 , 46 ,
*/
public class SERIES_Q1_B
{
public static void main()
{
int i,n=1;
for ( i=0; i<10;i++)
{
n=n+i;
System.out.print( n+" , ");
}
}
}
Program 3:
/** * Write a Program to print 3 , 6 , 9 , 12 , 15 , 18 , 21 , 24 , 27 , 30 ,
*/
public class SERIES_Q1_C
{
public static void main()
{
int i;
for ( i=3; i<=30;i+=3)
{
System.out.print( i+" , ");
}
}
}
Program 4:
/** * Write a prog to print 4 , 8 , 16 , 32 , 64 , 128 , 256 , 512 , 1024 , 2048
*/
public class SERIES_Q1_D
{
public static void main()
{
int i,n=4;
for ( i=1; i<=10;i++)
{
System.out.print( n+" , ");
n=n*2;
}
}
}
Program 5: copy question from book
import java.util.*;
public class PG_231_Q_9
{
public static void main()
{
Scanner in = new Scanner ( System.in);
System.out.println( " Enter a number ");
int m = in.nextInt();
int i, c=0;
for ( i= m ;i>0;i/=10)// LOOP STATEMENT FOR EXTRACTING DIGITS OF A NUMBBER
{
c++;
}
System.out.println(" NUMBER OF DIGITS= "+c);
if ( c%2==0)
System.out.println(" The number contains even number of digits");
else
System.out.println("The number contains odd number of digits");
}
}
Program 6: copy question from book
import java.util.*;
public class PG_231_Q_10
{
public static void main()
{
Scanner in = new Scanner ( System.in);
System.out.println( " Enter a number ");
int m = in.nextInt();
int i, rev=0 , digit;
for ( i= m ;i>0;i/=10)
{
digit =( i%10);
rev=(rev*10)+ digit;
}
System.out.println(" ORIGINAL NUMBER = "+m);
System.out.println(" REVERSE NUMBER = "+rev);
System.out.println(" ABSOLUTE DIFFERENCE = "+(Math.abs(m-rev)));
}
}
Program 7:
Write a program to accept a number and check and display whether it is a Niven number or not.
(Niven number is that number which is divisible by its sum of digits).
Example:
Consider the number 126.
Sum of its digits is 1+2+6 = 9 and 126 is divisible by 9.
import java.util.*;
public class PG_233_Q22_NIVEN_NO
{
int i, rev=0;
for ( i= m ;i>0;i/=10)
{
rev=(rev*10)+( i%10);
} // end of for loop
if ( m == rev)
System.out.println( m +" is a Palindromic Number ");
else
System.out.println( m +" is not a Palindromic Number ");
}
}
Program 9:
Write a program to accept a number check and display whether it is a Perfect_Number or not .
import java.util.*;
public class Perfect_Number
{
public static void main()
{
Scanner in = new Scanner ( System.in);
System.out.println( " Enter a number ");
int m = in.nextInt();
int j, sum=0;
for ( j= 1;j< m;j++)
{
if ( m % j ==0)
sum+= j;
}
if ( m == sum)
System.out.println( m +" is a Perfect Number ");
else
System.out.println( m +" is not a Perfect Number ");
}
}
Program 10:
A special two-digit number is such that when the sum of its digits is added to the product of its digits,
the result is equal to the original two-digit number. Example: Consider the number 59.
Sum of digits = 5 + 9=14
Product of its digits = 5 x 9 = 45
Sum of the sum of digits and product of digits = 14 + 45 = 59
Write a program to accept a two-digit number. Add the sum of its digits to the product of its digits. If the
value is equal to the number input, output the message “Special 2-digit number” otherwise, output the
message “Not a special 2-digit number”.
import java.util.*;
public class Special_two_digit_num
{
public static void main()
{
Scanner in = new Scanner( System.in);
System.out.println(" Enter a number ");
int n = in.nextInt();