0% found this document useful (0 votes)
75 views

Java Lab Experiment-2: Platform Used: Eclipse IDE

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)
75 views

Java Lab Experiment-2: Platform Used: Eclipse IDE

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
You are on page 1/ 5

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 java.util.Scanner;

public class FibonacciSeries {

public static void main(String[] args) {


Scanner sc= new Scanner(System.in);
System.out.println("Enter the value of n");
int n=sc.nextInt();
int a=0;
int b=1;
System.out.print(a+" ");
System.out.print(b+" ");
for(int i=0;i<=n-2;i++) {
int c=a+b;
System.out.print(c+" ");
a=b;
b=c;
}

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

import java.util.Scanner;

public class WhetheraCharacterisAlphabetorNot {

public static void main(String[] args) {


Scanner s = new Scanner(System.in);
System.out.println(" Enter the character");
String str = s.nextLine();
char ch= str.charAt(0);

if( (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))
System.out.println(ch + " is an alphabet.");
else
System.out.println(ch + " is not an alphabet.");

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

import java.util.Scanner;

public class DigitCounterofANumber {

public static void main(String[] args) {


Scanner sc=new Scanner(System.in);
System.out.println(" Enter the No. ");
int n=sc.nextInt();
int count=0;

while (n!= 0) {

n/= 10;
++count;
}

System.out.println("Number of digits: " + count);

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

import java.util.Scanner;

public class CalCulator {

public static void main(String[] args) {


Scanner sc= new Scanner(System.in);
System.out.println("Enter the first No.");
int a=sc.nextInt();
System.out.println("Enter the second No.");
int b=sc.nextInt();
System.out.println("Enter the Operation");
sc.nextLine();
char operation=sc.nextLine().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:
System.out.println("Invalid Operation");

}
System.out.println("Result is "+result);

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

import java.util.Scanner;

public class Pattern3 {

public static void main(String[] args) {


Scanner sc=new Scanner(System.in);
System.out.println("Enter the No.");
int n=sc.nextInt();
int i=1;
while(i<=n) {
int s=1;
while(s<=n-i) {
System.out.print(' ');
s+=1;
}
int j=1;
while(j<=2*i-1){
System.out.print("*");
j+=1;
}
i+=1;
System.out.println();
}

}
Output:

You might also like