Shahed
Shahed
Q1
:Create a program that does the following
}
;double avg=sum/num
;System.out.println("The average of numbers are :"+avg)
}
Q3
Write a program that generates a random number between 1 and 100,
and asks the user to guess the number. The program should then indicate
whether the user's guess is too high, too low, or correct. The program
should continue to prompt the user to guess the number until they guess
.correctly
: Answer
;Scanner input =new Scanner(System.in)
int rand=(int)(Math.random()*100);System.out.println("The random number
;between 1 to 100 is:"+rand)
;boolean stillplaying=true
{while(stillplaying)
;System.out.println("Enter your guess number:")
;)(int guess=input.nextInt
{if(guess>rand)
;System.out.println("Your Guess is too large:")
}
{else if(guess<rand)
;System.out.println("Your Guess is too small:")
else}
;System.out.println("Your Guess is right:"+guess)
;stillplaying=false
}
Write a program that asks the user to enter a number, and then determines Q4
whether the number is even or odd. The program should display a message
.dialog indicating whether the number is even or odd
: Answer
;Scanner input =new Scanner(System.in)
}
Write a program that calculates the factorial of a number. The program Q5
should ask the user to enter a number, and then display the factorial of that
.number
: Answer
;Scanner input =new Scanner(System.in)
System.out.println("Enter the number you want to calculate the
;factorial :")
;int fact=1
;)(int num=input.nextInt
{for(int i=1;i<=num;i++)
;fact=fact*i
}
;System.out.println("The factorial number is :"+fact)
}
}
;)(int num=input.nextInt
{switch(num)
:case 2
:case 3
;System.out.println("two or three ")
;break
;case 4:System.out.println("Four");break
;default:System.out.println("Other value")
}
}
}
}}
Write a program to read a positive integer and to print true if it is prime Q8
otherwise false
;Answer: Scanner input =new Scanner(System.in)
;System.out.println("Enter your positive number:")
;)(int num=input.nextInt
;boolean x=true
{if(num<=0)
;x=false
}
else
{for(int i=2;i<=Math.sqrt(num);i++)
{if(num%i==0)
;x=false
;break
}
}
{if(x)
;System.out.println("it is prime number:")
}
else
;System.out.println("its not a prime number:")