While Loop based Programs
/ Programming QuestionsQuestion 1
WAP to take a number (n) as input and then find the sum of all it’s digits. For example if the input n = 2673, then the output should be 18 i.e. 2 + 6 + 7 + 3
java
import java.util.*;
public class Test
{
public static void main()
{
int s = 0, n, d;
Scanner sc = new Scanner(System.in);
System.out.println("Enter a number");
n = sc.nextInt();
// extract digits
while (n != 0)
{
d = n % 10;
n = n / 10;
s += d;
}
System.out.println("Sum of the digits = " + s);
}
}
python
n = int(input('enter the number : '))
s = 0
while n != 0:
d = n % 10
n = int(n / 10)
s += d
print('sum of digits =', s)
Question 2
WAP to input a number and then reverse the number. For example if the original number is 2356, the reverse
of the number will be 6532. Then check if the number is a Palindrome
i.e. the original and the reversed number
are the same. For example, the number 23432 is a Palindrome
, since the reverse of the number is also 23432
java
import java.util.*;
public class MyClass
{
public static void main(String args[])
{
int n, temp, d, rev = 0;
Scanner sc = new Scanner(System.in);
System.out.println("Enter a number");
n = sc.nextInt();
temp = n;
// extract digits
while (temp != 0)
{
d = temp % 10;
temp /= 10;
rev = (10 * rev) + d;
}
System.out.println("Reversed Number : " + rev);
if (n == rev)
System.out.println("The number is a Palindrome");
}
}
python
n = int(input("Enter a Number: "))
temp = n
rev = 0
# reverse the number
while temp != 0:
d = temp % 10
temp = int(temp / 10)
rev = (rev * 10) + d
print ("Reverse of the number =", rev)
# check if palindrom
if n == rev:
print("Number is a Palindrome")
Question 3
Use a while loop to keep entering numbers except 0 - the loop will stop once the user enters a 0. Within the loop, keep summing up all the positive even numbers and the negative odd numbers. Once the loop stops, print out both the sums
java
import java.util.*;
public class Test
{
public static void main()
{
int pe = 0, no = 0, n = 1;
Scanner sc = new Scanner(System.in);
while (n != 0)
{
System.out.println("Enter number (0 to exit)");
n = sc.nextInt();
// positive even
if ((n > 0) && (n % 2 == 0))
pe += n;
// negative odd
else if ((n < 0) && (n % 2 != 0))
no += n;
}
System.out.println("Sum of +ve even: " + pe);
System.out.println("Sum of -ve odd: " + no);
}
}
python
n = 1
pe = 0
no = 0
while n != 0:
n = int(input("Enter a number (0 to exit) : "))
# positive even
if n > 0 and n % 2 == 0:
pe += n
# negative odd
elif n < 0 and n % 2 != 0:
no += n
print("sum of +ve even numbers : ", pe)
print("sum of -ve odd numbers : ", no)
Question 4
WAP to input a number and check if it is a Harshad number
i.e. a number that
is divisible by the sum of its digits.
For example, if the number is 156, then sum of its digits will be 1 + 5 + 6 = 12.
Since 156 is divisible by 12. So, 156 is a Harshad number
java
import java.util.*;
public class MyClass {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int rem = 0, sum = 0, n, num;
System.out.println("Enter a Number");
num = sc.nextInt();
//Make a copy of num and store it in variable n
n = num;
//Calculates sum of digits
while(num > 0)
{
rem = num % 10;
sum = sum + rem;
num = num/10;
}
//Checks whether number is divisible by sum of digits
if(n % sum == 0)
System.out.println(n + " is a harshad number");
else
System.out.println(n + " is not a harshad number");
}
}
python
n = int(input("Enter a Number: "))
s = 0
num = n
while n != 0:
d = n % 10
n = int(n / 10)
s += d
if num % s == 0:
print("Harshad Number")
else:
print("Not a Harshad Number")
Question 5
An Automorphic Number
is a number which is contained in the last digit(s) of it’s square
For example: 25, since 252 = 625 and 25 is contained in the last digit(s) of 625
WAP to input a number and check whether it is an Automorphic Number
or not
java
import java.util.*;
import java.lang.*;
public class Test
{
public static void main()
{
int n, d, temp;
double count = 0, last, s;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number: ");
n = sc.nextInt();
// square the number
s = n * n;
System.out.println("Square = " + s);
// count how many digits
temp = n;
while (temp != 0)
{
d = temp % 10;
temp = temp / 10;
count++;
}
// extract the last digits
last = s % (Math.pow(10,count));
System.out.println("Last Digit(s) = " + last);
if (last == n)
System.out.println("Automorphic");
else
System.out.println("Not Automorphic");
}
}
python
# enter the number
n = int(input('enter number: '))
# square the number
s = n * n
print("Square =", s)
# count how many digits
temp = n
count = 0
while temp != 0:
d = temp % 10
temp = int(temp / 10)
count += 1
# extract the last digits
print("Digits: ", count)
last = s % (10**count)
print("Last Digit(s) =", last)
if last == n:
print("Automorphic")
else:
print("Not Automorphic")
Question 6
An Armstrong number
of 3 digits is a 3-digit number
, the sum of cubes of each digits is equal to the number itself.
For example: 153 = 1x1x1 + 5x5x5 + 3x3x3
WAP to input a 3 digit number and check if it is an Armstrong Number
java
import java.util.*;
public class Test
{
public static void main()
{
int s = 0, n, d, a;
Scanner sc = new Scanner(System.in);
System.out.println("Enter a number");
n = sc.nextInt();
// preserve the original number
a = n;
// extract digits
while (n != 0)
{
d = n % 10;
n = n / 10;
s += (d*d*d);
}
System.out.println("Sum of the cube of the digits = " + s);
// check if armstrong
if (a == s)
System.out.println("Armstrong number");
else
System.out.println("Not an Armstrong number");
}
}
python
n = int(input("enter the number : "))
# preserve the original number
a = n
s = 0
# digit extraction
while n != 0:
d = n % 10
n = int(n / 10)
s += (d**3)
print("sum of cube of digits =", s)
if s == a:
print("armstrong number")
else:
print("not an armstrong number")
Question 7
WAP to take a 5 digit number from user and display the product of largest and
second largest digit of the number
For Example: If the input is 69231, the product will be (9 * 6) = 54
java
import java.util.*;
public class Test
{
public static void main()
{
Scanner sc = new Scanner(System.in);
int n, r, ld, sld, count = 0;
boolean flag;
System.out.println("Enter the number: ");
n = sc.nextInt();
// initialize
ld = -1;
sld = -1;
// extract digits
while (n != 0)
{
r = n % 10;
n = n/10;
// count the digits
count++;
if ( ld < r )
{
if ( sld < ld)
sld = ld;
ld = r;
}
else if ( ld > r && sld < r)
{
sld = r;
}
}
// check if 5 digits
if (count == 5)
{
System.out.println("Largest Digit: " + ld);
System.out.println("Second Largest Digit: " + sld);
System.out.println("Product = " + (ld * sld));
}
else
System.out.println("Number must be of 5 digits");
}
}
python
# enter the number
n = int(input('enter number: '))
count = 0
ld = -1
sld = -1
# extract digits
while n != 0:
r = n % 10
n = int(n/10)
# count the number of digits
count += 1
if ld < r:
if sld < ld:
sld = ld
ld = r
elif ld > r and sld < r:
sld = r
# print out if 5 digits
if count == 5:
print('Largest Digit: ', ld)
print('Second Largest Digit: ', sld)
print('Product: ', ld * sld)
else:
print('Number should be of 5 digits')
Question 8
WAP to enter a 3 digit positive number from user and print the LCM of the digits of the number
For Example: If the input is 664, LCM of digits of the number 6, 6, 4 is 12
java
import java.util.*;
public class Test
{
public static void main()
{
Scanner sc = new Scanner(System.in);
int n, d1, d2, d3, max, lcm;
System.out.println("Enter the number: ");
n = sc.nextInt();
// get the 3 digits
d1 = n % 10;
n /= 10;
d2 = n % 10;
d3 = n / 10;
System.out.println(d1 + "," + d2 + "," + d3);
// find the largest
max = d1;
if ((d1 >= d2) && (d1 >= d3))
max = d1;
else if ((d2 >= d1) && (d2 >= d3))
max = d2;
else if ((d3 >= d1) && (d3 >= d2))
max = d3;
// find lcm
lcm = max;
while (true)
{
if ((lcm % d1 == 0) && (lcm % d2 == 0) && (lcm % d3 == 0))
break;
lcm += max;
}
System.out.println("lcm is " + lcm);
}
}
python
# enter the number
n = int(input('enter number: '))
# get the 3 digits
d1 = n % 10
n = int(n / 10)
d2 = n % 10
d3 = int(n / 10)
print(d1, d2, d3)
# find the largest
mx = d1
if (d1 >= d2) and (d1 >= d3):
mx = d1
elif (d2 >= d1) and (d2 >= d3):
mx = d2
elif (d3 >= d1) and (d3 >= d2):
mx = d3
# find lcm
lcm = mx
while True:
if (lcm % d1 == 0) and (lcm % d2 == 0) and (lcm % d3 == 0):
break
lcm += mx
print('LCM is', lcm)
Question 9
WAP to input a number and find if it is Disarium
or not.
A number is called Disarium
if the sum of its digits powered with their respective positions is equal to the number itself
For Example: 135 is a Disarium number
as 11 + 32 + 53 = 135
java
import java.util.*;
import java.lang.*;
public class MyClass {
public static void main(String args[])
{
int n, temp, count_digits = 0, digit;
double sum = 0, x;
Scanner sc = new Scanner(System.in);
System.out.println("Enter a Number");
n = sc.nextInt();
// count the number of digits
temp = n;
while (temp != 0)
{
temp = temp / 10;
count_digits++;
}
System.out.println("Number of digits: " + count_digits);
// calculate sum
temp = n;
while (temp != 0)
{
// Get the digit
digit = temp % 10;
// Sum the digits by powering according to
// the positions
x = Math.pow(digit, count_digits);
sum = sum + x;
count_digits--;
temp = temp/10;
}
System.out.println("Sum = " + sum);
if (sum == n)
System.out.println("Disarium Number");
}
}
python
n = int(input("Enter a Number: "))
count_digits = 0
temp = n
# count number of digits
while temp != 0:
temp = int(temp / 10)
count_digits += 1
# calculate sum
temp = n
s = 0
while temp != 0:
# Get the digit
digit = temp % 10
# Sum the digits by powering according to
# the positions
x = digit**count_digits
s = s + x
count_digits -= 1
temp = int(temp / 10)
print('Sum =', s)
if s == n:
print("Disarium Number")
Question 10
WAP to input a number and check whether it is a Spy Number
A number is a Spy Number
if the sum of it’s digits equals the product of it’s digits
. For example, consider the number 1124 - the sum of it’s digits is 1 + 1 + 2 + 4 = 8
and the product of it’s digits is 1 * 1 * 2 * 4 = 8
java
import java.util.*;
import java.lang.*;
public class Test
{
public static void main()
{
int n, d, s = 0, p = 1;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number: ");
n = sc.nextInt();
// extract digits
while (n != 0)
{
d = n % 10;
n = n / 10;
// calculate sum and product
s += d;
p *= d;
}
System.out.println("Sum of digits: " + s);
System.out.println("Product of digits: " + p);
if (s == p)
{
System.out.println("Spy Number");
}
else
{
System.out.println("Not a Spy Number");
}
}
}
python
# enter the number
n = int(input('enter number: '))
# extract digits
s = 0
p = 1
while n != 0:
d = n % 10
n = int(n / 10)
# calculate sum and product
s += d
p *= d
print("sum of digits:", s)
print("product of digits:", p)
if s == p:
print("Spy Number")
else:
print("Not a Spy Number")