Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

Artificial Intelligence and Data Science Department

OOPM/Odd Sem 2023-23/Experiment 2


Name: Ameya
Nikhil Kalgutkar
Kadam Grade :

Class/Roll No.:
D6ADA/26
&27

2 Programs on Basic programming constructs like branching and looping


using function And accepting input through keyboard

Aim: A) Printing Fibonacci Series up to the nth term taking the value of n
from the user.

Theory :
▪ Java provides different ways to get input from the user.
▪ In order to use the object of Scanner, we need to import java.util.Scanner package.
import java.util.Scanner;

▪ Then, we need to create an object of the Scanner class. We can use the object to take input
from the user.

▪ Syntax:
Scanner input = new Scanner(System.in);

1. nextInt() Reads a int value from the user


2. nextFloat() Reads a float value from the user
3. next() Reads a character value from the user
4. nextDouble() Reads a double value from the user
5. nextLine() Reads a String value from the user
Artificial Intelligence and Data Science Department

OOPM/Odd Sem 2023-23/Experiment 2


Program :

Import java.util.Scanner;

Class FibonacciSeries {
Public static void main(String[] arg ){
Scanner scanner = new Scanner(System.in);

System.out.print(“Enter the value of n:”);


Int n = scanner.nextInt();
Int a = 0, b = 1;
System.out.print(“Fibonacci Series up to “ + n + “ terms: “);
System.out.print(a + “ “ + b + “ “);

For(int I = 3; I <= n; i++){


Int c = a + b;
System.out.print(c + “ “);
A = b;
B = c;
}
}
}
Artificial Intelligence and Data Science Department

OOPM/Odd Sem 2023-23/Experiment 2

Output
Screenshots :

Conclusion: Upon running the program, the user is prompted to enter the
value of 'n', representing the number of terms in the Fibonacci series. The
program then generates and prints the series up to the nth term. Students
will observe the sequence's growth and understand how objectoriented
programming can be applied to solve real-world mathematical problems.
Artificial Intelligence and Data Science Department

OOPM/Odd Sem 2023-23/Experiment 2

Aim : B) WAP to reverse the four digit no.

Theory :
1. A Function in Java is a block of statements that is defined to perform a
specific task. A function is also called a reusable code because once it is
created, it can be used again and again.
In Java, there are two categories of functions available, and they are:
• Built-in Functions : These functions are predefined, and we can use them
any time we want in our Java program. For example pow(), sqrt(), min(), etc.
• User Defined Functions : These functions are defined or created by the
programmer for performing a specific task in a program.

2. The static keyword is used to construct methods that will exist regardless of
whether or not any instances of the class are generated. Any method that
uses the static keyword is referred to as a static method.

Syntax to declare the static method:

Access_modifier static void methodName()


{
// Method body.
}
Artificial Intelligence and Data Science Department

OOPM/Odd Sem 2023-23/Experiment 2

Program :
import java.util.Scanner;

class NumberReverse {
int number;
public NumberReverse(int num) {
number = num;
}
public int reverseNumber() {
int reversed = 0;
while (number != 0) {
int digit = number % 10;
reversed = reversed*10 +digit number /= 10;
}
return reversed;
}

public class ReverseNumberProgram {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a four-digit number: ");
int num = scanner.nextInt();
if (num < 1000 || num > 9999) {
System.out.println("Please enter a valid four-digit number.");
return;
}
NumberReverser reverser = new NumberReverser(num);
int reversedNumber = reverser.reverseNumber();
System.out.println("Original number: " + num);
System.out.println("Reversed number: " + reversedNumber);
}
}
Artificial Intelligence and Data Science Department

OOPM/Odd Sem 2023-23/Experiment 2


}
1.
Output Screenshots :

2. Output Screenshots :

Conclusion : In this experiment, we successfully implemented a Java


program to reverse four-digit numbers using object-oriented principles. We
learned about creating classes, constructors, methods, and utilizing objects
to achieve a specific task. The experiment helped us strengthen our
understanding of basic programming constructs like branching and looping
and their implementation in an object-oriented context. Overall, it was a
valuable learning experience.
Artificial Intelligence and Data Science Department

OOPM/Odd Sem 2023-23/Experiment 2

Aim: C) WAP to read the numbers & shift left & right by 3 bits

Theory:
The right shift operator shifts the bits towards the right by the specified number of times. It is denoted
by the symbol >>.

When a bit is shifted towards the right, the rightmost bit (least significant bit) is discarded and the
leftmost bit (most significant) bit is filled with the sign bit (i.e.) when the number is positive, 0 is used to
fill the leftmost position, and when the number is negative, the sign bit is used to fill the leftmost
position.

➢ The syntax of the signed right shift operator in Java is:

❖ Operand >> number


• Operand – The values whose bits will be shifted towards the right
• Number – The number of bits to be shifted
Artificial Intelligence and Data Science Department

OOPM/Odd Sem 2023-23/Experiment 2

Program:
Import java.util.Scanner;

Public class RightBitShift {


Public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

System.out.print(“Enter a number: “);


Int number = scanner.nextInt();

Int shiftedRight = number >> 3;

System.out.println(“Original number: “ + number);


System.out.println(“Number shifted right by 3 bits: “ + shiftedRight);
}
}

Output:

Conclusion: The right shift operator in Java moves the bits of a value towards the right by the specified
number of bits. In the signed right shift, the rightmost bit is discarded, and the leftmost bit is filled with
the sign bit.

You might also like