Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
26 views

Java class set 3

Java

Uploaded by

Jeromy R
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)
26 views

Java class set 3

Java

Uploaded by

Jeromy R
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

Program - Simple Calculator using switch case

Source code:
import java.util.Scanner;
public class CalculatorUsingSwitch {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter value of 1st number ::");
int a = sc.nextInt();
System.out.println("Enter value of 2nd number ::");
int b = sc.nextInt();
System.out.println("Select operation");
System.out.println("Addition-a: Subtraction-s: Multiplication-m: Division-d: ");
char ch = sc.next().charAt(0);
switch(ch) {
case 'a' :
System.out.println("Sum of the given two numbers: "+(a+b));
break;
case 's' :
System.out.println("Difference between the two numbers: "+(a-b));
break;
case 'm' :
System.out.println("Product of the two numbers: "+(a*b));
case 'd' :
System.out.println("Result of the division: "+(a/b));
break;
default :
System.out.println("Invalid operation");
}
}
}
Output:
Program - Checking for Armstrong number using while loop
Source code:
Import java.util.*;
Class Armstrong{
Public static void main(string args[]){
Scanner sc= new Scanner(System.in);
Int n,sum,r,temp;
N=sc.nextInt();
System.out.println(“enter the value:”);
temp=n;
while(n>0)
{
r=n%10;
sum=sum+(r*r*r);
n=n/10;
}
if(sum==temp)
System.out.println(“given number is Armstrong number”);
else
System.out.println(“not a Armstrong number”);
}
}

Output:
Program - Reversing a number and finding sum using do… while
Source code:
import java.util.Scanner;
class reverseandsum {
public static void main(String[] args{
int num, rem;
int rev = 0, sum = 0;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number: ");
num = sc.nextInt();
do {
rem = num % 10;
rev = rev * 10 + rem;
sum = sum + rem;
num = num / 10;
}while (num > 0)
System.out.println("Reverse of given number: "+ rev);
System.out.print("Sum of digits of given number: "+ sum);
}
}
Output:
Program - Finding Fibonacci series using for loop
Source code:
import java.util.*;
class FibonacciExample1{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int n1=0,n2=1,n3,i,count;
System.out.println("Enter the number:");
count=sc.nextInt();
System.out.print(n1+" "+n2+" ");
for(i=2;i<count;++i)
{
n3=n1+n2;
System.out.print(" "+n3);
n1=n2;
n2=n3;
}
}
}
Output:

You might also like