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

Java Series Programs Examples

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)
33 views5 pages

Java Series Programs Examples

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

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;
[Link](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;
[Link]( 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)
{
[Link]( 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++)
{
[Link]( n+" , ");
n=n*2;
}
}
}
Program 5: copy question from book
import [Link].*;
public class PG_231_Q_9
{
public static void main()
{
Scanner in = new Scanner ( [Link]);
[Link]( " Enter a number ");
int m = [Link]();
int i, c=0;
for ( i= m ;i>0;i/=10)// LOOP STATEMENT FOR EXTRACTING DIGITS OF A NUMBBER
{
c++;
}
[Link](" NUMBER OF DIGITS= "+c);
if ( c%2==0)
[Link](" The number contains even number of digits");
else
[Link]("The number contains odd number of digits");
}
}
Program 6: copy question from book

import [Link].*;
public class PG_231_Q_10
{
public static void main()
{
Scanner in = new Scanner ( [Link]);
[Link]( " Enter a number ");
int m = [Link]();
int i, rev=0 , digit;
for ( i= m ;i>0;i/=10)
{
digit =( i%10);
rev=(rev*10)+ digit;
}
[Link](" ORIGINAL NUMBER = "+m);
[Link](" REVERSE NUMBER = "+rev);
[Link](" ABSOLUTE DIFFERENCE = "+([Link](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 [Link].*;
public class PG_233_Q22_NIVEN_NO
{

public static void main(){


Scanner in = new Scanner( [Link]);
[Link](" Enter a number ");
int n = [Link]();
int i, digit , sum =0;
for (i = n; i>0;i/=10)
{
digit = i%10;
sum =sum+ digit;
}
if ( n %sum ==0)
[Link](n+" IS A NIVEN NUMBER");
else
[Link](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 [Link].*;
public class Palindrome_Number
{
public static void main()
{
Scanner in = new Scanner ( [Link]);
[Link]( " Enter a number ");
int m = [Link]();

int i, rev=0;
for ( i= m ;i>0;i/=10)
{
rev=(rev*10)+( i%10);
} // end of for loop
if ( m == rev)
[Link]( m +" is a Palindromic Number ");
else
[Link]( 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 [Link].*;
public class Perfect_Number
{
public static void main()
{
Scanner in = new Scanner ( [Link]);
[Link]( " Enter a number ");
int m = [Link]();
int j, sum=0;
for ( j= 1;j< m;j++)
{
if ( m % j ==0)
sum+= j;
}
if ( m == sum)
[Link]( m +" is a Perfect Number ");
else
[Link]( 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 [Link].*;
public class Special_two_digit_num
{
public static void main()
{
Scanner in = new Scanner( [Link]);
[Link](" Enter a number ");
int n = [Link]();

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)
[Link]( n +" is a special two digit number ");
else
[Link]( n +" is a not special two digit number ");
}
else
[Link]( n +" is not a two digit number ");
}
}

Common questions

Powered by AI

Digit extraction and manipulation are enabled by using mathematical operations like modulus (to isolate the last digit) and integer division (to remove the last digit from the original number). These operations allow for iterative examination of each digit in the number, crucial for evaluating properties such as count of digits (evenness) and reconstruction of the number to check for palindromism .

The sequence uses constant addition of 3 to generate subsequent terms, representing an arithmetic sequence. The program achieves this structured repetition through a for loop where the initial term is set to the starting value (3), and in each iteration, the term is incremented by 3. This reveals how arithmetic sequences linearly progress, emphasizing a constant rate of change which is characteristic of such series .

A number is considered palindromic if it reads the same backward as forward. The program reads a number `m`, reverses its digits through a loop that continually extracts the last digit (using modulus 10), appends it to a reverse accumulator, and removes the last digit (using integer division by 10) until `m` is zero. It then checks if the reversed number is equal to `m` and concludes if it's palindromic .

The loop prints a geometric sequence, which is a series where each term after the first is obtained by multiplying the previous term with a constant factor, here being 2. The significance lies in the loop initialization and modification, where a variable `n` starts at 4 and is doubled in each iteration. This multiplication is what maintains the pattern so critical for comprehending geometric sequences. Recognition of this loop pattern illustrates fundamental concepts of growth rates in sequences .

The algorithm involves reading an integer input from the user, extracting and reversing its digits to form a new integer, and comparing this reversed integer to the original input. If they match, the number is palindromic. This design typically involves loops and conditionals, crucial for dynamically responding to user input and determining the result based on the number's digit symmetry .

The algorithm iteratively extracts each digit from the number using modulus and adds these to a cumulative sum until the entire number is reduced to zero via integer division. This step is significant because the correct calculation of the digit sum is critical since the number's divisibility by this sum determines if it is a Niven number, reflecting underlying number theory concepts .

The sequence follows an incremental pattern where the difference between each subsequent pair of numbers increases sequentially by one, starting with an increment of 1. It begins at 1, increases by 1 to 2, then 2 to 4 (an increment of 2), 4 to 7 (increment of 3), and so forth. The program implements this by initializing a value `n` to 1 and then incrementing `n` by the loop variable `i` in each iteration, where `i` starts at 0 and increases by 1 in each cycle up to 9 .

A special two-digit number is defined such that the sum of its digits added to the product of its digits equals the original number. The program checks if an input number `n` is within the two-digit range (10 to 99). It then calculates the sum and product of the digits of `n`. The result of adding both the sum and product is compared to `n`. If equal, it confirms the number is a special two-digit number .

A Perfect Number is a positive integer equal to the sum of its proper divisors excluding itself. The program reads an integer `m` and calculates the sum of all divisors of `m` from 1 to `m-1`. If this sum equals `m`, it determines `m` to be a Perfect Number. The program iterates through possible divisors, adding divisors to the sum when they divide `m` evenly .

A Niven number is an integer that is divisible by the sum of its digits. The program works by reading an integer `n`, then iteratively extracting each digit by taking the remainder of `n` divided by 10, adding it to a sum, and continually dividing `n` by 10 until all digits are processed. Finally, it checks if the original number `n` is divisible by this sum of its digits .

You might also like