Questions-
1. Finding factorial using for loop.
import java.util.Scanner;
public class my
{
public static void main(String args[])
{
int n,i,fact=1;
Scanner s= new Scanner(System.in);
System.out.println("Enter the no. to get the factorial");
n= s.nextInt();
if(n==0)
{
System.out.println("the factorial will be 1");
}
else
{
for(i=1;i<=n;i++)
{
fact= fact*i;
}
System.out.println("the factorial is"+fact);
}
}
}
2. wap to find given element in array
using scanner object.
import java.util.Scanner;
public class my
{
public static void main(String args[])
{
int i,st;
int[] a= {10,20,30,40,50};
Scanner s= new Scanner(System.in);
System.out.println("enter a no. to see is matched or not");
st= s.nextInt();
for(i=0;i<a.length;i++)
{
if(a[i]==st)
{
System.out.println("the element"+ " "+(i+1)+ " "+"is matched");
break;
}
else
{
System.out.println("the element" + " "+(i+1)+" "+"is not matched");
}
}
}
}
3. wap to Program to generate 5 random numbers like
365
215
7748
259
78
import java.util.Scanner;
public class my
{
public static void main(String args[])
{
int rand1,rand2,rand3,rand4,rand5;
Scanner s= new Scanner(System.in);
rand1 = (int)(Math.random()*101);
rand2 = (int)(Math.random()*102);
rand3 = (int)(Math.random()*103);
rand4 = (int)(Math.random()*104);
rand5 = (int)(Math.random()*145);
System.out.println("your random number is"+ rand1);
System.out.println("your random number is"+ rand2);
System.out.println("your random number is"+ rand3);
System.out.println("your random number is"+ rand4);
System.out.println("your random number is"+ rand5);
}
}
4. wap to find the greatest number in Array.
import java.util.Scanner;
public class my
{
public static void main(String args[])
{
int i,maxi,temp;
int[] a= new int[5];
Scanner s= new Scanner(System.in);
System.out.println("enter the elements");
for(i=0;i<a.length;i++)
{
a[i] = s.nextInt();
}
maxi=0;
for(i=0;i<a.length;i++)
{
if( a[maxi]<a[i])
{
temp=a[maxi];
a[maxi]=a[i];
a[i]=temp;
}
}
System.out.println("the greatest number is"+a[maxi]);
}
}
5. Program to find the average of numbers using array initilization methods
public class my
{
public static void main(String args[]){
int i,avg,sum=0;
int[] a={120,70,300,90,10};
Scanner s= new Scanner(System.in);
for(i=0;i<a.length;i++)
{
sum=sum+a[i];
}
avg=sum/a.length;
System.out.println("the average is"+avg);
}
}
6 . Program to check whether input number is prime or not
import java.util.Scanner;
public class my
{
public static void main(String args[])
{
int i, n;
Scanner s1=new Scanner(System.in);
n=s1.nextInt();
int Prime=1;
for(i=2;i<=n/2;i++)
{
if(n%i==0)
{
Prime=0;
break;
}
}
if(Prime==1 && n>1)
{
System.out.println("the no. is prime");
}
else
{
System.out.println("composite number");
}
}
}
7 : Finding largest of three numbers using if-else..if
import java.util.Scanner;
public class my
{
public static void main (String args [])
{
int var1,var2,var3;
Scanner s1= new Scanner(System.in);
System.out.println("Enter three numbers");
var1= s1.nextInt();
var2= s1.nextInt();
var3= s1.nextInt();
if(var1>var2 && var1>var3)
{
System.out.println("the value" +var1+ "is the greatest");
}
else if(var2>var1 && var2>var3)
{
System.out.println("the value" +var2+ "is the greatest");
}
else if(var3>var1 && var3>var2)
{
System.out.println("the value" +var3+ "is the greatest");
}
else
{
System.out.println("can not enter two same numbers ");
}
}
}
Comments
Post a Comment