Most Frequently asked programs in Technical interview
Here are some programs that are frequently asked in Technical interviews.
1. Program to print the Fibonacci series.
Fibonacci series is a series in which a term is the sum of it's two previous terms.
Nth term = (N-1)th term + (N-2)th term
Fibonacci series is:
1, 1, 2, 3, 5, 8, 13, 21, 34, 55
Program in Java:
import java.util.Scanner;
class Fibonacci
{
public static void main(String [] args)
{
Scanner in =new Scanner(System.in);
int n=in.nextInt();
for(int i=1;i<=n;i++)
{
System.out.print(nth_term(i)+" ");
}
}
static int nth_term(int n) //Function to find nth term of Fibonacci serires
{
if(n==1||n==2)
return 1;
else
return nth_term(n-1)+nth_term(n-2);
}
}
2. Program to print the reverse of a number.
import java.util.Scanner;
class Reverse
{
public static void main(String [] args)
{
Scanner in =new Scanner(System.in);
int n=in.nextInt();
int rem,reverse=0;
while(n>0)
{
rem=n%10;
n/=10;
reverse=reverse*10+rem;
}
System.out.println(reverse);
}
}
3. Program to check whether a number is prime or not.
import java.util.Scanner;
class Prime
{
public static void main(String [] args)
{
Scanner in =new Scanner(System.in);
int n=in.nextInt();
int flag=1;
for(int i=2;i<=n/2;i++)
{
if(n%i==0)
{
flag=0;
break;
}
}
if(flag==1)
System.out.println("Number is a Prime number");
else
System.out.println("Number is not Prime number");
}
}
4. C Program to print the Factorial of a number.
#include<stdio.h>
int factorial(int);
void main()
{
int n;
scanf("%d",&n);
printf("Factorial of n is %d",factorial(n));
}
int factorial(int n)
{
if(n==1)
return 1;
else
return n*factorial(n-1);
}
5. Program to calculate the sum of digits of a number.
#include<stdio.h>
void main()
{
int n,sum=0;
scanf("%d",&n);
while(n>0)
{
sum+=n%10;
n/=10;
}
printf("%d",sum);
}
1. Program to print the Fibonacci series.
Fibonacci series is a series in which a term is the sum of it's two previous terms.
Nth term = (N-1)th term + (N-2)th term
Fibonacci series is:
1, 1, 2, 3, 5, 8, 13, 21, 34, 55
Program in Java:
import java.util.Scanner;
class Fibonacci
{
public static void main(String [] args)
{
Scanner in =new Scanner(System.in);
int n=in.nextInt();
for(int i=1;i<=n;i++)
{
System.out.print(nth_term(i)+" ");
}
}
static int nth_term(int n) //Function to find nth term of Fibonacci serires
{
if(n==1||n==2)
return 1;
else
return nth_term(n-1)+nth_term(n-2);
}
}
2. Program to print the reverse of a number.
import java.util.Scanner;
class Reverse
{
public static void main(String [] args)
{
Scanner in =new Scanner(System.in);
int n=in.nextInt();
int rem,reverse=0;
while(n>0)
{
rem=n%10;
n/=10;
reverse=reverse*10+rem;
}
System.out.println(reverse);
}
}
3. Program to check whether a number is prime or not.
import java.util.Scanner;
class Prime
{
public static void main(String [] args)
{
Scanner in =new Scanner(System.in);
int n=in.nextInt();
int flag=1;
for(int i=2;i<=n/2;i++)
{
if(n%i==0)
{
flag=0;
break;
}
}
if(flag==1)
System.out.println("Number is a Prime number");
else
System.out.println("Number is not Prime number");
}
}
4. C Program to print the Factorial of a number.
#include<stdio.h>
int factorial(int);
void main()
{
int n;
scanf("%d",&n);
printf("Factorial of n is %d",factorial(n));
}
int factorial(int n)
{
if(n==1)
return 1;
else
return n*factorial(n-1);
}
5. Program to calculate the sum of digits of a number.
#include<stdio.h>
void main()
{
int n,sum=0;
scanf("%d",&n);
while(n>0)
{
sum+=n%10;
n/=10;
}
printf("%d",sum);
}
Comments
Post a Comment