Name Prathamesh Mane
UID no. & Branch 2022200078 (B1)
Experiment No. 1
AIM: Write a Java Program for I/O operations using command line arguments/
Scanner class.
Program 1
PROBLEM Write a program to take name as input and write a ‘Hello’ to the name
STATEMENT: inputted , in addition to it input a number and for that number calculate the
factorial (number ‘n’).
PROGRAM: import java.util.Scanner;
class HelloWorld{
public static void main(String []args){
HelloWorld pratham = new HelloWorld(); //creating an instance of class
HelloWorld we have to do this because factorial is not static method
System.out.println("===========================");
System.out.println("Enter your name:");
Scanner scan=new Scanner(System.in);
String name=scan.nextLine();
System.out.println("Hello,"+name+"!");
System.out.println("===========================");
System.out.println("Enter a number:");
int n=scan.nextInt();
pratham.factorial(n);
}
public int factorial ( int a) {
int fac =1 ;
for(int i=1;i<=a;i++)
{
fac=fac*(i);
}
System.out.println("factorial of "+a+" is "+fac+"");
return fac;
}
}
RESULT:
Program 2
PROBLEM Write a program for printing fibonacci series in java programming language
STATEMENT:
PROGRAM: import java.util.Scanner;
class HelloWorld{
public static void main(String []args){
Scanner scan=new Scanner(System.in);
System.out.println("===========================");
System.out.println("Enter a number:");
int n = scan.nextInt();
fib(n);
//method for calculating the fibonaci series
public static void fib ( int a) {
int fib=0;
int c=1,b=1;
System.out.println("The value of element 1 is = 1");
System.out.println("The value of element 2 is = 1");
for(int i=3;i<=a;i++)
{
fib=c+b;
c=b;
b=fib;
System.out.println("The value of element "+i+" is = "+fib+"");
}
}
}
RESULT:
Program 3
PROBLEM A Mersenne prime is a prime number that has the form 2^p−1 where p is a
STATEMENT: positive number greater than 1. Write a program that calculates candidate
Mersenne primes 2^p − 1 for 2≤p≤31. Then test the number to see if it is
prime. If you detect that the number is prime, print out the number and the
value of p.
PROGRAM: import java.util.Scanner;
class Prathamesh {
public static void main(String[] args) {
for (int p = 2; p <= 31; p++) {
int mersenne = power(2,p) - 1;
if (isPrime(mersenne)) {
System.out.println("Mersenne prime found: value of p is " + p + "
(2^p - 1) = " + mersenne);
}
}
}
//method to calculate the power of mersenne prime number
public static int power(int base, int exponent) {
int result = 1;
for (int i = 1; i <= exponent; i++) {
result =result * base;
}
return result;
}
//method to check wether the number is prime or not
public static boolean isPrime(int number) {
if (number < 2) {
return false;
}
for (int i = 2; i <= number / 2; i++) {
if (number % i == 0) {
return false;
}
}
return true;
}
}
RESULT:
Program 4
PROBLEM Write a program to print all Armstrong numbers and Prime Numbers in the
STATEMENT: range inputted by the user. Also print the total count of Prime Numbers in
the given range.
PROGRAM: import java.util.Scanner;
class Prathamesh {
public static void main(String[] args) {
// Making an instance 'mane' of class Prathamesh
Prathamesh mane = new Prathamesh();
Scanner scan = new Scanner(System.in);
System.out.println("===========================");
System.out.println("Enter the range of numbers:");
int n = scan.nextInt();
mane.printArm(n);
int pCount = mane.printPrime(n);
System.out.println("===========================");
System.out.println("Total count of prime numbers in the given range: "
+ pCount);
}
// Method to print all Armstrong numbers in the given range
public void printArm(int range) {
System.out.println("Armstrong numbers in the range 1 to " + range +
":");
for (int i = 1; i <= range; i++) {
if (arm(i)) {
System.out.println(i);
}
}
}
// Method to check if a number is an Armstrong number
public boolean arm(int number) {
int temp = number, sum = 0;
int digits = countDigits(number); // Count the number of digits
// Calculate the sum of the power of its digits
while (temp > 0) {
int r = temp % 10;
sum =sum+ power(r, digits);
temp = temp / 10;
}
return sum == number;
}
// Method to count the number of digits in a number
public int countDigits(int number) {
int count = 0;
while (number > 0) {
count++;
number = number/ 10;
}
return count;
}
// Method to calculate the power of a number
public int power(int base, int exp) {
int result = 1;
for (int i = 1; i <= exp; i++) {
result =result* base;
}
return result;
}
// Method to print all prime numbers in the given range and return the
count
public int printPrime(int range) {
int count = 0;
System.out.println("Prime numbers in the range 1 to " + range + ":");
for (int i = 2; i <= range; i++) {
if (isPrime(i)) {
System.out.println(i);
count++;
}
}
return count;
}
// Method to check if a number is prime
public boolean isPrime(int number) {
if (number < 2) {
return false;
}
// Check divisibility from 2 up to number/2
for (int i = 2; i <= number / 2; i++) {
if (number % i == 0) {
return false;
}
}
return true;
}
}
RESULT:
0
CONCLUSION: In our recent exploration of Java programming, we covered the
fundamentals of input and output operations. We learned how to display
messages on the console and how to capture user input using the `Scanner`
class. Additionally, we learnt the concepts of classes and objects. An
important observation we made was regarding the use of methods.
Specifically, we noted that when a method is not declared as ‘static’, it
cannot be called directly from another method. Instead, an instance of the
class must be created, and the non-static method is then invoked using this
instance. This distinction between static and non-static methods is crucial
for understanding how to effectively structure and utilize classes in Java.