1)Write a menu driven program to print the following series:
• 2,4,8,16,…………..256.
• 1,11,111,……111111.
• 2/!2 + 4/!4 + 6/!6 +……+10/!10.
Code:
import [Link];
public class Question_1
public static void main(String[] args)
Scanner sc = new Scanner([Link]);
int choice, i;
[Link]("Menu");
[Link]("1. 2,4,8,16,...256");
[Link]("2. 1,11,111,...111111");
[Link]("3. 2!/2 + 4!/4 + 6!/6 + ... + 10!/10");
[Link]("Enter your choice: ");
choice = [Link]();
switch(choice)
case 1:
int n = 2;
while(n <= 256)
[Link](n + " ");
n = n * 2;
break;
case 2:
int n = 1;
for(i = 1; i <= 6; i++)
[Link](n + " ");
n = n * 10 + 1;
break;
case 3:
for(i = 2; i <= 10; i = i + 2)
[Link](i + "!/" + i);
if(i != 10)
{
[Link](" + ");
break;
default:
[Link]("Invalid Choice");
[Link]();
OUTPUT:
2) Write a menu driven program to print the following series:
• 1 + 11 + 111 + 1111 + 11111.
• 2 + 5 +10 + 17 + …….n tems.
Code:
import [Link];
public class Question_2
public static void main(String[] args)
Scanner sc = new Scanner([Link]);
int choice;
[Link]("Menu");
[Link]("1. 1 + 11 + 111 + 1111 + 11111");
[Link]("2. 2 + 5 + 10 + 17 + ... n terms");
[Link]("Enter your choice: ");
choice = [Link]();
switch(choice)
case 1:
int n = 1;
for(int i = 1; i <= 5; i++)
{
[Link](n);
if(i != 5)
[Link](" + ");
n = n * 10 + 1;
break;
case 2:
int terms;
[Link]("Enter number of terms: ");
terms = [Link]();
for(int i = 1; i <= terms; i++)
int value = (i * i) + 1;
[Link](value);
if(i != terms)
{
[Link](" + ");
break;
default:
[Link]("Invalid Choice");
[Link]();
OUTPUT:
3) Write a program to check if a given number is:
• Neon Number
• Niven Number
A neon number is a number whose square’s digits add up to the number itself.4
Example: 9 is a neon number because (9^2 = 81) and (8 + 1 = 9).
A Niven number is a number that is divisible by the sum of its digits.
Example: 18 is a Niven number because (1 + 8 = 9) and (18 \div 9 = 2).
Code:
import [Link];
public class Question_3
public static void main(String[] args)
Scanner sc = new Scanner([Link]);
int choice, num;
[Link]("1. Neon Number");
[Link]("2. Niven Number");
[Link]("Enter your choice: ");
choice = [Link]();
[Link]("Enter a number: ");
num = [Link]();
switch (choice) {
case 1:
int square = num * num;
int sum1 = 0;
while (square > 0) {
sum1 += square % 10;
square /= 10;
if (sum1 == num)
[Link](num + " is a Neon Number");
else
[Link](num + " is not a Neon Number");
break;
case 2:
int temp = num;
int sum2 = 0;
while (temp > 0) {
sum2 += temp % 10;
temp /= 10;
}
if (num % sum2 == 0)
[Link](num + " is a Niven Number");
else
[Link](num + " is not a Niven Number");
break;
default:
[Link]("Invalid Choice");
OUTPUT:
4) Write a program to input a number and display the new number after reversing the
digits of the original number. The program also displays the absolute difference
between the original number and the reversed number.
Sample Input: 194
Sample Output: 491
Absolute Difference = 297
Code:
import [Link];
public class Question_4
public static void main(String[] args)
Scanner sc = new Scanner([Link]);
int num, reverse = 0, rem, original, diff;
[Link]("Enter a number: ");
num = [Link]();
original = num;
while (num > 0) {
rem = num % 10;
reverse = reverse * 10 + rem;
num = num / 10;
}
diff = [Link](original - reverse);
[Link]("Reversed Number = " + reverse);
[Link]("Absolute Difference = " + diff);
[Link]();
OUTPUT:
5)Write a menu driven program to print the following series:
• X + X2/2! + X3/3! +……..n terms. (get value of X from user)
• 1/1 * 2/4 * 3/9 *…. n terms.
Code:
import [Link];
public class Question_5
public static void main(String[] args)
Scanner sc = new Scanner([Link]);
int choice, n, x;
[Link]("-----------MENU----------");
[Link]("1. X + X^2/2! + X^3/3! + ...");
[Link]("2. 1/1 * 2/4 * 3/9 * ...");
[Link]("Enter your choice: ");
choice = [Link]();
[Link]("Enter number of terms: ");
n = [Link]();
switch (choice) {
case 1:
[Link]("Enter value of X: ");
x = [Link]();
for (int i = 1; i <= n; i++) {
if (i == 1)
[Link](x);
else
[Link](" + " + x + "^" + i + "/" + i + "!");
break;
case 2:
for (int i = 1; i <= n; i++) {
if (i == 1)
[Link](i + "/" + (i * i));
else
[Link](" * " + i + "/" + (i * i));
break;
default:
[Link]("Invalid Choice");
[Link]();
}
OUTPUT: