The document contains a series of Java programming exercises, including swapping two variables without a third variable, adding binary numbers, converting decimal numbers to binary and hexadecimal, and computing the sum of an integer's digits. Each program is presented with its code and expected output. The exercises aim to enhance Java programming skills through practical applications.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
3 views
Lecture no 3(OOP)
The document contains a series of Java programming exercises, including swapping two variables without a third variable, adding binary numbers, converting decimal numbers to binary and hexadecimal, and computing the sum of an integer's digits. Each program is presented with its code and expected output. The exercises aim to enhance Java programming skills through practical applications.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16
By Samra Tariq (Gold medalist), lecturer at NUML
Java Practice Programs
P A T C R C I E
P R O G R A M S Program no 12
Write a Java program to swap two
variables without using third variable. Program 12 Output
public class Swap {
public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("Input first number: "); int a = in.nextInt(); System.out.print("Input second number: "); int b = in.nextInt(); System.out.println("Before swapping : a, b = "+a+", "+ b); a = a + b; b = a – b; a = a – b; System.out.println("After swapping : a, b = "+a+", " + b); } } Program no 13
Write a Java program to add two binary
numbers. Binary Addition Program 13 Output public class Swap { public static void main(String[] args) { long binary1, binary2; int i = 0, remainder = 0; int[] sum = new int[20]; Scanner in = new Scanner(System.in); System.out.print("Input first binary number: "); binary1 = in.nextLong(); System.out.print("Input second binary number: "); binary2 = in.nextLong(); while (binary1 != 0 || binary2 != 0) { sum[i++] = (int)((binary1 % 10 + binary2 % 10 + remainder) % 2); remainder = (int)((binary1 % 10 + binary2 % 10 + remainder) / 2); binary1 = binary1 / 10; binary2 = binary2 / 10;} if (remainder != 0) { sum[i++] = remainder;} --i; System.out.print("Sum of two binary numbers: "); while (i >= 0) { System.out.print(sum[i--]); } System.out.print("\n"); } } Program no 14
Write a Java program to convert an integer
number to a binary number. Decimal to Binary Conversion Program 14 Output public class Swap { public static void main(String[] args) { int dec_num, quot, i=1, j; int bin_num[] = new int[100]; Scanner scan = new Scanner(System.in); System.out.print("Input a Decimal Number : "); dec_num = scan.nextInt(); quot = dec_num; while(quot != 0) { bin_num[i++] = quot%2; quot = quot/2; } System.out.print("Binary number is: "); for(j=i-1; j>0; j--) { System.out.print(bin_num[j]); } System.out.print("\n"); } } Program no 15
Write a Java program to convert an integer
number to a hexadecimal number. Decimal to Hexadecimal Conversion Program 15 Output public class Hexadecimal { public static void main(String[] args) { int dec_num, rem; String hexdec_num=""; /* hexadecimal number digits */ char hex[]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'}; Scanner in = new Scanner(System.in);
System.out.print("Input a decimal number: ");
dec_num = in.nextInt(); while(dec_num>0) { rem = dec_num%16; hexdec_num = hex[rem] + hexdec_num; dec_num = dec_num/16; } System.out.print("Hexadecimal number is : "+hexdec_num+"\n");} } Program no 16
Write a Java program and compute the sum
of an integer's digits. Digit’s Summation Program 16 Output public class Hexadecimal { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Input an integer: "); long n = input.nextLong(); System.out.println("The sum of the digits is: " + sumDigits(n));
public static int sumDigits(long n) {
int sum = 0; while (n != 0) { sum += n % 10; n /= 10; } return sum; } }