# For Loop Structure

#### Java Programs

**Program [1]**

Write a program to find the sum of 1st 10 odd natural numbers.

**Program [2]**

Write a program to find the sum of 1st 10 even natural numbers.

**Program [3]**

Write a program to find the sum of all 3-digit even natural numbers.

**Program [4]**

Write a program to find the sum of all 3 digit odd natural numbers, which are multiples of 5.

**Program [5]**

Write a program to input an integer and find its factorial. Factorial of a number is the product of all natural numbers till that number. For example factorial of 5 is 120 since 1×2×3×4×5=120.

**Program [6]**

Write a program to input an integer and print its factors.

**Example**

INPUT: Enter an integer:12

OUTPUT: Factors: 1 2 3 4 6 12

**Program [7]**

Write a program to input an integer and count the number of factors.

**Program [8]**

Write a program to input an integer and check whether it is a prime number or not.

**Program [9]**

Write a program to input 10 integers and find their sum.

**Program [10]**

Write a program to input 10 integers and find the sum of even numbers only.

**Program [11]**

Write a program to input 10 integers and find the sum of two digit as well as three digit numbers separately.

**Program [12]**

Write a program to input 10 integers and display the largest integer.

**Program [13]**

Write a program to input 10 integers and display the largest as well as the smallest integer.

**Program [14]**

Write a program to input 10 integers and display the largest even integer. In case there is no even integer, it should print “No even integer found”.

**Program [15]**

Write a program to input 10 integers and display the largest even and smallest odd integer.

**Program [16]**

Write a program to input 10 integers and check whether all the entered numbers are even numbers or not.

**Program [17]**

Write a program to input 10 integers and check whether all the entered numbers are same or not.

For Example,

INPUT: Enter 10 numbers: 10 12 13 234 45 34 67 78 76 12

OUTPUT: All numbers are not same.

INPUT: Enter 10 numbers: 12 12 12 12 12 12 12 12 12 12

OUTPUT: All numbers are same.

**Program [18]**

Write a program to input 10 integers and check whether the entered numbers are in ascending order or not.

For Example,

INPUT: Enter 10 numbers: 10 12 13 25 45 55 67 78 106 122

OUTPUT: The numbers are in ascending order.

INPUT: Enter 10 numbers: 25 34 56 67 12 32 43 21 23 111

OUTPUT: The numbers are not in ascending order.

**Program [19]**

Write a program to find the sum of 1st 10 numbers of Fibonacci series i.e. 1,1,2,3,5,8,13….

Fibonacci series is such a series which starting from 1 and 1, and subsequent numbers are the sum of the previous two numbers.

**Program [20]**

Write a program to print the first 15 numbers of the Pell series. Pell series is such a series which starts from 1 and 2 , and subsequent numbers is the sum of twice the previous number and the number previous to the previous number. Pell series: 1, 2, 5, 12, 29, 70, 169, 408, 985, 2378, 5741, 13860, …

**Program [21]**

Write a program to find the sum of 1st 10 numbers of Lucas series i.e. 2,1,3,4,7,11,18,….

Lucas series is such a series which starting from 2 and 1, and subsequent numbers are the sum of the previous two numbers.

**Program [22]**

Write a program to input an integer and check whether it is perfect, abundant or deficient number. If the sum of the factors excluding itself is equal to that number it is perfect, if greater than that number it is abundant and if less than that number it is deficient number.

**Program [23]**

Write a program to input two integers and check whether it forms an amicable pair or not. An amicable pair is such that, the sum of the factors excluding itself of one number is the other number and sum of the factors excluding itself of the other number is this number.

Example, (220, 284) . Since sum of factors excluding itself of:

220= 1+2+4+5+10+11+20+22+ 44+55+110=284

284= 1+ 2+4+71+142=220.

**Program [24]**

Write a program to pass an integer as argument and find the sum of its digits.

**Program [25]**

Write a program to pass an integer as argument and find the sum of odd digits and even digits separately.

**Program [26]**

Write a program to pass an integer as argument and find the average of its digits.

**Program [27]**

Write a program to pass an integer as argument and print the largest as well as smallest digit.

**Program [28]**

Write a program to input an integer and remove all the even digits from it.

For Example,

INPUT: Enter an integer: 1234

OUPUT: 13

**Program [29]**

Write a program to pass an integer as argument and check whether all digits in it are even digit or not.

**Program [30]**

Write a program to pass an integer as argument and check whether the digits are in ascending order or not.

**Program [31]**

Write a program to pass an integer as argument and check whether it is Armstrong number or not. Numbers whose sum of the cube of its digit is equal to the number itself is called Armstrong numbers. Example 15^{3}=1^{3}+5^{3} +3^{3}.

**Program [32]**

Write a program to input an integer and check whether it is an automorphic, trimorphic or tri-automorphic number or not. A number n is said to be automorphic, if its square ends in n. For instance 5 is automorphic, because 52= 25, which ends in 5, 25 is automorphic, because 252=625, which ends in 25. A number n is called trimorphic if n3 ends in n. For example 493, = 117649, is trimorphic. A number n is called tri-automorphic if 3n2 ends in n; for example 667 is tri-automorphic because 3 × 6672, = 1334667, ends in 667.

**Program [33]**

Write a program to input an integer and check whether it is Harshad or Niven number or not. A number is said to be harshad if it is divisible by the sum of the digits of that number, example 126 and 1729.

**Program [34]**

Write a program to input a number and check whether it is a Kaprekar number or not. Take a positive whole number n that has d number of digits. Take the square n and separate the result into two pieces: a right-hand piece that has d digits and a left-hand piece that has either d or d-1 digits. Add these two pieces together. If the result is n, then n is a Kaprekar number.

Examples are 9 (92 = 81, 8 + 1 = 9), 45 (452 = 2025, 20 + 25 = 45), and 297 (2972 = 88209, 88 + 209 = 297).

**Program [35]**

Write a program to input two integers and find their Least Common Multiple(L.C.M).

For Example,

INPUT: Enter 2 integers:

12

8

OUTPUT: L.C.M. = 24

**Program [36]**

Write a program to input two integers and find their Highest Common Factor(H.C.F).

For Example,

INPUT: Enter 2 integers:

12

8

OUTPUT: H.C.F. = 4

**Program [37]**

Write a menu driven class to accept a number from the user and check whether it is a Palindrome or a Perfect number.

a.** Palindrome numb**er- (a number is a Palindrome which when read in reverse order is same as read in the right order) Example: 11, 101, 151, etc.

b. **Perfect number**- (a number is called Perfect if it is equal to the sum of its factors other than the number itself.) Example: 6=1+2+3

**Program [38]**

Write a menu driven program to accept a number from the user and check whether it is ‘BUZZ’ number or to accept any two numbers and print the ‘GCD’ of them.

a. A BUZZ number is the number which either ends with 7 or divisible by 7.

b. GCD (Greatest Common Divisor) of two integers is calculated by continued division method. Divide the larger number by the smaller; the remainder then divides the previous divisor. The process is repeated till the remainder is zero. The divisor then results the GCD.

**Program [39]**

Write a menu driven program to accept a number and check and display whether it is a Prime Number or not OR an Automorphic Number or not.

a. **Prime number :** A number is said to be a prime number if it is divisible only by 1 and itself and not by any other number. Example : 3,5,7,11,13 etc.

b. **Automorphic number : **An automorphic number is the number which is contained in the last digit(s) of its square.

**Example: **

25 is an automorphic number as its square is 625 and 25 is present as the last two digits.

**Program [40]**

Write a program to input 10 integers and print the second largest number. Assume that there is at least one second largest number in the given set of integers.

For Example,

INPUT: Enter 10 integers:

12 35 46 22 34 56 78 89 23 21

OUTPUT: Second Largest Integer: 78

**Program [41]**

Write a program to find the sum of the following series:

S=1+(1+2)+(1+2+3)+(1+2+3+4)+(1+2+3+4+5)+…+(1+2+3+4+…+10)

(Please note that no nested loop is to be used)

**Program [42]**

Write a program to print the sum of negative numbers, sum of positive even numbers and sum of positive odd numbers from a list of numbers (N) entered by the user. The list terminates when the user enters a zero.

