0% found this document useful (0 votes)
58 views

Practice Programs For Tcs

The document describes various arithmetic and logical programs written in Java without using operators. It includes programs to perform addition, subtraction, division, multiplication, check if a number is Armstrong, even or odd, calculate factorial, Fibonacci series, check if a year is a leap year, check if a number is a palindrome, prime number, print a star pattern, reverse a number, convert between binary, decimal and hexadecimal number systems, and convert a decimal number to octal. The programs demonstrate performing these operations through bitwise operations and loops instead of using arithmetic operators.

Uploaded by

Tanu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
58 views

Practice Programs For Tcs

The document describes various arithmetic and logical programs written in Java without using operators. It includes programs to perform addition, subtraction, division, multiplication, check if a number is Armstrong, even or odd, calculate factorial, Fibonacci series, check if a year is a leap year, check if a number is a palindrome, prime number, print a star pattern, reverse a number, convert between binary, decimal and hexadecimal number systems, and convert a decimal number to octal. The programs demonstrate performing these operations through bitwise operations and loops instead of using arithmetic operators.

Uploaded by

Tanu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

PRACTICE PROGRAMS FOR TCS BY SHUBHAM THAKARE

Guided By, Prof. Vediya Raghuvanshi

ARITHMETIC OPERATIONS WITHOUT USING OPERATORS


1. Addition
public void Add(int x, int y)
{
// Iterate till there is no carry
while (y != 0)
{
// carry now contains common set bits of x and y
int carry = x & y;

// Sum of bits of x and y where at least one of the bits is not set
x = x ^ y;

// Carry is shifted by one so that adding it to x gives the required sum


y = carry << 1;
}
System.err.println("Addition : "+x);
}

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;

// Subtraction of bits of x and y where at least


// one of the bits is not set
x = x ^ y;

// Borrow is shifted by one so that subtracting it from


// x gives the required sum
y = borrow << 1;
}
System.err.println("Substraction is : "+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);

// result of last subtraction


System.err.println("Remainder is "+dividend);
}

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

for(i=2;i<count;i++)//loop starts from 2 because 0 and 1 are already printed


{
n3=n1+n2;
System.out.print(" "+n3);
n1=n2;
n2=n3;
}

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!";
}
}

11. Prime Number


public class PrimeNumber {
public void checkPrime(int input_number)
{
int i=1, c=0;

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!");
}
}

12. Print Star


public class PrintStar {
int i=0;
int j=0;
public void print()
{
for (i=1;i<=4;i++)
{
for (j=1;j<=i;j++)
{
System.out.print("*");
}
System.out.println("");
}
}
}

13. Reversing Number


public class ReversingNumber {
public int reverse(int input_number)
{
int number =0, remainder;
while (input_number!=0)
{
remainder = input_number%10;
number = number*10 + remainder;
input_number= input_number/10;
}
return number;
}
}

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);

System.out.print("Enter Binary Number : ");


binnum = scan.nextInt();

// converting the number into decimal format


while(binnum != 0)
{
rem = binnum%10;
decnum = decnum + rem*i;
i = i*2;
binnum = binnum/10;
}

System.out.print("Equivalent Decimal is : ");


System.out.println(decnum);
}
}

15. Binary to Hex


public class BinaryToHex {
public static void main(String args[])
{
int binnum, rem;
String hexdecnum="";

// digits in hexadecimal number system

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();

// converting the number in hexadecimal format


while(binnum>0)
{
rem = binnum%16;
hexdecnum = hex[rem] + hexdecnum;
binnum = binnum/16;
}

System.out.print("Equivalent Hexadecimal Value of " +binnum+ " is :\n");


System.out.print(hexdecnum);

}
}

16. Binary to Octal


public class BinaryToOctal {
public static void main(String args[])
{
int binnum, rem, quot, i=1, j;
int octnum[] = new int[100];
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;
}

System.out.print("Equivalent Octal Value of " +binnum+ " is :\n");


for(j=i-1; j>0; j--)
{
System.out.print(octnum[j]);
}
}
}

17. Decimal to Binary


public class DecToBinary {
public DecToBinary()
{
int decnum, rem, quot, i=1, j;
int binnum[] = new int[100];
Scanner scan = new Scanner(System.in);

System.out.print("Enter any Decimal Number : ");


decnum = scan.nextInt();

quot = decnum;

while(quot != 0)
{
binnum[i++] = quot%2;
quot = quot/2;
}

System.out.print("Equivalent Binary Value of " + decnum + " is :\n");


for(j=i-1; j>0; j--)
{
System.out.print(binnum[j]);
}
}
public static void main(String[] args) {
new DecToBinary();
}
}

18. Decimal to Hex


public class DecToHex {
public static void main(String args[])
{
int decnum, rem;
String hexdecnum="";

/* digits in hexadecimal number system */

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 Decimal Number : ");


decnum = scan.nextInt();

while(decnum>0)
{
rem = decnum%16;
hexdecnum = hex[rem] + hexdecnum;
decnum = decnum/16;
}

System.out.print("Equivalent Hexadecimal Value of " + decnum + " is :\n");


System.out.print(hexdecnum);
}
}

19. Decimal to Octal


public class DecToOctal {
public static void main(String args[])
{
int decnum, rem, quot, i=1, j;
int octnum[] = new int[100];
Scanner scan = new Scanner(System.in);

System.out.print("Enter any Decimal Number : ");


decnum = scan.nextInt();

quot = decnum;

while(quot != 0)
{
octnum[i++] = quot%8;
quot = quot/8;
}

System.out.print("Equivalent Octal Value of " + decnum + " is :\n");


for(j=i-1; j>0; j--)
{
System.out.print(octnum[j]);
}
}
}

