0% found this document useful (0 votes)
65 views5 pages

Chapter 8 Programs Loop

The document contains 10 programs that demonstrate different types of loops and series in Java. Program 1 prints squares from 1 to 100 using a for loop. Program 2 prints the series 1, 2, 4, 7, 11... using a for loop. Program 3 prints multiples of 3 from 3 to 30 using a for loop. The remaining programs demonstrate additional examples of loops, input/output, and number checking logic.

Uploaded by

furquan sabir
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
65 views5 pages

Chapter 8 Programs Loop

The document contains 10 programs that demonstrate different types of loops and series in Java. Program 1 prints squares from 1 to 100 using a for loop. Program 2 prints the series 1, 2, 4, 7, 11... using a for loop. Program 3 prints multiples of 3 from 3 to 30 using a for loop. The remaining programs demonstrate additional examples of loops, input/output, and number checking logic.

Uploaded by

furquan sabir
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

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
{

public static void main(){


Scanner in = new Scanner( System.in);
System.out.println(" Enter a number ");
int n = in.nextInt();
int i, digit , sum =0;
for (i = n; i>0;i/=10)
{
digit = i%10;
sum =sum+ digit;
}
if ( n %sum ==0)
System.out.println(n+" IS A NIVEN NUMBER");
else
System.out.println(n+" IS NOT A NIVEN NUMBER");
}
}
Program 8:
Write a program to accept a number a check and display whether it is Palindromic number or not .
import java.util.*;
public class Palindrome_Number
{
public static void main()
{
Scanner in = new Scanner ( System.in);
System.out.println( " Enter a number ");
int m = in.nextInt();

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();

int i, digit , sum =0, pro=1;


if ( n>=10 && n<=99)
{
for (i = n; i>0;i/=10)
{
digit = i%10;
sum =sum + digit;
pro= pro * digit ;
}// end of for loop block
int total = sum+pro;
if ( n == total)
System.out.println( n +" is a special two digit number ");
else
System.out.println( n +" is a not special two digit number ");
}
else
System.out.println( n +" is not a two digit number ");
}
}

You might also like