0% found this document useful (0 votes)
170 views5 pages

Java Lab Experiments in Eclipse

This document contains the code submissions and outputs for 5 Java programs by a student. The programs include: 1) A Fibonacci series program 2) A program to check if a character is an alphabet 3) A program to count the digits in an integer 4) A basic calculator program using switch-case statements 5) A program to create a pyramid pattern
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)
170 views5 pages

Java Lab Experiments in Eclipse

This document contains the code submissions and outputs for 5 Java programs by a student. The programs include: 1) A Fibonacci series program 2) A program to check if a character is an alphabet 3) A program to count the digits in an integer 4) A basic calculator program using switch-case statements 5) A program to create a pyramid pattern
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

Java Lab

Experiment-2
Submitted-by-Aakash Soni(2K19/EC/004)
Platform Used: Eclipse IDE

Program 1: Java Program to Find Factorial of a Number.


Sol.
Code:
package loops;

import [Link];

public class FibonacciSeries {

public static void main(String[] args) {


Scanner sc= new Scanner([Link]);
[Link]("Enter the value of n");
int n=[Link]();
int a=0;
int b=1;
[Link](a+" ");
[Link](b+" ");
for(int i=0;i<=n-2;i++) {
int c=a+b;
[Link](c+" ");
a=b;
b=c;
}

Output:
Program 2: Java Program to Check Whether a Character is Alphabet or Not
Sol.
Code:
package practiseProblems;

import [Link];

public class WhetheraCharacterisAlphabetorNot {

public static void main(String[] args) {


Scanner s = new Scanner([Link]);
[Link](" Enter the character");
String str = [Link]();
char ch= [Link](0);

if( (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))
[Link](ch + " is an alphabet.");
else
[Link](ch + " is not an alphabet.");

Output:
Program 3: Java Program to Count Number of Digits in an Integer.
Sol.
Code:
package practiseProblems;

import [Link];

public class DigitCounterofANumber {

public static void main(String[] args) {


Scanner sc=new Scanner([Link]);
[Link](" Enter the No. ");
int n=[Link]();
int count=0;

while (n!= 0) {

n/= 10;
++count;
}

[Link]("Number of digits: " + count);

Output:
Program 4: Java Program to Make a Simple Calculator Using switch...case
Sol.
Code:
package nestedElse;

import [Link];

public class CalCulator {

public static void main(String[] args) {


Scanner sc= new Scanner([Link]);
[Link]("Enter the first No.");
int a=[Link]();
[Link]("Enter the second No.");
int b=[Link]();
[Link]("Enter the Operation");
[Link]();
char operation=[Link]().charAt(0);
int result = 0;
switch(operation) {
case '+':
result=a+b;
break;
case'-':
result=a-b;
break;
case'*':
result=a*b;
break;
case'/':
result=a/b;
break;
default:
[Link]("Invalid Operation");

}
[Link]("Result is "+result);

}
Output:
Program 5: Java Code To Create Pyramid Pattern
Sol.
Code:
package practiseProblems;

import [Link];

public class Pattern3 {

public static void main(String[] args) {


Scanner sc=new Scanner([Link]);
[Link]("Enter the No.");
int n=[Link]();
int i=1;
while(i<=n) {
int s=1;
while(s<=n-i) {
[Link](' ');
s+=1;
}
int j=1;
while(j<=2*i-1){
[Link]("*");
j+=1;
}
i+=1;
[Link]();
}

}
Output:

Common questions

Powered by AI

The Java program creates a pyramid pattern using nested loops to control the spacing and number of asterisks per row . The outer 'while' loop manages the number of pyramid levels as defined by 'n', where 'i' represents the current pyramid level from 1 to 'n'. The first inner 'while' loop introduces spaces, this loop iterates 'n-i' times each level to format the left-aligned edges and center the pyramid shape. The second inner 'while' loop then prints asterisks ('*') for each level, generating '2*i-1' asterisks at each level, incrementing with each iteration of 'i'. Hence, the nested loops collaboratively render the rows of the pyramid, with the total structure reflecting a symmetric increase of space and asterisk formation .

The Java calculator program uses a switch-case structure to determine and perform the desired arithmetic operation based on user input . Once the user inputs two operands and an operation character (e.g., '+', '-', '*', '/'), the program evaluates the operation character within the switch statement. Each case corresponds to a particular arithmetic operation: addition, subtraction, multiplication, or division. Depending on the input character, the control transfers to the associated case block, where the specific operation is computed on the two input numbers, resulting in the output being stored in a variable 'result'. If an invalid operation character is input, a default case handles it, printing an error message for unsupported operations, thereby ensuring robust handling of inputs .

The Java program identifies whether a character is an alphabet using conditional logic involving character range checks . It prompts the user to enter a character, then extracts the first character of the input string. The program performs a check to see if the character falls within the ranges of 'a' to 'z' or 'A' to 'Z'. These ranges check whether the character is a lowercase or uppercase alphabet, respectively. If the character satisfies either condition, it is deemed an alphabet; otherwise, it is categorized as a non-alphabet character .

In the Java program for generating the Fibonacci series, a loop-based structure is employed . The program first initializes two integer variables, a and b, to 0 and 1 respectively, which represent the first two numbers in the Fibonacci sequence. Using a for-loop, it iterates from 0 to n-2, where 'n' is the number of terms desired, as input by the user. Within the loop, a new Fibonacci number 'c' is computed as the sum of the preceding two numbers, 'a' and 'b'. After printing 'c', the values of 'a' and 'b' are updated to 'b' and 'c', respectively, for use in the next iteration. This process continues, printing each subsequent Fibonacci number, until the loop completes, thus generating the Fibonacci sequence up to 'n' terms .

The Java program counts the number of digits in an integer by repeatedly dividing the number by 10 until the integer becomes zero . This approach operates on the principle that dividing an integer by 10 truncates its last digit, and hence, reduces the number by one digit. A counter variable keeps track of the number of times the division operation is performed. This method is effective because it works universally for any positive integer, utilizing simple arithmetic operations and loop constructs to accurately count and print the total number of digits present in the given integer .

You might also like