5.coding Questions
5.coding Questions
Write a Java Program to print the sum of the first N natural number
Input : 5
Output: 15
import java.util.*;
public class Abc
{
public static void main(String[] args)
{
int n;
int sum=0;
System.out.println("enter n value");
Scanner sc1= new Scanner(System.in);
n=sc1.nextInt();
for(int i = 1; i <= n; i++)
{
sum += i;
}
System.out.print(sum);
}
}
import java.util.*;
public class Abc
{
public static voidmain(String[] args)
{
int a, b, c, smallest;
Scanner sc = new Scanner(System.in);
System.out.println("Enter numbers ");
a = sc.nextInt();
b = sc.nextInt();
c = sc.nextInt();
smallest = c < (a < b ? a : b) ? c : ((a < b)
? a : b);
System.out.println("The smallest number is:
"+smallest);
}}
import java.util.Scanner;
public class Abc
{
public static void main(String []args)
{
Scanner sc=new Scanner(System.in);
System.out.println("enter a number");
int num= sc.nextInt();
boolean flag = false;
for (int i = 2; i <= num / 2; ++i)
{
if (num % i == 0)
{
flag = true;
break;
}
}
if (!flag)
System.out.println("Prime Number");
else
System.out.println("Not a Prime Number");
}
}
import java.util.*;
public class Abc
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.print("enter n value ");
package hello;
import java.util.*;
public class Abc
{
public static void main(String[] args)
{
int a, b;
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the first number: ");
a = scanner.nextInt();
System.out.print("Enter the second number: ");
b = scanner.nextInt();
System.out.println("Before swapping:");
System.out.println("a = " +a +", b = " +b);
import java.util.Scanner;
public class Abc
{
public static void main(String [] args)
{
Scanner sc=new Scanner(System.in);
System.out.print("enter number");
int n=sc.nextInt();
if(n%2==0)
System.out.println("even number");
else
System.out.println("odd number");
}
}
import java.util.*;
public class Abc
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("enter the number");
int num= sc.nextInt();
solution(num);
}
public static void solution(int n)
{
int r,sum=0,temp;
temp=n;
while(n>0)
{
r=n%10;
sum=(sum*10)+r;
n=n/10;
}
if(temp==sum)
System.out.println("palindrome number");
else
System.out.println("not palindrome");
}
}
import java.util.*;
public class Abc
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.print("enter n value");
int n=sc.nextInt();
solution(n);
}
public static void solution(int number)
{
int reverse = 0,remainder=0;
while(number != 0)
{
remainder = number % 10;
reverse = reverse * 10 + remainder;
number = number/10;
}
System.out.println(reverse);
}
}
import java.util.*;
public class Abc
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("enter number");
int n =sc.nextInt();
int num1 = 0, num2 = 1;
int counter = 0;
while (counter < n)
{
System.out.print(num1 + " ");
int num3 = num2 + num1;
num1 = num2;
num2 = num3;
counter = counter + 1;
}
}
}
import java.util.*;
public class Abc
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.print("enter number");
int n=sc.nextInt();
int i, temp, count=0;
temp=n;
while (temp>0)
{
count++;
temp = temp/10;
}
double num = Math.pow(10, count-1);
i = (int)num;
for (;i>0;i/=10)
System.out.println(n/i%10);
}
}
import java.util.*;
public class Abc
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("enter the number");
int n=sc.nextInt();
int fac,temp,sum = 0;
temp=n;
while(n != 0)
{
fac = 1;
int r = n % 10;
for(int i = r ; i >= 1 ; i--)
fac = fac * i;
sum = sum + fac;
n=n/10;
}
if(sum == temp)
System.out.println("Strong Number");
else
System.out.println("Not a Strong Number");
}
}
import java.util.Scanner;
public class Abc
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
System.out.println("enter numbers");
int a =sc.nextInt();
int b = sc.nextInt();
int gcdValue;
gcdValue = gcd(a, b);
System.out.print(gcdValue + " ");
System.out.println(lcm(a, b));
}
static int gcd(int a, int b)
{
if (b == 0)
return a;
else
return gcd(b, a % b);
}
static int lcm(int a, int b)
{
int max, lcm=0, step;
if(a > b)
{
max = step = a;
}
else
{
max = step = b;
}
while(a!= 0)
{
if(max % a == 0 && max % b == 0)
import java.util.Scanner;
public class Abc
{
public static void main(String []args)
{
Scanner sc=new Scanner(System.in);
System.out.println("enter a number");
int num= sc.nextInt();
int temp, rem, i;
int count = 0;
int sum = 0;
temp = num;
for(i = 1; i <= temp; i++)
if(temp%i == 0)
count++;
while(num > 0)
{
rem = num%10;
sum = sum*10+rem;
num = num/10;
17. Write a java program to find the sum of the digits in a number
Input:48
Output:12
Explanation:
Input: 48 add each digit individually
4+8=12
so the output is 12
import java.util.Scanner;
public class Abc
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("enter number");
int n =sc.nextInt();
int sum = 0;
while (n != 0)
{
sum = sum + n % 10;
n = n/10;
}
System.out.println(sum);
}
}
import java.util.*;
public class Abc
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.print("enter n value");
int n=sc.nextInt();
int [] arr= new int[n];
System.out.print("enter array elements");
for(int i=0;i<arr.length;i++)
{
arr[i]=sc.nextInt();
}
int size=n;
int odd_count=0,even_count=0;
for(int i = 0; i < size; i++)
{
if(arr[i]%2!=0)
odd_count++;
else
even_count++;
}
System.out.print(odd_count+" "+even_count);
}
}
import java.util.*;
public class Abc
{
public static void main(String args[])
{
int n;
Scanner sc=new Scanner(System.in);
System.out.print("enter size of array");
n = sc.nextInt();
int [] arr= new int[n];
System.out.print("enterarray elements");
for(int i=0;i<n;i++)
{
arr[i]=sc.nextInt();
}
for(int i = 0; i < n; i++)
{
for(int j = i + 1; j < n; j++)
{
if(arr[i] == arr[j])
System.out.print(arr[j]+" ");
}
}
}}
import java.util.*;
public class Abc
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("enter size of array");
int n=sc.nextInt();
int [] arr= new int[n];
System.out.println("enter array elements");
for(int i=0;i<arr.length;i++)
{
arr[i]= sc.nextInt();
}
double m=0;
int x=arr.length;
if(n%2==1)
m=arr[n/2];
else
m=(arr[n/2-1]+arr[n/2])/2;
System.out.println(m);
}
}
25. Write a Java program to check if two arrays are equal or not.( No need to
print "Yes" or "No", only return true to the main method if the arrays are
same else return false)
Input : arr1[] = {1, 2, 5, 4, 0};
arr2[] = {2, 4, 5, 0, 1};
Output : Yes
Input : arr1[] = {1, 2, 5, 4, 0, 2, 1};
arr2[] = {2, 4, 5, 0, 1, 1, 2};
Output : Yes
Input : arr1[] = {1, 7, 1};
arr2[] = {7, 7, 1};
Output : No
import java.util.*;
public class Abc
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("enter array size");
int n=sc.nextInt();
int [] arr1= new int[n];
int [] arr2= new int[n];
System.out.println("enter array1");
for(int i=0 ;i<n;i++)
import java.util.Scanner;
public class Abc
{
public static void search(int arr[], int x)
{
int n=arr.length;
int flag=0;
for(int i = 0; i < n; i++)
{
if(arr[i] == x)
flag =1;
}
if(flag == 1)
System.out.print("Element Found");
else
System.out.print("Element not Found");
}
public static void main(String [] args)
{
Scanner sc=new Scanner(System.in);
System.out.print("enter size of array");
int n=sc.nextInt();
int [] arr= new int[n];
System.out.print("enter array elements ");
for(int i=0;i<n;i++)
{
arr[i]=sc.nextInt();
}
System.out.println("enter search element");
int item =sc.nextInt();
search(arr, item);
}
}
import java.util.Scanner;
public class Abc
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.print("enter size of array");
int n=sc.nextInt();
int [] arr= new int[n];
System.out.print("enter array elements ");
for(int i=0;i<arr.length;i++)
{
arr[i]=sc.nextInt();
}
int temp=0;
for( int i=0;i<n ; i++,n--)
{
temp=arr[i];
arr[i]=arr[n-1];
arr[n-1]=temp;
}
for( int i=0; i < arr.length; i++)
{
System.out.print(arr[i]+" ");
}
}
}
import java.util.*;
public class Abc
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.print("enter size of array");
int n=sc.nextInt();
int [] arr= new int[n];
System.out.print("enter array elements ");
for(int i=0;i<n;i++)
{
arr[i]=sc.nextInt();
}
int i,j;
j = 0;
for(i = 0; i < n; i++)
{
if(arr[i] < 0)
{
if(i != j)
{
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
j++;
}
}
for(i = 0; i < n; i++)
{
System.out.print(arr[i]+" ");
}
}
}
import java.util.*;
public class Abc
{
public static void main(String[] args)
{
int n;
System.out.println("enter size of array");
Scanner sc=new Scanner(System.in);
n=sc.nextInt();
int [] arr = new int [n];
System.out.println("enter array elements");
for(int i=0;i<arr.length;i++)
{
arr[i]= sc.nextInt();
}
solution(arr);
}
public static void solution(int []a)
{
int count = 0;
for (int i = 0; i <a.length; i++)
{
count += a[i];
}
System.out.println(count);
}}
33. Binary Search in Java. (No need to print only return the position of the
search element in the array if found, else return -1 to the driver function if
search element not found in the array)
Binary Search in Java is a search algorithm that finds the position of a
target value within a sorted array.
Input: [19 28 15 17 83], 28
import java.util.*;
public class Abc
{
public static void main(String args[])
{
int n;
Scanner sc=new Scanner(System.in);
System.out.print("enter search element");
int searchElement=sc.nextInt();
System.out.print("enter array size");
n=sc.nextInt();
int [] arr= new int[n-1];
System.out.print("enter array elements");
for(int i=0;i<arr.length-1;i++)
arr[i]=sc.nextInt();
int result=solution(arr,searchElement);
if (result == -1)
System.out.println("Element not found");
else
System.out.println("Element found at "
+ "index " + result);
}
public static int solution(int [] arr,int x)
{
Arrays.sort(arr);
int l = 0, r = arr.length - 1;
while (l <= r)
{
int m = l + (r - l) / 2;
if (arr[m] == x)
return m;
if (arr[m] < x)
l = m + 1;
else
r = m - 1;
}
return -1;
}}
System.out.println();
}
}
}
import java.util.*;
public class Abc
{
public static void main(String[] args)
{
System.out.println("enter a statement");
Scanner sc=new Scanner(System.in);
String s = sc.nextLine();
solution(s);
}
public static void solution(String str)
{
int count = 1;
for (int i = 0; i < str.length() - 1; i++)
{
if ((str.charAt(i) == ' ') && (str.charAt(i + 1) != ' '))
{
count++;
}
}
System.out.println(count);
}
}
import java.util.*;
public class Abc
{
public static void main(String[] args)
{
int i =0;
import java.util.*;
public class Abc
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("enter string");
String s=sc.nextLine();
int count = 0;
s= s.toLowerCase();
for (int i = 0; i < s.length(); i++)
{
if (s.charAt(i) == 'a' || s.charAt(i) == 'e'
|| s.charAt(i) == 'i'
|| s.charAt(i) == 'o'
|| s.charAt(i) == 'u')
{
count++;
}
44. Given a string, we have to find the longest word in the input string and
then calculate the number of characters in this word. (In Java)
Input: Java Coding Questions
Output: 9
import java.util.*;
public class Abc
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.print("enter a string");
String s= sc.nextLine();
solution(s);
}
public static void solution(String str)
{
int n = str.length();
int res = 0, curr_len = 0;
for (int i = 0; i < n; i++)
{
if (str.charAt(i) != ' ')
curr_len++;
else
{
res = Math.max(res, curr_len);
curr_len = 0;
}
}
System.out.print(Math.max(res, curr_len));
}
}
47. Write a java program to remove all vowels from String and print the string
Input: hello
Output: hll
import java.util.Scanner;
public class Abc
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("enter string");
String str=sc.nextLine();
import java.util.Scanner;
public class Abc
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int i =0;
System.out.println("enter string");
String s =sc.nextLine() ;
s=s.toLowerCase();
solution(s);
}
public static void solution(String str)
{
String rev="";
int length = str.length();
for ( int i = length - 1; i >= 0; i-- )
rev = rev + str.charAt(i);
if (str.equals(rev))
System.out.println("palindrome");
else
System.out.println("not palindrome");
}
}