Example : A Java program to show the table values of given data.
import java.io.*;
import java.util.Scanner;
class Table
{
public static void main(String args[])
{
int i=1;
int count;
int table;
System.out.print("Enter the table value = ");
Scanner sc=new Scanner(System.in);
count=sc.nextInt();
System.out.println("The tables are");
while(i<=10)
{
table=count*i;
System.out.println(count+" * "+i+" = "+ table);
i++;
}
}
}
------------- OR -------------
import java.io.*;
import java.util.Scanner;
class Table
{
public static void main(String args[])
{
int count;
int table;
System.out.print("Enter the table value = ");
Scanner sc=new Scanner(System.in);
count=sc.nextInt();
System.out.println("The tables are");
for(int i=1;i<10;i++)
{
table=count*i;
System.out.println(count+" * "+i+" = "+ table);
}
}
}
Example : A Java program to show sum of up to given value using loop.
import java.io.*;
import java.util.Scanner;
class Sum
{
int n,sum=0,i;
Scanner sc=new Scanner(System.in);
void input()
{
System.out.println("Enter the value for addition");
n=sc.nextInt();
for(i=1;i<=n;i++)
{
sum=sum+i;
}
System.out.println("Sum of " +n+" is ="+" = "+sum);
}
public static void main(String[] args)
{
Sum z=new Sum();
z.input();
}
}
Example : A Java program to show factorial result of given value.
import java.io.*;
import java.util.Scanner;
class Simple
{
public static void main(String arg[])
{
int num1,fact=1;
Scanner sc= new Scanner(System.in);
System.out.println("Enter an integer value");
num1=sc.nextInt();
for(int i=1;i<=num1;i++)
{
fact=fact*i;
}
System.out.println("Factoral Results are = "+fact);
}
}
Example : A Java program to show factorial result of given value using user defined function.
import java.io.*;
import java.util.Scanner;
class Simple
{
public static void main(String args[])
{
long num,factresult;
Scanner sc= new Scanner(System.in);
System.out.println("Enter an Integer Value");
num=sc.nextInt();
//num=Long.parseLong(args[0]);
factresult=factorial(num);
System.out.println("fact="+factresult);
}
static long factorial(long n)
{
long fact=1;
for(int i=1;i<=n;i++)
{
fact=fact*i;
}
return fact;
}
//Recursion Method
/* static long factorial(long n)
{
if(n<=0)
return 1;
else
return factorial(n-1)*n;
} */
}
Example : A Java program to make calculator using switch case statement.
import java.io.*;
import java.util.Scanner;
class Simple
{
public static void main(String[] args)
{
int num1, num2;
Scanner Sc = new Scanner(System.in);
System.out.print("Enter first number:");
num1 = Sc.nextInt();
System.out.print("Enter second number:");
num2 = Sc.nextInt();
System.out.print("Enter any Symbol +, -, *, / : ");
char symbol = Sc.next().charAt(0);
int result;
switch(symbol)
{
case '+':
result = num1 + num2;
break;
case '-':
result = num1 - num2;
break;
case '*':
result = num1 * num2;
break;
case '/':
result = num1 / num2;
break;
default:
System.out.printf("Entered choice is incorrect, Change it");
return;
}
System.out.println("The result of "+num1+" "+symbol+" "+num2+ " = : "+result);
}
}
0 Comments