1.
Factorials of large numbers
Given an integer, the task is to find factorial of the number.
Input:
The first line of input contains an integer T denoting the number of test cases.
The first line of each test case is N,the number whose factorial is to be found
Output:
Print the factorial of the number in separate line.
Constraints:
1 ≤ T ≤ 100
1 ≤ N ≤ 1000
Example:
Input
3
5
10
2
Output
120
3628800
2
Solution
import [Link];
import [Link];
class BigFactorial {
public static void main(String arg[]){
BigInteger fac=new BigInteger("1");
int n;
Scanner sc=new Scanner([Link]);
[Link]("Enter a number:");
n=[Link]();
for(int i=2;i<=n;++i){
fac=[Link]([Link](i));
}
[Link](fac);
}
}
2. Check if a number is Armstrong or not.
Note- A number is armstrong if the sum of the cubes of digit of number is equal to the
number. ex- 407 = 4*4*4 + 0*0*0 + 7*7*7
Solution
import [Link];
public class ArmstrongNum{
public static void main(String[] args){
Scanner in = new Scanner([Link]);
[Link]("Enter a number which you want to check whether that is armstrong or not:
");
int n = [Link]();
int a = n, r=0, s=0;
while(a!=0){
r = a%10;
a = a/10;
s = s + r*r*r;
}
if(s==n){
[Link]("Number "+n+" is an armstrong number.");
}
else{
[Link]("Number "+n+" is not an armstrong number.");
}
}
}
3. Floyd Triangle Note- Floyd Triangle is like 1 2 3 4 5 6 7 8 9 10 ———— Code-
Solution
import [Link];
public class FloydTriangle{
public static void main(String[] args){
Scanner in = new Scanner([Link]);
[Link]("Enter the number of rows which you want in your Floyd Triangle: ");
int r = [Link]();
int n=0;
for(int i=0; i<r; i++){
for(int j=0; j<=i; j++){
[Link](++n+" ");
}
[Link]();
}
}
}
Output
Enter the number of rows which you want in your Floyd Triangle:
5
1
23
456
7 8 9 10
11 12 13 14 15
4. Write a Java program to check if a given number is circular prime or not.
Circular Prime : A circular prime is a prime number with the property that the number
generated at each intermediate step when cyclically permuting its (base 10) digits will be
prime.
For example, 1193 is a circular prime, since 1931, 9311 and 3119 all are also prime. A
circular prime with at least two digits can only consist of combinations of the digits 1, 3,
7 or 9, because having 0, 2, 4, 6 or 8 as the last digit makes the number divisible by 2, and
having 0 or 5 as the last digit makes it divisible by 5.
Test Data
Input a number: 35
import [Link].*;
class GFG
{
// Function to check if a number is prime or not.
static boolean isPrime(int n)
{
// Corner cases
if (n <= 1)
return false;
if (n <= 3)
return true;
// This is checked so that we can skip
// middle five numbers in below loop
if (n % 2 == 0 || n % 3 == 0)
return false;
for (int i = 5; i * i <= n; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return false;
return true;
}
// Function to check if the number is circular
// prime or not.
static boolean checkCircular(int N)
{
// Count digits.
int count = 0, temp = N;
while (temp>0) {
count++;
temp /= 10;
}
int num = N;
while (isPrime(num)) {
// Following three lines generate the next
// circular permutation of a number. We
// move last digit to first position.
int rem = num % 10;
int div = num / 10;
num = (int)(([Link](10, count - 1)) * rem)
+ div;
// If all the permutations are checked and
// we obtain original number exit from loop.
if (num == N)
return true;
}
return false;
}
// Driver Program
public static void main (String[] args)
{
int N = 1193;
if (checkCircular(N))
[Link]("Yes");
else
[Link]("No");
}
}
MCQS
1. What will be output of the following program ?
public class Test
{
public static void main(String[] args)
{
int i=0, j=5;
for( ; (i<3) && (j++ <10) ; i++)
{
[Link](“ “ + i + “ “ + j);
}
[Link](“ “ + i + “ “ + j);
}
}
a)0 6 1 7 2 8 3 8 c) 0 6 1 5 2 5 3 5
b)0 6 1 7 2 8 3 9 d) Compilation error
2. int i = l, j = -1;
switch (i)
{
case 0, 1: j = 1; /* Line 4 */
case 2: j = 2;
default: j = 0;
}
[Link]("j = " + j);
A. j = -1 C. j = 1
B. j = 0 D. Compilation fails.
3. What will be output of the following program?
public class Test
{
public static void main(String [] args)
{
int I = 1;
do while ( I < 1 )
[Link]("I is " + I);
while ( I > 1 ) ;
}
}
A. I is 1 C. No output is produced.
B. I is 1 I is 1 D. Compilation error
4. With x = 0, which of the following are legal lines of Java code for changing the value
of x to 1?
1. x++;
2. x = x + 1;
3. x += 1;
4. x =+ 1;
a) 1, 2 & 3 c) 1, 2, 3 & 4
b) 1 & 4 d) 3 & 2
5. What is the output of this program?
class mainclass {
public static void main(String args[])
{
char a = 'A';
a++;
[Link]((int)a);
}
}
A. 66 C. 65
B. 67 D. 64
6. What is the output of this program, Command line execution is done as – “java
Output This is a command Line”?
class Output
{
public static void main(String args[])
{
[Link]("args[0]");
}
}
a) java c) This
b) Output d) Is
7. Can command line arguments be converted into int automatically if required?
a) Yes
b) No
c) Compiler Dependent
d) Only ASCII characters can be converted
8. public class Main
{
public static void main(String[] args) {
if(0){
[Link]("Sun");
}
else{
[Link]("Moon");
}
}
}
(A) Moon (D) error: int can not be converted to
(B) Sun boolean
(C) SunMoon
9. public class Test{
public static void main(String args[]){
int i, j;
for(i=1, j=0;i<10;i++) j += i;
[Link](i);
}
}
A. 20 C. 9
B. 11 D. 10
10. class Test{
public void display(int x, double y)
{
[Link](x+y);
}
public double display(int p, double q)
{
return (p+q);
}
public static void main(String args[])
{
Test test = new Test();
[Link](4, 5.0); [Link]([Link](4, 5.0));
}
}
a) 9.0 9.0 c) Compilation Error
b) 9 9 d) None of these
Answers
1. A
2. D
3. C
4. D
5. A
6. C
7. B
8. D
9. D
10.C