Most Frequently asked Coding Questions
vindhya Hegde
Posted on September 21, 2021
Here I will be mainly focusing on OnCampus technical Interviews because I have attended a couple of them. and I observed these are common questions to check you know the basics of coding or not.
1.To swap two numbers without using an extra variable
import java.util.*;
public class Main
{
public static void main(String[] args)
{
int a,b;
System.out.println("Enter two numbers");
Scanner sc = new Scanner(System.in);
a = sc.nextInt();
b = sc.nextInt();
//logic to swap without using extra variable.
a = a+b;
b = a - b;
a = a - b;
System.out.println("After swapping: "+ "a="+ a +"," + "b="+ b );
}
}
2.Check given number is Amstrong Number or not
To Know more about Amstrong Number refer the link here
import java. util.*;
public class Main
{
public static void main(String[] args)
{
int a;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number");
a = sc.nextInt();
//to find length or number of digits in number
int num = a;
int len = 0;
int rem;
while(num!=0)
{
rem = num%10;
num = num/10;
len++;
}
//logic to check Armstrong number or Not
int sum = 0;
num = a;
while(num!=0)
{
rem = num%10;
int mul= 1;
for(int i=0;i<len;i++)
{
mul = mul * rem;
}
sum +=mul;
num= num/10;
}
if(sum==a)
{
System.out.println("Amstrong Number");
}
else{
System.out.println("Not an Amstrong number");
}
}
}
3.To find out the Fibonacci value
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
int n;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number");
n = sc.nextInt();
int n1 = 0 , n2 = 1;
int res = 0;
for(int i=2;i<=n;i++)
{
res = n1+n2;
n1 = n2;
n2 = res;
}
System.out.println("Fib of n is " + res);
}
}
4.to print Fibonacci series
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
int n;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number");
n = sc.nextInt();
int n1 = 0 , n2 = 1;
int res = 0;
System.out.print("Fibonacci series is " + n1 + " " + n2 + " ");
for(int i=2;i<=n;i++)
{
res = n1+n2;
n1 = n2;
n2 = res;
System.out.print(res + " ");
}
}
}
5.Factorial of a number
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
int n;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number");
n = sc.nextInt();
int fact = 1;
for(int i=1;i<=n;i++)
{
fact = fact * i;
}
System.out.println("Factorial of n is:" + fact);
}
}
6.LCM and GCD of a number
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
int n1,n2;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number");
n1 = sc.nextInt();
n2 = sc.nextInt();
int numerator,denominator;
if(n1>n2)
{
numerator = n1;
denominator = n2;
}
else{
numerator = n2;
denominator = n1;
}
int rem = numerator%denominator;
while(rem!=0)
{
numerator = denominator;
denominator = rem;
rem = numerator%denominator;
}
System.out.println("HCF of numbers:" + denominator );
int lcm = (n1*n2)/denominator;
System.out.println("LCM of numbers:" + lcm );
}
}
7.Program to check leap year or not
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
int n1,n2;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the year");
int year = sc.nextInt();
if(year%4==0)
{
//check for century year
if(year%100==0)
{
//check it is divisible by 400
if(year%400==0)
{
System.out.println("Leap year");
}
else{
System.out.println("Not a leap year");
}
}
else{
System.out.println("Leap year");
}
}
else{
System.out.println("Not a leap year");
}
}
}
I hope this helps for your OnCampus technical Interviews.
💖 💪 🙅 🚩
vindhya Hegde
Posted on September 21, 2021
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.