Java Programs (Part 1: Questions 8–12)
Q8. Input n integers and find:
Number of positive integers
Number of negative integers
Sum of positive numbers
Product of negative numbers
Average of positive numbers
import [Link];
public class Q8
{
public static void main(String args[])
{
Scanner sc = new Scanner([Link]);
int n, num;
int pos = 0, neg = 0;
int sumPos = 0;
long productNeg = 1;
[Link]("Enter number of integers: ");
n = [Link]();
for(int i = 1; i <= n; i++)
{
[Link]("Enter number " + i + ": ");
num = [Link]();
if(num > 0)
{
pos++;
sumPos += num;
}
else if(num < 0)
{
neg++;
productNeg *= num;
}
}
[Link]("Positive Integers = " + pos);
[Link]("Negative Integers = " + neg);
[Link]("Sum of Positive Numbers = " + sumPos);
if(neg > 0)
[Link]("Product of Negative Numbers = " + productNeg);
else
[Link]("No negative numbers.");
if(pos > 0)
[Link]("Average of Positive Numbers = " + (double)sumPos /
pos);
else
[Link]("No positive numbers.");
}
}
Q9. Sum of first 500 positive odd integers (using do-while)
public class Q9
{
public static void main(String args[])
{
int count = 0;
int odd = 1;
int sum = 0;
do
{
sum += odd;
odd += 2;
count++;
}
while(count < 500);
[Link]("Sum = " + sum);
}
}
Q10. Product of series 3, 9, 12, ... , 30
(a) Using for loop
public class Q10For
{
public static void main(String args[])
{
long product = 1;
for(int i = 3; i <= 30; i += 3)
{
if(i != 6)
product *= i;
}
[Link]("Product = " + product);
}
}
(b) Using while loop
public class Q10While
{
public static void main(String args[])
{
int i = 3;
long product = 1;
while(i <= 30)
{
if(i != 6)
product *= i;
i += 3;
}
[Link]("Product = " + product);
}
}
(c) Using do-while loop
public class Q10DoWhile
{
public static void main(String args[])
{
int i = 3;
long product = 1;
do
{
if(i != 6)
product *= i;
i += 3;
}
while(i <= 30);
[Link]("Product = " + product);
}
}
Q11. Convert kilograms to pounds
public class Q11
{
public static void main(String args[])
{
[Link]("Kilograms\tPounds");
for(int kg = 1; kg <= 20; kg++)
{
double pounds = kg * 2.2;
[Link](kg + "\t\t" + pounds);
}
}
}
Q12. Display numbers from 150 to 250 divisible by 5 or 6, but not both
public class Q12
{
public static void main(String args[])
{
for(int i = 150; i <= 250; i++)
{
if((i % 5 == 0 || i % 6 == 0) && !(i % 5 == 0 && i % 6 == 0))
{
[Link](i + " ");
}
}
}
}
Java Programs (Part 2: Questions 13–17)
Q13. Print the Tribonacci Series (0, 0, 1, 1, 2, 4, 7, ...)
import [Link];
public class Q13
{
public static void main(String args[])
{
Scanner sc = new Scanner([Link]);
[Link]("Enter number of terms: ");
int n = [Link]();
int a = 0, b = 0, c = 1;
if(n >= 1)
[Link](a + " ");
if(n >= 2)
[Link](b + " ");
if(n >= 3)
[Link](c + " ");
for(int i = 4; i <= n; i++)
{
int d = a + b + c;
[Link](d + " ");
a = b;
b = c;
c = d;
}
}
}
Q14. Calculate the value of P
[
P=(4!)^2+(4!)^3+(4!)^4+\cdots+(4!)^{10}
]
public class Q14
{
public static void main(String args[])
{
long fact = 1;
for(int i = 1; i <= 4; i++)
fact *= i;
double sum = 0;
for(int i = 2; i <= 10; i++)
sum += [Link](fact, i);
[Link]("P = " + sum);
}
}
Q15. Check whether a number is a Pronic Number
(A number is Pronic if it is the product of two consecutive integers.)
import [Link];
public class Q15
{
public static void main(String args[])
{
Scanner sc = new Scanner([Link]);
[Link]("Enter a number: ");
int n = [Link]();
boolean found = false;
for(int i = 0; i <= n; i++)
{
if(i * (i + 1) == n)
{
found = true;
break;
}
}
if(found)
[Link]("Pronic Number");
else
[Link]("Not a Pronic Number");
}
}
Q16. Check whether a number is a Spy Number
(A Spy Number is one in which the sum of digits = product of digits.)
import [Link];
public class Q16
{
public static void main(String args[])
{
Scanner sc = new Scanner([Link]);
[Link]("Enter a number: ");
int n = [Link]();
int sum = 0;
int product = 1;
int temp = n;
while(temp > 0)
{
int digit = temp % 10;
sum += digit;
product *= digit;
temp /= 10;
}
if(sum == product)
[Link]("Spy Number");
else
[Link]("Not a Spy Number");
}
}
Q17(i). Check whether a number is a Niven Number
(A Niven Number is divisible by the sum of its digits.)
import [Link];
public class Q17a
{
public static void main(String args[])
{
Scanner sc = new Scanner([Link]);
[Link]("Enter a number: ");
int n = [Link]();
int temp = n;
int sum = 0;
while(temp > 0)
{
sum += temp % 10;
temp /= 10;
}
if(n % sum == 0)
[Link]("Niven Number");
else
[Link]("Not a Niven Number");
}
}
Q17(ii). Find the sum of digits of an integer
import [Link];
public class Q17b
{
public static void main(String args[])
{
Scanner sc = new Scanner([Link]);
[Link]("Enter a number: ");
int n = [Link]();
int sum = 0;
while(n > 0)
{
sum += n % 10;
n /= 10;
}
[Link]("Sum of digits = " + sum);
}
}
Java Programs (Part 3: Questions 18–20)
Q18. Menu-driven program
(i) Check whether a number is Prime
(ii) Check whether a number is Automorphic
import [Link];
public class Q18
{
public static void main(String args[])
{
Scanner sc = new Scanner([Link]);
[Link]("MENU");
[Link]("1. Prime Number");
[Link]("2. Automorphic Number");
[Link]("Enter your choice: ");
int ch = [Link]();
[Link]("Enter a number: ");
int n = [Link]();
switch(ch)
{
case 1:
int count = 0;
for(int i = 1; i <= n; i++)
{
if(n % i == 0)
count++;
}
if(count == 2)
[Link]("Prime Number");
else
[Link]("Not a Prime Number");
break;
case 2:
int square = n * n;
int temp = n;
while(temp > 0)
{
if(temp % 10 != square % 10)
{
[Link]("Not an Automorphic Number");
return;
}
temp /= 10;
square /= 10;
}
[Link]("Automorphic Number");
break;
default:
[Link]("Invalid Choice");
}
}
}
Q19. Menu-driven Program
(i) Buzz Number
(ii) GCD of Two Numbers
import [Link];
public class Q19
{
public static void main(String args[])
{
Scanner sc = new Scanner([Link]);
[Link]("MENU");
[Link]("1. Buzz Number");
[Link]("2. GCD");
[Link]("Enter choice: ");
int ch = [Link]();
switch(ch)
{
case 1:
[Link]("Enter a number: ");
int n = [Link]();
if(n % 7 == 0 || n % 10 == 7)
[Link]("Buzz Number");
else
[Link]("Not a Buzz Number");
break;
case 2:
[Link]("Enter first number: ");
int a = [Link]();
[Link]("Enter second number: ");
int b = [Link]();
while(b != 0)
{
int r = a % b;
a = b;
b = r;
}
[Link]("GCD = " + a);
break;
default:
[Link]("Invalid Choice");
}
}
}
Q20. Read x and compute the series
[
\frac{x}{2}+\frac{x}{5}+\frac{x}{8}+\frac{x}{11}+ \cdots +\frac{x}{20}
]
import [Link];
public class Q20
{
public static void main(String args[])
{
Scanner sc = new Scanner([Link]);
[Link]("Enter value of x: ");
double x = [Link]();
double sum = 0;
for(int i = 2; i <= 20; i += 3)
{
sum = sum + (x / i);
}
[Link]("Sum of the series = " + sum);
}
}
Java Programs (Part 4: Questions 21–22)
Q21. Read the number x and compute the series
[
\frac{x}{2}+\frac{x}{5}+\frac{x}{8}+\frac{x}{11}+\cdots+\frac{x}{20}
]
import [Link];
public class Q21
{
public static void main(String args[])
{
Scanner sc = new Scanner([Link]);
[Link]("Enter value of x: ");
double x = [Link]();
double sum = 0;
for(int i = 2; i <= 20; i += 3)
{
sum = sum + (x / i);
}
[Link]("Sum of the series = " + sum);
}
}
Q22(i). Find the sum of the series
[
x^1+x^2+x^3+\cdots+x^n
]
import [Link];
public class Q22i
{
public static void main(String args[])
{
Scanner sc = new Scanner([Link]);
[Link]("Enter x: ");
int x = [Link]();
[Link]("Enter n: ");
int n = [Link]();
double sum = 0;
for(int i = 1; i <= n; i++)
{
sum += [Link](x, i);
}
[Link]("Sum = " + sum);
}
}
Q22(ii). Find the sum of the series
[
x^1-x^2+x^3-x^4+\cdots+x^n
]
import [Link];
public class Q22ii
{
public static void main(String args[])
{
Scanner sc = new Scanner([Link]);
[Link]("Enter x: ");
int x = [Link]();
[Link]("Enter n: ");
int n = [Link]();
double sum = 0;
for(int i = 1; i <= n; i++)
{
if(i % 2 == 0)
sum -= [Link](x, i);
else
sum += [Link](x, i);
}
[Link]("Sum = " + sum);
}
}
Q22(iii). Find the sum of the series
[
\frac{1}{x^1}+\frac{2}{x^2}+\frac{3}{x^3}+\cdots+\frac{n}{x^n}
]
import [Link];
public class Q22iii
{
public static void main(String args[])
{
Scanner sc = new Scanner([Link]);
[Link]("Enter x: ");
double x = [Link]();
[Link]("Enter n: ");
int n = [Link]();
double sum = 0;
for(int i = 1; i <= n; i++)
{
sum += (double)i / [Link](x, i);
}
[Link]("Sum = " + sum);
}
}
Q22(iv). Find the sum of the series
[
\frac12+\frac23+\frac34+\cdots+\frac{49}{50}
]
public class Q22iv
{
public static void main(String args[])
{
double sum = 0;
for(int i = 1; i <= 49; i++)
{
sum += (double)i / (i + 1);
}
[Link]("Sum = " + sum);
}
}
Q22(v). Find the sum of the series
[
\frac{x+1}{3}+\frac{x+2}{5}+\frac{x+3}{7}+\cdots
]
import [Link];
public class Q22v
{
public static void main(String args[])
{
Scanner sc = new Scanner([Link]);
[Link]("Enter x: ");
int x = [Link]();
[Link]("Enter number of terms: ");
int n = [Link]();
double sum = 0;
int d = 3;
for(int i = 1; i <= n; i++)
{
sum += (double)(x + i) / d;
d += 2;
}
[Link]("Sum = " + sum);
}
}
Q22(vi). Find the sum of the series
[
\frac{x}{2!}+\frac{x}{3!}+\frac{x}{4!}+\cdots+\frac{x}{20!}
]
import [Link];
public class Q22vi
{
public static void main(String args[])
{
Scanner sc = new Scanner([Link]);
[Link]("Enter x: ");
double x = [Link]();
double sum = 0;
long fact = 1;
for(int i = 1; i <= 20; i++)
{
fact *= i;
if(i >= 2)
sum += x / fact;
}
[Link]("Sum = " + sum);
}
}
Q22(vii). Find the sum of the series
[
\frac{x^2}{2!}+\frac{x^3}{3!}+\frac{x^4}{4!}+\cdots+\frac{x^{10}}{10!}
]
import [Link];
public class Q22vii
{
public static void main(String args[])
{
Scanner sc = new Scanner([Link]);
[Link]("Enter x: ");
double x = [Link]();
double sum = 0;
long fact = 1;
for(int i = 1; i <= 10; i++)
{
fact *= i;
if(i >= 2)
sum += [Link](x, i) / fact;
}
[Link]("Sum = " + sum);
}
}
Q22(viii). Find the sum of the series
[
1+3+7+15+31+\cdots+(2^{20}-1)
]
public class Q22viii
{
public static void main(String args[])
{
long sum = 0;
for(int i = 0; i <= 20; i++)
{
sum += (long)[Link](2, i) - 1;
}
[Link]("Sum = " + sum);
}
}
Q22(ix). Find the product of the series
[
1 \times 2 \times 4 \times 8 \times \cdots \times 2^{20}
]
import [Link];
public class Q22ix
{
public static void main(String args[])
{
BigInteger product = [Link];
for(int i = 0; i <= 20; i++)
{
product = [Link]([Link]((long)[Link](2, i)));
}
[Link]("Product = " + product);
}
}
Java Programs (Part 5: Questions 23–25)
Q23. Check whether a number is a Peterson Number
A Peterson number is a number whose sum of the factorials of its digits is equal to the
number itself.
import [Link];
public class Q23
{
public static void main(String args[])
{
Scanner sc = new Scanner([Link]);
[Link]("Enter a number: ");
int n = [Link]();
int temp = n;
int sum = 0;
while(temp > 0)
{
int digit = temp % 10;
int fact = 1;
for(int i = 1; i <= digit; i++)
fact *= i;
sum += fact;
temp /= 10;
}
if(sum == n)
[Link]("Peterson Number");
else
[Link]("Not a Peterson Number");
}
}
Q24. Check whether a number is a Duck Number
A Duck Number contains at least one '0', but the first digit should not be 0.
import [Link];
public class Q24
{
public static void main(String args[])
{
Scanner sc = new Scanner([Link]);
[Link]("Enter a number: ");
int n = [Link]();
int temp = n;
boolean duck = false;
while(temp > 0)
{
if(temp % 10 == 0)
{
duck = true;
break;
}
temp /= 10;
}
if(duck)
[Link]("Duck Number");
else
[Link]("Not a Duck Number");
}
}
Q25. Menu-Driven Program
1. Check whether a number is an Armstrong Number
2. Display the Fibonacci Series up to n terms
import [Link];
public class Q25
{
public static void main(String args[])
{
Scanner sc = new Scanner([Link]);
[Link]("MENU");
[Link]("1. Armstrong Number");
[Link]("2. Fibonacci Series");
[Link]("Enter your choice: ");
int ch = [Link]();
switch(ch)
{
case 1:
[Link]("Enter a number: ");
int n = [Link]();
int temp = n;
int digits = 0;
while(temp > 0)
{
digits++;
temp /= 10;
}
temp = n;
int sum = 0;
while(temp > 0)
{
int digit = temp % 10;
sum += (int)[Link](digit, digits);
temp /= 10;
}
if(sum == n)
[Link]("Armstrong Number");
else
[Link]("Not an Armstrong Number");
break;
case 2:
[Link]("Enter number of terms: ");
int terms = [Link]();
int a = 0, b = 1;
for(int i = 1; i <= terms; i++)
{
[Link](a + " ");
int c = a + b;
a = b;
b = c;
}
break;
default:
[Link]("Invalid Choice");
}
}
}