**Program [43]**

A game of dice is to be simulated for two players, each player gets a chance to throw his dice, and the value is added to his points, this process continues alternately until for the player whose added points equals to 20 and is declared the winner. Write a program to simulate this process using the random( ) function.

**Program [44]**

Write a program to input 10 integers and find the sum of the prime numbers only.

**Program [45]**

Write a program to input 10 integers and find the average of the prime numbers only.

**Program [46]**

Write a program to input 10 integers and find the sum of the perfect numbers only.

**Program [47]**

Write a program to input 10 integers and find the average of the perfect numbers only.

**Program [48]**

Write a program to input 10 integers and find the largest prime number if any.

**Program [49]**

Write a program to input 10 integers and find the smallest perfect number if any.

**Program [50]**

Write a program to generate all 2 digit prime numbers.

**Program [51]**

Write a program to generate all perfect numbers between 1 to 100.

**Program [52]**

Write a program to generate all 3 digit palindrome numbers.

**Program [53]**

WAP to Input an integer and check whether it is a special number or not.

A special number is such a number whose sum of the factorials of each digit is the number itself. For example, 145 is a special number because 1! + 4! + 5! = 1 + 24 +120 = 145. Special numbers are also called factorion.

**Program [54]**

Write a program to input a number and check whether it is a magic number or not. If you iterate the process of summing the squares of the decimal digits of the number, and if this process terminates in 1, then the original number is called a magic number. For example 55=> (5+5)=10=>(1 +0)=1.

**Program [55]**

Write a program to input an integer as argument, and print the number by having the digits arranged in ascending order.

**Program [56]**

Write a program to pass an integer as argument, and check whether all digits in it are unique or not.

**Program [57]**

Write a program to pass an integer as argument, and print the frequency of each digit in it.

**Program [58]**

Write a program to input 10 integers and find the sum of two-digit as well as three-digit numbers separately.

**Program [59]**

Write a program to input 10 numbers and print the largest prime number if any.

**Program [60]**

Write a program to generate all 2 digit twisted prime number. Twisted prime number is a number which is prime and its reverse is also prime. Example 13 and 31.

**Program [61]**

WAP to Input an integer and form a new number where the digits are in ascending order.

**Program [62]**

Write a program to pass an integer as argument and print the second largest digit

**Program [63]**

Write a program to input a number and check whether it is a happy number or not. If you iterate the process of summing the squares of the decimal digits of a number and if this process terminates in 1, then the original number is called a happy number. For example 7=> (72)=49=> (42+92)=97=>(92 +72)=130 =>(12 +32+02)=10 =>(12+02)= 1.

**Program [64]**

Write a program to input a number and print all its prime factors using prime factorization.

For Example,

INPUT: Enter an integer: 24

OUTPUT: Prime Factors using Prime Factorisation are:

2223

**Program [65]**

Write a program to input a number and check whether it is a Smith number or not. Smith number is such a number, the sum of whose digits equals the sum of the digits of its prime factors.

Smith number is a composite number in which the sum of its digits is equal to the sum of the digits of all its prime factors.

For Example 378 is a Smith Number as the sum of the digits of 378 are : 3+7+8 = 18. The prime factors of 378 are: 2, 3, 3, 3, 7 ( sum = 2+3+3+3+7 = 18).

Similarly 22 is a Smith Number as the sum of the digits are : 2+2=4. The prime factors of 22 are:

2 and 11 (Sum = 2+(1+1) = 4

Other Examples include 27, 58, 85, 94, 121, 166, 202, 265, etc.

**Program [66]**

Write a program to accept a number and check and display whether it is a spy number or not.

(A number is spy if the sum its digits equals the product of its digits.)

Example: consider the number 1124 , sum of the digits = 1 + 1 + 2 + 4 = 8

Product of the digits = 1 × 1 × 2 × 4 = 8

**Program [67]**

Write a program to input a number and check and print whether it is a Pronic number or not.

(Pronic number is the number which is the product of two consecutive integers)

Examples: 12 = 3 × 4

20 = 4 × 5

42 = 6 × 7