Practice Programs For Tcs
Practice Programs For Tcs
// Sum of bits of x and y where at least one of the bits is not set
x = x ^ y;
2. Substraction
public void subtract(int x, int y)
{
// Iterate till there is no carry
while (y != 0)
{
// borrow contains common set bits of y and unset
// bits of x
int borrow = (~x) & y;
3. Division
public void div(int dvdnd, int dvsr)
{
// initialize dividend and divisor
int dividend = dvdnd;
int divisor = dvsr;
// initialize quotient
int quotient = 0;
//loop till the divisor does not become smaller than dividend
while(dividend >= divisor)
{
dividend = dividend - divisor;
quotient++;
}
// print results
System.err.println("Quotient is "+quotient);
4. Multiplication
public void Mul(int x, int y)
{
int c=0;
for(int i=0;i<y;i++){
c=c+x;
}
System.err.println("The Product is : "+c);
}
5. Armstrong Number
public class Armstrong {
public String checkNumber(int input_number)
{
int sum = 0, r, temp = input_number;
while(input_number != 0)
{
r = input_number%10;
input_number = input_number/10;
sum = sum+(r*r*r);
}
if(temp == sum)
return "Number is armstrong!";
else
return "Number not armstrong!";
}
}
6. EvenOdd
public class EvenOdd {
public void checkEvenOdd(int input_number)
{
if(input_number%2 == 0)
System.err.println("Number is even!");
else
System.err.println("Number is odd!");
}
}
7. Factorial
public class Factorial {
public void printFactorial(int input_number)
{
System.err.print("Factorial of "+input_number+" is : ");
int f=1;
while(input_number > 0)
{
f=f*input_number;
input_number--;
}
System.err.println(f);
}
}
8. Fibonacci
public class Fibonacci {
public void printFibonacci(int number_upto)
{
int n1=0,n2=1,n3,i,count=number_upto;
System.out.print(n1+" "+n2);//printing 0 and 1
System.out.println();
}
}
9. Leap Year
public class LeapYear {
public void checkYear(int input_year)
{
int year = input_year;
boolean leap = false;
if(year % 4 == 0)
{
if( year % 100 == 0)
{
if ( year % 400 == 0)
leap = true;
else
leap = false;
}
else
leap = true;
}
else
leap = false;
if(leap)
System.err.println(year + " is a leap year!");
else
System.err.println(year + " is not a leap year!");
}
}
10. Palindrome
public class Palindrome {
public String checkPalindrome(int input_number)
{
int number = 0, remainder, temp = input_number;
while (input_number!=0)
{
remainder = input_number%10;
number = (number*10) + remainder;
input_number= input_number/10;
}
if(temp == number)
return "Number is palindrome!";
else
return "Number is not palindrome!";
}
}
while(i<=input_number)
{
if(input_number%i == 0)
c++;
i++;
}
if(c == 2)
System.err.println("Number is prime!");
else
System.err.println("Number is not prime!");
}
}
NUMBER CONVERSION
14. Binary to Decimal
public class BinaryToDec {
public static void main(String args[])
{
int binnum, decnum=0, i=1, rem;
Scanner scan = new Scanner(System.in);
char hex[]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
Scanner scan = new Scanner(System.in);
System.out.print("Enter Binary Number : ");
binnum = scan.nextInt();
}
}
quot = binnum;
while(quot != 0)
{
octnum[i++] = quot%8;
quot = quot/8;
}
quot = decnum;
while(quot != 0)
{
binnum[i++] = quot%2;
quot = quot/2;
}
char hex[]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
while(decnum>0)
{
rem = decnum%16;
hexdecnum = hex[rem] + hexdecnum;
decnum = decnum/16;
}
quot = decnum;
while(quot != 0)
{
octnum[i++] = quot%8;
quot = quot/8;
}
decnum = hex2decimal(hexdecnum);
decnum = hex2decimal(hexdecnum);
while(decnum != 0)
{
octnum[i++] = decnum%8;
decnum = decnum/8;
}
quot = octnum;
while(quot != 0)
{
binnum[i++] = quot%2;
quot = quot/2;
}
orig = octnum;
while(octnum != 0)
{
decnum = decnum + (octnum%10) * (int) Math.pow(8, i);
i++;
octnum = octnum/10;
}
4. Three Arguments
$ ./a.out First Second Third
Program Name Is: ./a.out
Number Of Arguments Passed: 4
----Following Are The Command Line Arguments Passed----
argv[0]: ./a.out
argv[1]: First
argv[2]: Second
argv[3]: Third
******************************************************************