Nested Loop Programs
/ Programming QuestionsQuestion 1
A positive integer of n digits is called an Armstrong number of order n
(order is the number of digits) if abcd... = pow(a,n) + pow(b,n) + pow(c,n) + pow(d,n) + ....
where the digits of the number are abcd…
For Example: 1634 is an Armstrong Number of 4 digits because
1634 = 14 + 64 + 34 + 44
WAP to input a number and check whether it is an Armstrong Number or not
java
import java.util.*;
import java.lang.*;
public class Armstrong
{
public static void main()
{
int num, d = 0, sum = 0, counter = 0,temp;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number to be verified: ");
num = sc.nextInt();
temp = num;
// calculating number of digits
while(temp!=0){
d=temp%10;
temp = temp/10;
counter++;
}
System.out.println("number of digits: " + counter);
// calculating the sum of the powers
temp= num;
while(temp!=0)
{
d=temp%10;
temp = temp/10;
sum+=Math.pow(d, counter);
}
System.out.println("The sum of the powers = " + sum);
if(sum == num)
{
System.out.println("The number is an armstrong number");
}
}
}
python
n = int(input('Enter the value of n: '))
counter = 0
temp = n
# get the number of digits
while temp != 0:
d = temp % 10
temp = int(temp/10)
counter += 1
print("Number of digits =", counter)
# get the sum of the powers
temp = n
s = 0
while temp != 0:
d = temp % 10
temp = int(temp/10)
s += d**counter
print("Sum of the powers =", s)
if s == n:
print("Armstrong Number")
Question 2
Take 2 inputs from the user, START and COUNT - both integers WAP, which prints out COUNT number of prime numbers starting from START For example - let’s say the user inputs START = 72 and COUNT = 5 Then your program should print out the first 5 prime numbers starting from 72 So the output should be 73, 79, 83, 89, 97
java
import java.util.*;
public class Test
{
public static void main()
{
int i, start, count, total = 0, n;
boolean flag;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the starting number");
start = sc.nextInt();
System.out.println("Enter the number of prime numbers");
count = sc.nextInt();
// start from the starting number
n = start;
while(total < count)
{
flag = true;
// check if n is prime
for (i = 2; i < n; i++)
{
if (n%i == 0)
{
flag = false;
break;
}
}
// is prime
if (flag)
{
System.out.println(n);
total++;
}
// go to the next number
n++;
}
}
}
python
start = int(input("Enter the starting number: "))
count = int(input("Enter the number of primes: "))
total = 0
# start from the starting number
n = start
while total < count:
flag = True
# check if n is prime
for i in range(2, n, 1):
if n%i == 0:
flag = False
break
# is prime
if flag:
print(n)
total+=1
# go to the next number
n+=1
Question 3
WAP to input a number and print the sum of the factorial of it’s digits For example, if the number n = 243, then the program should print out 2! + 4! + 3! = 2 + 24 + 6 = 32
java
import java.util.*;
public class Test
{
public static void main()
{
int i, n, digit, f, s = 0;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number");
n = sc.nextInt();
// extract digits
while(n != 0)
{
digit = n%10;
n = n/10;
// calculate factorial
f = 1;
for (i = 1; i <= digit; i++)
f*=i;
// add to sum
s+=f;
}
System.out.println("Sum of factorial of digits = " + s);
}
}
python
n = int(input("Enter the number: "))
s = 0
# extract digits
while n != 0:
digit = n % 10
n = int(n/10)
# calclate factorial
f = 1
for i in range(1, digit+1, 1):
f *= i
# add to sum
s += f
print("sum=", s)
Question 4
WAP to calculate the largest 2 digit prime number
java
import java.util.*;
public class Test
{
public static void main()
{
int i, n, j;
boolean flag;
// reverse loop from 99
for (i = 99; i >= 10; i--)
{
// check if number is prime
n = i;
flag = true;
for (j = 2; j < n; j++)
{
// factor found
if (n % j == 0)
{
flag = false;
break;
}
}
// is prime
if (flag)
{
System.out.println(n);
break;
}
}
}
}
python
# reverse loop from 99
for i in range(99, 10, -1):
n = i
# check if number is prime
flag = True
for i in range(2, n, 1):
# factor found
if n % i == 0:
flag = False
break
# is prime
if flag:
print(n)
break
Question 5
WAP to calculate the largest 3 digit prime number
java
import java.util.*;
public class Test
{
public static void main()
{
int i, n, j;
boolean flag;
// reverse loop from 999
for (i = 999; i >= 100; i--)
{
// check if number is prime
n = i;
flag = true;
for (j = 2; j < n; j++)
{
// factor found
if (n % j == 0)
{
flag = false;
break;
}
}
// is prime
if (flag)
{
System.out.println(n);
break;
}
}
}
}
python
# reverse loop from 99
for i in range(999, 100, -1):
n = i
# check if number is prime
flag = True
for i in range(2, n, 1):
# factor found
if n % i == 0:
flag = False
break
# is prime
if flag:
print(n)
break
Question 6
WAP that will take a number that contains atleast 3 digits and display
the difference between sum of all composite digits
and product of all prime digits in the number
For Example: If the input is 2563, the difference is 6 - 30 = -24
java
import java.util.*;
public class Test
{
public static void main()
{
Scanner sc = new Scanner(System.in);
int n, i, digit, s = 0, p = 1, count = 0;
boolean flag;
System.out.println("Enter the number: ");
n = sc.nextInt();
// extract digits
while (n != 0)
{
digit = n % 10;
n = n/10;
// count the number of digits
count++;
// check if digit is prime or composite
flag = true;
for (i = 2; i < digit; i++)
{
if (digit % i == 0)
{
flag = false;
break;
}
}
// prime
if (flag)
p *= digit;
// composite
else
s += digit;
}
// check if less than 3 digits
if (count < 3)
System.out.println("Number must be atleast 3 digits");
else
System.out.println("Difference is " + (s - p));
}
}
python
# enter the number
n = int(input('enter number: '))
count = 0
s = 0
p = 1
# extract digits
while n != 0:
digit = n % 10
n = int(n/10)
# count the number of digits
count += 1
# check if prime
prime = True
for i in range(2,digit,1):
if digit % i == 0:
prime = False
break
# prime
if prime:
p *= digit
# composite
else:
s += digit
# check if 3 digits or more
if count < 3:
print('Number must be 3 digits or more')
else:
print('Difference is', (s - p))