Posts

Showing posts from February, 2019

FIND A PERFECT NUMBER

Perfect Numbers Submissions:  20008    Accuracy:  36.64%    Difficulty:  Easy    Marks: 2 Associated Course(s):    Interview Preparation Show Topic Tags          Wipro Given a number  N,  check if a number is perfect or not. A number is said to be perfect if sum of all its factors excluding the number itself is equal to the number. Input: First line consists of  T  test cases. Then T test cases follow .Each test case consists of a number  N. Output: For each testcase, in a new line, output in a single line  1  if a number is a perfect number else print  0 . Constraints: 1 <=  T  <= 300 1 <=  N  <= 10 18 Example: Input: 2 6 21 Output: 1 0 ** For More Input/Output Examples Use  'Expected Output'  option ** import java.util.*; import java.lang.*; import java.io.*; class GFG { public static void main (String[] args) { Scanner s=new Scanner(System.in); int t=s.nextInt(); for(int i=0;i<t;i++) {     i

FIND ADDITION OF TWO FRACTIONS

You are given four numbers  num1, den1, num2, and den2 . You need to find (num1/den1)+(num2/den2)  and output the result in the form of  (numx/denx) . Input Format: The first line of input contains an integer  T  denoting the number of test cases . Then T test cases follow . Each test case contains four integers  num1, den1, num2, den2 respectively . Output Format: For each test case, in a new line,  output will be the fraction in the form  a/b  where the fraction denotes the sum of the two given fractions in reduced form. Your Task: Since this is a function problem, you don't need to worry about the testcases. Your task is to complete the function  addFraction   which adds the two fractions and prints the resulting fraction. The function takes four arguments  num1, den1, num2, den2 where  num1, num2  denotes the  numerators  of two fractions and  den1, den2 denotes their  denominators . Constraints: 1 <= T <= 100 1 <= den1,den2,num1,num2 <= 1000 Example:

FIND GCD OF AN ARRAY

Given an array of  N  positive integers. Your task is to find the  GCD  of all numbers of the array. Input: First line of input contains number of test cases  T . First line of test case contains a positive integer N, size of the array. Next line contains the array elements. Output: For each testcase, print the GCD of array elements. Constraints: 1 <= T <= 100 1 <= N <= 10 6 1 <= Arr[i] <= 10 6 Example: Input: 1 2 5 10 Output: 5 import java.util.*; import java.lang.*; import java.io.*; class GFG { public static void main (String[] args) { Scanner s=new Scanner(System.in); int t=s.nextInt(); for(int i=0;i<t;i++) {   int x=s.nextInt();   int[] arr=new int[x];   for(int j=0;j<x;j++)   {       arr[j]=s.nextInt();   }   System.out.println(findGCD(arr, x));  } } static int findGCD(int arr[], int x)      {          int result = arr[0];          for (int i = 1; i < x; i++)   

FIND CLOSEST NUMBER

Given  non-zero  two integers  N  and  M . The problem is to find the number closest to  N and divisible by  M . If there are more than one such number, then output the one having maximum absolute value . Input: The first line consists of an integer  T  i.e number of test cases. T testcases follow.  The first and only line of each test case contains two space separated integers  N  and  M . Output: For each testcase, in a new line, print the closest number to N which is also divisible by M. Constraints:  1 <= T <= 100 -1000 <= N, M <= 1000 Example: Input: 2 13 4 -15 6 Output: 12 -18 import java.util.Scanner; import java.lang.Math; class Closest { public static void main(String[] args) { Scanner s=new Scanner(System.in); int t=s.nextInt(); double c,d; for(int i=0;i<t;i++) { double a=s.nextInt(); double b=s.nextInt();                  d=Math.abs(a);              c=d%b;              if(a>0){