20. Hexadecimal to Binary


public class HexToBinary {
public static int hex2decimal(String s)
{
String digits = "0123456789ABCDEF";
s = s.toUpperCase();
int val = 0;
for (int i = 0; i < s.length(); i++)
{
char c = s.charAt(i);
int d = digits.indexOf(c);
val = 16*val + d;
}
return val;
}
public static void main(String args[])
{
String hexdecnum;
int decnum, i=1, j;
int binnum[] = new int[100];
Scanner scan = new Scanner(System.in);

System.out.print("Enter Hexadecimal Number : ");


hexdecnum = scan.nextLine();

/* first convert the hexadecimal to decimal */


decnum = hex2decimal(hexdecnum);

/* now convert the decimal to binary */


while(decnum != 0)
{
binnum[i++] = decnum%2;
decnum = decnum/2;
}

System.out.print("Equivalent Binary Number is : ");


for(j=i-1; j>0; j--)
{
System.out.print(binnum[j]);
}
System.out.println();
}
}

21. Hexadecimal to Decimal


public class HexToDecimal {
public static int hex2decimal(String s)
{
String digits = "0123456789ABCDEF";
s = s.toUpperCase();
int val = 0;
for (int i = 0; i < s.length(); i++)
{
char c = s.charAt(i);
int d = digits.indexOf(c);
val = 16*val + d;
}
return val;
}
public static void main(String args[])
{
String hexdecnum;
int decnum;
Scanner scan = new Scanner(System.in);

System.out.print("Enter Hexadecimal Number : ");


hexdecnum = scan.nextLine();

decnum = hex2decimal(hexdecnum);

System.out.print("Equivalent Decimal Number is " + decnum);


}
}

22. Hexadecimal to Octal


public class HexToOctal {
public static int hex2decimal(String s)
{
String digits = "0123456789ABCDEF";
s = s.toUpperCase();
int val = 0;
for (int i = 0; i < s.length(); i++)
{
char c = s.charAt(i);
int d = digits.indexOf(c);
val = 16*val + d;
}
return val;
}
public static void main(String args[])
{
String hexdecnum;
int decnum, i=1, j;
int octnum[] = new int[100];
Scanner scan = new Scanner(System.in);

System.out.print("Enter Hexadecimal Number : ");


hexdecnum = scan.nextLine();

// first convert hexadecimal to decimal

decnum = hex2decimal(hexdecnum);

// now convert decimal to octal

while(decnum != 0)
{
octnum[i++] = decnum%8;
decnum = decnum/8;
}

System.out.print("Equivalent Octal Number is :\n");


for(j=i-1; j>0; j--)
{
System.out.print(octnum[j]);
}
}
}

23. Octal to Binary


public class OctalToBinary {
public static void main(String args[])
{
int octnum, rem, quot, i=1, j;
int binnum[] = new int[100];
Scanner scan = new Scanner(System.in);

System.out.print("Enter Octal Number : ");


octnum = scan.nextInt();

quot = octnum;

while(quot != 0)
{
binnum[i++] = quot%2;
quot = quot/2;
}

System.out.print("Equivalent Binary Value of " +octnum+ " is :\n");


for(j=i-1; j>0; j--)
{
System.out.print(binnum[j]);
}
}
}

24. Octal to Decimal


public class OctalToDec {
public static void main(String args[])
{
int octnum, decnum=0, i=0, orig;
Scanner scan = new Scanner(System.in);
System.out.print("Enter Octal Number : ");
octnum = scan.nextInt();

orig = octnum;

while(octnum != 0)
{
decnum = decnum + (octnum%10) * (int) Math.pow(8, i);
i++;
octnum = octnum/10;
}

System.out.print("Equivalent Decimal Value of " + orig + " is :\n");


System.out.print(decnum);
}
}

25. Octal to Hex


public class OctalToHex {
public static void main(String args[])
{
String octnum, hexnum;
int decnum;
Scanner scan = new Scanner(System.in);

System.out.print("Enter Octal Number : ");


octnum = scan.nextLine();

decnum = Integer.parseInt(octnum, 8);


hexnum = Integer.toHexString(decnum);

System.out.print("Equivalent Hexadecimal Value of " + octnum + " is :\n");


System.out.print(hexnum);
}
}

26. Command Line Arguments


#include<stdio.h>

int main(int argc,char* argv[])


{
int counter;
printf("Program Name Is: %s",argv[0]);
if(argc==1)
printf("\nNo Extra Command Line Argument Passed Other Than Program Name");
if(argc>=2)
{
printf("\nNumber Of Arguments Passed: %d",argc);
printf("\n----Following Are The Command Line Arguments Passed----");
for(counter=0;counter<argc;counter++)
printf("\nargv[%d]: %s",counter,argv[counter]);
}
return 0;
}

/*** Output ***/


1. Without Argument
$ ./a.out
Program Name Is: ./a.out
No Extra Command Line Argument Passed Other Than Program Name
******************************************************************

2. Single Argument in double qoutes


$ ./a.out "First Second Third"
Program Name Is: ./a.out
Number Of Arguments Passed: 2
----Following Are The Command Line Arguments Passed----
argv[0]: ./a.out
argv[1]: First Second Third
******************************************************************

3. Single Argument in single qoutes


$ ./a.out 'First Second Third'
Program Name Is: ./a.out
Number Of Arguments Passed: 2
----Following Are The Command Line Arguments Passed----
argv[0]: ./a.out
argv[1]: First Second Third
******************************************************************

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
******************************************************************

You might also like