JAVA PROGRAMMING
Q1:What is the output of the below Program
public class Q1 {
public static void main(String[] args) {
int $a;
$a=10;
System.out.println($a);
double b=2_3.4_5;
System.out.println(b);
double c=2_3._53;
System.out.println(c);
}
}
Ans:Compile time error, we cannot have an underscore next to decimal point.
Q2: What is the output of the below Program
public class Q2 {
public static void main(String[] args) {
int n1=10;
int n3=n1;
int n2=n3;
n3=n1+n2;
n2=n3;
System.out.println(n1);
System.out.println(n2);
System.out.println(n3);
}
}
Q3: What is the output of the below Program
public class Q3 {
public static void main(String[] args) {
int n1;
n1++;
n1++;
System.out.println(n1);
}
}
Ans:Compile time Error, variable should be initialized before utilization.
Q4: What is the output of the below Program
public class Q4 {
public static void main(String[] args) {
int n1 = 1;
if (n1 > 0) {
n1++;
} else if (n1 > 0) {
n1 = n1 + 5;
} else {
n1++;
}
System.out.println(n1);
}
}
Q5:Write a program to give array as input to method
public class Q5 {
static void array(int[] abb) {
System.out.println("HI");
}
public static void main(String[] args) {
int[] arr = { 10, 20, 50 };
array(arr);
}
}
Q6:Write a program to return array from a method
import java.util.Arrays;
public class Q6 {
static int[] array()
{
int []arr= {10,60,45};
return arr;
}
public static void main(String[] args) {
int[] abb =array();
System.out.println(Arrays.toString(abb));
}
}
Q7:Write a program to swap two number using third variable.
import java.util.Scanner;
public class Q7 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter first number a");
int a = sc.nextInt();
System.out.println("Enter first number b");
int b = sc.nextInt();
System.out.println("a=" + a + " b=" + b);
int c = a;
a = b;
b = c;
System.out.println("After swapping");
System.out.println("a=" + a + " b=" + b);
}
}
Q8:Write a program to swap two number without using third variable.
import java.util.Scanner;
public class Q8{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter first number a");
int a = sc.nextInt();
System.out.println("Enter first number b");
int b = sc.nextInt();
System.out.println("a=" + a + " b=" + b);
a=a+b;
b= a-b;
a = a-b;
System.out.println("After swapping");
System.out.println("a=" + a + " b=" + b);
}
}
Q9:Write a program to find factorial of given number.
import java.util.Scanner;
public class Q9 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter first number");
int no = sc.nextInt();
int fact = 1;
for (int i = no; i > 0; i--) {
fact = fact * i;
}
}
}
Q10:Write a program to fibonacci series for first 10 numbers.
public class Q10 {
public static void main(String[] args) {
int fib1=0;
int fib2=1;
int fib3;
System.out.print(fib1+" "+fib2+" ");
for(int i=1;i<=10;i++)
{
fib3=fib1+fib2;
fib1=fib2;
fib2=fib3;
System.out.print(fib3+" ");
}
}
}
Q11:Write a program to find square of given number.
import java.util.Scanner;
public class Q11 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number");
int no = sc.nextInt();
int square = no * no;
System.out.println("The of the number " + no + " is " + square);
}
}
Q12:Write a program to print 2 table.
public class Q12 {
public static void main(String[] args) {
for (int i = 1; i <= 10; i++) {
System.out.println("2*" + i + "=" + (2 * i));
}
}
}
Q13:Write a program to print fizz fizz if divisible by 2, buzz buzz if divisible by 3, fizz buzz if divisible by both
2 nad 3.
public class Q13 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number");
int no = sc.nextInt();
if (no % 2 == 0 && no % 3 == 0) {
System.out.println("Fizz Buzz");
} else if (no % 2 == 0) {
System.out.println("Fizz Fizz");
} else if (no % 3 == 0) {
System.out.println("Buzz Buzz");
}
} }
Q14:Write a program to print the numbers which is divisible by 2 for range 1 to 100.
public class Q14 {
public static void main(String[] args) {
for (int i = 1; i <= 100; i++) {
if (i % 2 == 0) {
System.out.print(i+" ");
}
}
}
}
Q15:Write a program to print the numbers which is divisible by 3 for range 1 to 100.
public class Q15 {
public static void main(String[] args) {
for (int i = 1; i <= 100; i++) {
if (i % 3 == 0) {
System.out.print(i+" ");
}
}
}
}
Q16:Write a program to print the Fizz Fizz which is divisible by 3 for range 1 to 100 and count the number of
numbers.
public class Q16 {
public static void main(String[] args) {
int count=0;
for (int i = 1; i <= 100; i++) {
if (i % 3 == 0) {
System.out.println("Fizz Fizz ");
count++;
}
}
System.out.println(count);
}
}
Q17:Write a Program to call area() method with return type
public class Q17 {
static double area()
{
int r=5;
return 3.14*5*5;
}
public static void main(String[] args) {
double result=area();
System.out.println("Area of circle is "+result);
}
}
Q18:Write a program to differentiate between print and println
public class Q18 {
public static void main(String[] args) {
System.out.print("Hello ");
System.out.println("Hi");
}
}
Q19:Write a program to reverse a string
import java.util.Scanner;
public class Q19 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the string");
String s = sc.next();
for (int i = s.length()-1; i >= 0; i--) {
System.out.print(s.charAt(i));
}
}
}
Q20:Write a program to print from 1 to 10
public class Q20 {
public static void main(String[] args) {
for (int i = 1; i <= 10; i++) {
System.out.println(i);
}
}
}
Q21:Write a program to print only even numbers between to 1 to 10
public class Q21 {
public static void main(String[] args) {
for (int i = 1; i <= 10; i++) {
if (i % 2 == 0) {
System.out.println(i);
}
}
}
}
Q22:Write a program to print only odd numbers between to 1 to 10
public class Q22 {
public static void main(String[] args) {
for (int i = 1; i <= 10; i++) {
if (i % 2 == 1) {
System.out.println(i);
}
}
}
}
Q23:Write a program to find given number is prime or not
import java.util.Scanner;
public class Q23 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number");
int no = sc.nextInt();
boolean flag = true;
for (int i = 2; i < no; i++) {
if (no % i == 0) {
flag = false;
}
}
if (flag == true) {
System.out.println(no + " is a prime number");
} else {
System.out.println(no + " is not a prime number");
}
}
}
Q24:Write a program to print all prime numbers between a to 100.
public class Q24 {
public static void main(String[] args) {
for (int i = 1; i <= 100; i++) {
int no = i;
boolean flag = true;
for (int j = 2; j < no; j++) {
if (no % j == 0) {
flag = false;
}
}
if (flag == true) {
System.out.println(no);
}
}
}
}
Q25:Write a program to take dynamic input from user and check number is even or odd
public class Q25 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number");
int no = sc.nextInt();
if (no % 2 == 0) {
System.out.println(no + " is an even number");
} else {
System.out.println(no + " is a odd number");
}
}
}
Q26:Write a program to swap to strings taking dynamic input
public class Q26{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter first string");
String s1=sc.next();
System.out.println("Enter second string");
String s2=sc.next();
System.out.println("First string is "+s1+" and Second string is "+s2);
String s3=s1;
s1=s2;
s2=s3;
System.out.println("After swapping");
System.out.println("First string is "+s1+" and Second string is "+s2);
}
}
Q27:Write a program to print 1 to 10 using while loop
public class Q27 {
public static void main(String[] args) {
int no=1;
while(no<=10)
{
System.out.println(no);
no++;
}
}
}
Q28:Write a program to print 1 to 10 without using any loop.
public class Q28 {
static void num(int no) {
if (no <= 10) {
System.out.println(no);
}
no++;
num(no);
}
public static void main(String[] args) {
num(1);
}
}
Q29:Write a program to print sum of digits of given number.
public class Q29 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter num");
int no = sc.nextInt();
int sum=0;
while(no!=0) {
int rem=no%10;
sum=sum+rem;
no=no/10;
}
System.out.println("Sum of digits of number is " +sum);
}
}
Q30:Write a program to print sum of squares of digits of given number.
public class Q30 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter num");
int no = sc.nextInt();
int sum=0;
while(no!=0) {
int rem=no%10;
sum=sum+rem*rem;
no=no/10;
}
System.out.println("Sum of digits of number is " +sum);
}
}
Q31:Write a program to print sum of cubes of digits of given number.
public class Q31 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter num");
int no = sc.nextInt();
int sum=0;
while(no!=0) {
int rem=no%10;
sum=sum+rem*rem*rem;
no=no/10;
}
System.out.println("Sum of digits of number is " +sum);
}
}
Q32:Write a program to check given number is arm strong number or not.
import java.util.Scanner;
public class Q32 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter num");
int no = sc.nextInt();
int copy = no;
int sum = 0;
while (no != 0) {
int rem = no % 10;
sum = sum + rem * rem * rem;
no = no / 10;
}
if (sum == copy) {
System.out.println(copy + " is an armstrong number");
}else {
System.out.println(copy + " is not an armstrong number");
}
}
}
Q33:Write a program to find sum of factorial of digits of given number.
import java.util.Scanner;
public class Q33 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter num");
int no = sc.nextInt();
int sum = 0;
while (no != 0) {
int rem = no % 10;
int fact = 1;
for (int i = rem; i > 0; i--) {
fact = fact * i;
}
sum = sum + fact;
no = no / 10;
}
System.out.println("Sum of factorial of digits of number is " + no);
}
}
Q34:Write a program to check given number is strong number or not.
import java.util.Scanner;
public class Q34 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter num");
int no = sc.nextInt();
int copy=no;
int sum = 0;
while (no != 0) {
int rem = no % 10;
int fact = 1;
for (int i = rem; i > 0; i--) {
fact = fact * i;
}
sum = sum + fact;
no = no / 10;}
if (sum == copy) {
System.out.println(copy + " is an strong number");
} else {
System.out.println(copy + " is not an strong number");
}
}
}
Q35:Write a program to sort an array in ascending order.
import java.util.Arrays;
import java.util.Scanner;
public class Q35 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the size of array");
int size = sc.nextInt();
System.out.println("Enter the numbers");
int[] arr = new int[size];
for (int i = 0; i < size; i++) {
arr[i] = sc.nextInt();
}
int temp = 0;
for (int i = 0; i < arr.length; i++) {
for (int j = 1; j < arr.length - i; j++) {
if (arr[j - 1] > arr[j]) {
temp = arr[j - 1];
arr[j - 1] = arr[j];
arr[j] = temp;
}
}
}
System.out.println("Sorted array is " + Arrays.toString(arr));
}
}
Q38:Write a program to separate charracters digits and special characters from given String.
import java.util.Scanner;
public class Q38 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the String");
String s1 = sc.next();
s1.toLowerCase();
String alpha = "";
String digits = "";
String spcl = "";
for (int i = 0; i < s1.length(); i++) {
char ch = s1.charAt(i);
if (Character.isAlphabetic(ch)) {
alpha = alpha + ch;
} else if (Character.isDigit(ch)) {
digits = digits + ch;
} else {
spcl = spcl + ch;
}
}
System.out.println(alpha);
System.out.println(digits);
System.out.println(spcl);
}
}
Q37:Write a program to count no of vowels in given string.
import java.util.Scanner;
public class Q37
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the String");
String s1 = sc.next();
int count = 0;
for (int i = 0; i < s1.length(); i++) {
char ch = s1.charAt(i);
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {
count++;
}
}
System.out.println("The num of vowels in the string is " + count);
}
}
Q39:Write a program to print even position of given string
import java.util.Scanner;
public class Q39
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the String");
String s1 = sc.next();
for (int i = 0; i < s1.length(); i++) {
if(i%2==0) {
System.out.print(s1.charAt(i));
}
}
}
}
Q40:Write a program to print number of spcl char in given string.
import java.util.Scanner;
public class Q40
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the String");
String s1 = sc.next();
int count = 0;
for (int i = 0; i < s1.length(); i++) {
char ch = s1.charAt(i);
if ((Character.isAlphabetic(ch) == false) && (Character.isDigit(ch) == false)) {
count++;
}
}
System.out.print("The no of spcl characters in given string is " + count);
}
}