Cracking the coding interview | Step-1

DAY 1:coding interview questions

Question 1:

IsEven?
Write a function to find whether the given input number is Even.
If the given number is even, the function should return 2 else it should return 1.
Note: 
The number passed to the function can either be negative, positive or zero.
Zero should be treated as Even.

//Code:
import java.io.*;
import  java.util.*;
class UserMainCode
{
 public int isEven(int input1){
  if(input1%2==0)
   return 2;
  else
   return 1;
                     
 }
}
Top 10 in-demand programming languages to learn in 2020

Question 2:

IsOdd?

Write a function to find whether the given input number is Odd.
If the given number is odd, the function should return 2 else it should return 1.
Note:
 The number passed to the function can either be negative, positive or zero.
Zero should be treated as Even.

//Code:


import java.io.*;
import  java.util.*;
class UserMainCode
{
 public int isEven(int input1){
  if(input1%2!=0)
   return 2;
  else
   return 1;
 }
}

Question 3:

Return the last digit of the given number.

Write a function that returns the last digit of the given number. The last digit is being referred to the least significant digit i.e the digit in the ones(units) place in the number.
The last digit should be returned as a positive number.
for example,
if the given number is 197, the last digit is 7
if the given number is -197, the last digit is 7

//Code:
import java.io.*;
import  java.util.*;
class UserMainCode
{
 public int lastDigitOf(int input1){
     if(input1<0)
  {
   input1*=-1;
  }
  int n=input1%10;
  return n;
 }
}


Question 4:

Return the second last digit of the given number.

Write a function that returns the second last digit of the given number. The second last digit is being referred to the digit in the tens place in the given number.
For example, if the given number is 197, the second last digit is 9
Note 1 - The second last digit should be returned as a positive number. i.e. if the given number is -197, the second last digit is 9
Note 2 - If the given number is a single-digit number, then the second last digit does not exist. In such cases, the function should return -1. i.e. if the given number is 5, the second last digit should be returned as -1.


//Code:
import java.io.*;
import  java.util.*;
class UserMainCode
{
 public int secondLastDigitOf(int input1){
  if(input1>=-9 && input1<=9)
   return -1;
   if(input1<0)
   input1*=-1;
  input1/=10;
  int n=input1%10;
  return n;
 }
}

Question 5:

Sum of last digits of two given numbers


Rohit wants to add the last digits of two given numbers.
For example,
If the given numbers are 267 and 154, the output should be 11.
Below is the explanation-
The last digit of the 267 is 7
The last digit of the 154 is 4
Sum of  7 and 4 = 11
Write a program to help Rohit achieve this for any given two numbers.
The prototype of the method should be-
int addLastDigits(int input1, int input2);
where input1 and input2 denote the two numbers whose last digits are to be added.
Note: The sign of the input numbers should be ignored.
i.e.
if the input numbers are 267 and 154, the sum of last two digits should be 11
if the input numbers are 267 and -154, the sum of last two digits should be 11
if the input numbers are -267 and 154, the sum of last two digits should be 11
if the input numbers are -267 and -154, the sum of last two digits should be 11

//Code:

import java.io.*;
import  java.util.*;
class UserMainCode
{
 public int addLastDigits(int input1,int input2){
    if(input1<0)
   input1*=-1;
  if(input2<0)
   input2*=-1;
  int x=input1%10;
  int y=input2%10;
  int sum = x+y;
  return sum;
 }
}

Cracking the coding interview | Step-2

C/C++/java/Python practice questions with solution

SQL Queries   Android Project Ideas





Post a Comment

1 Comments

  1. Return last digit of the given number.

    Write a function that returns the last digit of the given number. Last digit is being referred to the least significant digit i.e. the digit in the ones (units) place in the given number.

    The last digit should be returned as a positive number. for example,

    if the given number is 197, the last digit is 7

    if the given number is -197, the last digit is 7

    ReplyDelete

Please do not Enter any spam link in the comment box