RYTWAYS Fresher Interview Questions with Answers – Part 1

RYTWAYS  Fresher Interview Questions with Answers – Part 1

This blog explains about RYTWAYS  Fresher Interview Questions with Answers – Part 1  and is given below :

 1 . Write a Prime Number Program in Java

Prime number in Java: Prime number is a number that is greater than 1 and divided by 1 or itself only. In other words, prime numbers can’t be divided by other numbers than itself or 1. For example 2, 3, 5, 7, 11, 13, 17…. are the prime numbers. In this java program, we will take a number variable and check whether the number is prime or not.

  1. publicclass PrimeExample{    
  2. public static void main(String args[]){    
  3. int i,m=0,flag=0;      
  4. int n=3;//it is the number to be checked    
  5. m=n/2;      
  6. if(n==0||n==1){  
  7. out.println(n+” is not prime number”);      
  8. }else{  
  9. for(i=2;i<=m;i++){      
  10. if(n%i==0){      
  11. out.println(n+” is not prime number”);      
  12. flag=1;      
  13. break;      
  14. }      
  15. }      
  16. if(flag==0)  { System.out.println(n+” is prime number”); }  
  17. }//end of else  
  18. }    
  19. }   

Output:

3 is prime number

2. Write a java program for Factorial using recursion . 

Factorial Program using recursion in java

Let’s see the factorial program in java using recursion.

  1. classFactorialExample2{  
  2. static int factorial(int n){    
  3. if (n == 0)    
  4. return 1;    
  5. else    
  6. return(n * factorial(n-1));    
  7. }    
  8. public static void main(String args[]){  
  9. int i,fact=1;  
  10. int number=4;//It is the number to calculate factorial    
  11. fact = factorial(number);   
  12. out.println(“Factorial of “+number+” is: “+fact);    
  13. }  
  14. }  

Output:

Factorial of 4 is: 24

 

3 . Write a java program to check whether the given number is palindrome or not  

In this java program, we will get a number variable and check whether number is palindrome or not.

  1. classPalindromeExample{  
  2. public static void main(String args[]){  
  3. int r,sum=0,temp;    
  4. int n=454;//It is the number variable to be checked for palindrome  
  5. temp=n;    
  6. while(n>0){    
  7. r=n%10;  //getting remainder  
  8. sum=(sum*10)+r;    
  9. n=n/10;    
  10. }    
  11. if(temp==sum)    
  12. out.println(“palindrome number “);    
  13. else    
  14. out.println(“not palindrome”);    
  15. }  
  16. }  

Output:

palindrome  number

 

4 . Write a java program for Armstrong number .

Armstrong Number in Java

Let’s write a java program to check whether the given number is armstrong number or not.

Armstrong Number in Java: A positive number is called armstrong number if it is equal to the sum of cubes of its digits for example 0, 1, 153, 370, 371, 407 etc.

Let’s see the java program to check Armstrong Number.

  1. classArmstrongExample{  
  2. public static void main(String[] args)  {  
  3. int c=0,a,temp;  
  4. int n=153;//It is the number to check armstrong  
  5. temp=n;  
  6. while(n>0)  
  7. {  
  8. a=n%10;  
  9. n=n/10;  
  10. c=c+(a*a*a);  
  11. }  
  12. if(temp==c)  
  13. out.println(“armstrong number”);   
  14. else  
  15. out.println(“Not armstrong number”);   
  16. }  
  17. }  

Output:

armstrong number

 

5 . Write a java program to print odd or even number .

Java Program to print Odd and Even Numbers from an Array

We can print odd and even numbers from an array in java by getting remainder of each element and checking if it is divided by 2 or not. If it is divided by 2, it is even number otherwise it is odd number.

  1. publicclass OddEvenInArrayExample{  
  2. publicstatic void main(String args[]){  
  3. inta[]={1,2,5,6,3,2};  
  4. out.println(“Odd Numbers:”);  
  5. for(inti=0;i<a.length;i++){  
  6. if(a[i]%2!=0){  
  7. out.println(a[i]);  
  8. }  
  9. }  
  10. out.println(“Even Numbers:”);  
  11. for(inti=0;i<a.length;i++){  
  12. if(a[i]%2==0){  
  13. out.println(a[i]);  
  14. }  
  15. }  
  16. }}  

Output:

Odd Numbers:

1

5

3

Even Numbers:

2

6

2

REFERENCES : 

https://www.javatpoint.com/prime-number-program-in-java

https://www.javatpoint.com/factorial-program-in-java

https://www.javatpoint.com/palindrome-program-in-java

https://www.javatpoint.com/armstrong-number-in-java

https://www.javatpoint.com/java-program-to-print-odd-and-even-numbers-from-an-array