Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
5 views

Java Basic Questions

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Java Basic Questions

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

School of Information Technology & Engineering Winter

Semester 2020-2021
ITA5004 – Object Oriented Programming using JAVA
Programming Questions (Basic)

Q1.Write a Java program to display the multiplication table of a number.


Solution
import java.util.*
public class multiplicatio

public static void main(String args[]

Scanner in=new Scanner(System.in)


System.out.println("enter the number")
int n=in.nextInt()
System.out.println("The multiplication table of a given number is:")
for(int i=1;i<=10;i++

System.out.println(n+" * "+ i+ " = "+(n*i))

Q2.Write a Java program to nd the factorial of a given number.


Solution

import java.util.*
public class factoria

public static void main(String args[]

Scanner in=new Scanner(System.in)


{

}
}

fi
)

System.out.println("enter the number")


int n=in.nextInt()
int p=1
for(int i=1;i<=n;i++

p=p*i

System.out.print("The factorial of a given number is:"+p)

Q3.Write a Java Program to generate the Fibonacci series till n number.


Solution

import java.util.*
public class bbonacc

public static void main(String args[]

Scanner in=new Scanner(System.in)


System.out.println("enter the number of terms")
int n=in.nextInt()
int a=0,b=1
System.out.print("The bbonacci series upto: "+n +" terms is: ")
if(1<=n

System.out.print(a+",")

if(2<=n

System.out.print(b+",")

for(int i=3;i<=n;i++
}
}

fi
;

fi
;

int c=a+b
System.out.print(c+",")
a=b
b=c

Q4.Write a Java Program to nd the roots of a quadratic equation.


Solution:
import java.util.*
public class quadrati

public static void main(String args[]

Scanner in=new Scanner(System.in)


System.out.println("enter the value of a,b and c to nd the roots o
quadratic equation")
int a=in.nextInt()
int b=in.nextInt()
int c=in.nextInt()
int d=(b*b)-(4*a*c)
if(d>0

System.out.print("The roots are real and there values are: ");


double x=((-b)+Math.sqrt(d))/2*a
double y=((-b)+Math.sqrt(d))/2*a
System.out.print(x+ " & " +y)

else if(d==0

System.out.print("The roots are real and equal and there values are: ");
System.out.print((-b)/(2*a)+" & "+(b)/(2*a))
{

fi
;

fi

els

System.out.print("The roots are imaginary")

}
Q5.Write a Java program to get n numbers from the user and display th
number of positive, negative and zeroes.
Solution

import java.util.*
public class count_positive_negative_zer

public static void main(String args[]

Scanner in=new Scanner(System.in)


System.out.println("enter the number of values you want to count")
int n=in.nextInt()
int a[]=new int[n]
int ctr1=0,ctr2=0,ctr3=0
for(int i=0;i<n;i++

a[i]=in.nextInt();

for(int i=0;i<n;i++

if(a[i]==0

ctr1++

else if(a[i]<0
{

ctr2++

els

ctr3++

System.out.println("The number of zeroes in the numbers are: "+ctr1)


System.out.println("The number of negative values in the numbers are:
"+ctr2)
System.out.println("The number of positive values in the numbers are
"+ctr3)

}
Q6.Write a Java program to nd the sum of the series:1+2/2!+3/3
+…….n/n!
Solution

import java.util.*
public class series

public static void main(String args[]

Scanner in=new Scanner(System.in)


System.out.println("enter the number")
int n=in.nextInt()
double sum=0
for(int i=1;i<=n;i++

int p=1
for(int j=1;j<=i;j++

p=p*j
}

fi
)

sum=sum+((double)i/(double)p)

System.out.println("The sum of the given series is: "+sum)

Q7.Write a Java program to nd the sum of the series: x - x2/2! + x3/3! -


x4/4! +....+ xn/n!
Solution:

import java.util.*
public class series

public static void main(String args[]

Scanner in=new Scanner(System.in)


System.out.println("enter the number of terms")
int n=in.nextInt()
System.out.println("enter the value of x")
int x=in.nextInt()
double sum=0
for(int i=1;i<=n;i++

int p=1
for(int j=1;j<=i;j++

p=p*j

if(i%2==1

sum=sum+(double)(Math.pow(x,i)/p)
}

fi
;

els

sum=sum-(double)(Math.pow(x,i)/p)

System.out.println("The sum of the given series is: "+sum)

Q8. Write a Java program to nd the given number is Armstrong or not. (if
the sum of cubes of the digits of the number is equal to the given number)
Solution:

import java.util.*
public class armston

public static void main(String args[]

Scanner in=new Scanner(System.in)


System.out.println("enter the number")
int n=in.nextInt()
int x=n,s=0
while(x>0

int d=x%10
s=s+d*d*d
x=x/10

if(s==n

System.out.println("Armstong number")

els
}

fi
)

System.out.println("Not a Armstong number")

Q9. Write a program that accepts three numbers from the user and prints
"increasing" if the numbers are in increasing order, "decreasing" if the
numbers are in decreasing order, and "Neither increasing or decreasing
order" otherwise.
Solution:

import java.util.*
public class increase_decrease_neithe

public static void main(String args[]

Scanner in=new Scanner(System.in)


System.out.println("enter the three numbers")
int a=in.nextInt()
int b=in.nextInt()
int c=in.nextInt()
if(b>a && c>b

System.out.println("Increasing")

else if(a>b && b>c

System.out.println("Decreasing")

els

System.out.println("Neither Inreasing nor Decreasing");


}

Q10.Given a number n, write a Java program to print all prime factors of


n. For example, if the input number is 12, then output should be “2 2 3”.
And if the input number is 315, then output should be “3 3 5 7”.

Solution:

import java.util.*
public class prime_factor
{
public static void main(String args[]

Scanner in=new Scanner(System.in)


System.out.println("enter the number")
int n=in.nextInt()
prime_factors obj=new prime_factors()
System.out.print("The Prime Factors of a given number is:")
int ctr=0,k=0
for(int i=1;i<=n;i++

if(n%i==0

k=obj.prime(i)

if(k!=0

System.out.print(k+",")

public static int prime(int x


}

int ctr=0,p=0
for(int i=1;i<=x;i++

if(x%i==0

ctr++;

if(ctr==2

p=x

return p

Q11. Write a Java program to display the following pattern:


1
22
333
4444
55555
Solution:

import java.util.*
public class pattern

public static void main(String args[]

Scanner in=new Scanner(System.in)


System.out.println("enter the number of terms")
int n=in.nextInt()
}

for(int i=1;i<=n;i++

for(int j=1;j<=i;j++

System.out.print(i)

System.out.println()

Q12. Write a Java program to display the following pattern:


1
21
321
4321
54321
Solution:

import java.util.*
public class pattern

public static void main(String args[]

Scanner in=new Scanner(System.in)


System.out.println("enter the number of terms")
int n=in.nextInt()
for(int i=1;i<=n;i++

for(int j=i;j>=1;j--

System.out.print(j)
}

System.out.println()

Q13. Write a Java program to print the k-th digit from last. e.g. input
23617 and k=4 output 3.
Solution:
import java.util.*
public class kth_digi

public static void main(String args[]

Scanner in=new Scanner(System.in)


System.out.println("enter the number")
int n=in.nextInt()
System.out.println("enter the kth digit from last which u want to print")
int k=in.nextInt()
int d=0,i=1
while(n>0 && i<=k

d=n%10
n=n/10
i++

System.out.print("The kth digit from last is: "+d)


}

Q14. Write a Java program that reads two numbers (assume that both
have the same number of digits). The program outputs the sum of the
product of corresponding digits.
Input: 327 and 539
output : 3*5+2*3+7*9=84.

Solution:

import java.util.*
public class sum_product_digit

public static void main(String args[]

Scanner in=new Scanner(System.in)


System.out.println("enter the two numbers")
int a=in.nextInt()
int b=in.nextInt()
int s=0
while(a>0 && b>0

int d=a%10
int d1=b%10
s=s+d*d1
a=a/10
b=b/10

System.out.print("The sum of product of corresponding digits of a


number: "+s)

}
{

Q15.Write a Java program to nd number of digits. Input 423 output 3.


Input: 21151
output: 5.
Solution:

import java.util.*
public class count_digit

public static void main(String args[]

Scanner in=new Scanner(System.in)


System.out.println("enter the number")
int n=in.nextInt()
int x=n,c=0
while(x>0

int d=x%10
c++
x=x/10

System.out.println("The number of digits in a number is:"+c)


{

fi
)

Q16.Write a Java program to read input values from the user continuously.
The program should terminate if exactly three String values have been
inputted. Display the count of integer values and oat values entered so
far. Also, display the average of all integer values and all oat values
individually.
Solution:

fl
fl

You might also like