0% found this document useful (0 votes)
2 views4 pages

Java Lab 1

Uploaded by

Suravi Nayak
Copyright
© All Rights Reserved
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% found this document useful (0 votes)
2 views4 pages

Java Lab 1

Uploaded by

Suravi Nayak
Copyright
© All Rights Reserved
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

[Link].

ITM

Paper : java programming

Lab -1

1. To find the sum of any number of integers entered as command line arguments.
import [Link];
class SumTest {
public static void main(String[] values) {
int sum = 0;
[Link]("Calculates Sum for below Integers");
for(int i=0;i<[Link];i++){
[Link](values[i]);
sum = sum + [Link](values[i]);
}
[Link]("Sum :" + sum);
}
}

Output :
D:\Java_Programs>javac [Link]
D:\Java_Programs>java SumTest 10 20 30 40 50
Calculates Sum for below Integers
10
20
30
40
50
Sum :150
2. To find the factorial of a given number.
import [Link];
public class Factorial {
public static void main(String[] args) {
Scanner input = new Scanner([Link]);

[Link]("Enter a number: ");


int num = [Link]();

int fact = 1;
for (int i = 1; i <= num; i++) {
fact *= i;
}

[Link]("Factorial of " + num + " is: " + fact);


}
}

Out put:
Enter a number: 5
Factorial of 5 is: 120

3. To convert a decimal to binary number. simple java code


(with using library function [Link]())
import [Link];

public class DecimalToBinary {


public static void main(String[] args) {
Scanner input = new Scanner([Link]);

[Link]("Enter a decimal number: ");


int decimal = [Link]();

String binary = [Link](decimal);

[Link]("Binary equivalent: " + binary);

}
}

Output :
Enter a decimal number: 10
Binary equivalent: 1010

(Without library function)


import [Link];
public class DecimalToBinaryManual {
public static void main(String[] args) {
Scanner input = new Scanner([Link]);

[Link]("Enter a decimal number: ");


int decimal = [Link]();

String binary = "";


int num = decimal;

if (num == 0) {
binary = "0";
} else {
while (num > 0) {
int remainder = num % 2;
binary = remainder + binary;
num = num / 2;
}
}
[Link]("Binary equivalent of " + decimal + " is: " + binary);
}
}

4. To check if a number is prime or not, by taking the number as input from the keyboard.

import [Link];
public class PrimeCheck {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);

[Link]("Enter a number: ");


int num = [Link]();
boolean isPrime = true;

if (num <= 1) {
isPrime = false;
} else {
for (int i = 2; i <= num / 2; i++) {
if (num % i == 0) {
isPrime = false;
break;
}
}
}
if (isPrime)
[Link](num + " is a prime number.");
else
[Link](num + " is not a prime number.");

[Link]();
}
}

Output :

Enter a number: 7
7 is a prime number.

5. To find the sum of any number of integers interactively, i.e., entering every
number from the keyboard, whereas the total number of integers is given as a
command line argument.
import [Link];
public class SumOfIntegers {
public static void main(String[] args) {
if ([Link] == 0) {
[Link]("Please provide the number of integers as a command-line
argument.");
return;
}

int count = [Link](args[0]);


Scanner sc = new Scanner([Link]);
int sum = 0;

for (int i = 1; i <= count; i++) {


[Link]("Enter number " + i + ": ");
int num = [Link]();
sum += num;
}

[Link]("Sum of entered numbers: " + sum);

}
}

Output :
javac [Link]

java SumOfIntegers 3

Enter number 1: 10
Enter number 2: 5
Enter number 3: 20
Sum of entered numbers: 35

You might also like