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.
- publicclass PrimeExample{
- public static void main(String args[]){
- int i,m=0,flag=0;
- int n=3;//it is the number to be checked
- m=n/2;
- if(n==0||n==1){
- out.println(n+” is not prime number”);
- }else{
- for(i=2;i<=m;i++){
- if(n%i==0){
- out.println(n+” is not prime number”);
- flag=1;
- break;
- }
- }
- if(flag==0) { System.out.println(n+” is prime number”); }
- }//end of else
- }
- }
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.
- classFactorialExample2{
- static int factorial(int n){
- if (n == 0)
- return 1;
- else
- return(n * factorial(n-1));
- }
- public static void main(String args[]){
- int i,fact=1;
- int number=4;//It is the number to calculate factorial
- fact = factorial(number);
- out.println(“Factorial of “+number+” is: “+fact);
- }
- }
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.
- classPalindromeExample{
- public static void main(String args[]){
- int r,sum=0,temp;
- int n=454;//It is the number variable to be checked for palindrome
- temp=n;
- while(n>0){
- r=n%10; //getting remainder
- sum=(sum*10)+r;
- n=n/10;
- }
- if(temp==sum)
- out.println(“palindrome number “);
- else
- out.println(“not palindrome”);
- }
- }
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.
- classArmstrongExample{
- public static void main(String[] args) {
- int c=0,a,temp;
- int n=153;//It is the number to check armstrong
- temp=n;
- while(n>0)
- {
- a=n%10;
- n=n/10;
- c=c+(a*a*a);
- }
- if(temp==c)
- out.println(“armstrong number”);
- else
- out.println(“Not armstrong number”);
- }
- }
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.
- publicclass OddEvenInArrayExample{
- publicstatic void main(String args[]){
- inta[]={1,2,5,6,3,2};
- out.println(“Odd Numbers:”);
- for(inti=0;i<a.length;i++){
- if(a[i]%2!=0){
- out.println(a[i]);
- }
- }
- out.println(“Even Numbers:”);
- for(inti=0;i<a.length;i++){
- if(a[i]%2==0){
- out.println(a[i]);
- }
- }
- }}
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