0% found this document useful (0 votes)
38 views5 pages

Shahed

This document contains 6 programming questions. The first question asks to create a program that gets a user's name and age and calculates what year they will turn 100 years old. The second question asks to create a program that gets multiple numbers from the user and calculates the average. The third question asks to generate a random number and have the user guess it until they guess correctly. The fourth question asks to determine if a number entered is even or odd. The fifth question asks to calculate the factorial of a number. The sixth question asks to rewrite an if/else statement as a switch statement.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views5 pages

Shahed

This document contains 6 programming questions. The first question asks to create a program that gets a user's name and age and calculates what year they will turn 100 years old. The second question asks to create a program that gets multiple numbers from the user and calculates the average. The third question asks to generate a random number and have the user guess it until they guess correctly. The fourth question asks to determine if a number entered is even or odd. The fifth question asks to calculate the factorial of a number. The sixth question asks to rewrite an if/else statement as a switch statement.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Computer Programming 1: Assignment1

Q1
:Create a program that does the following

.Ask the user to input their name •


.Print a greeting to the user with their name •
.Ask the user to input their age •
.Calculate the year in which the user will turn 100 years old •
Print a message to the user stating what year they will turn 100 •
.years old

Here is an example of what the program should output

?What is your name


Ali
!Hello, Ali
?What is your age
25
.You will turn 100 years old in 2096
: Answer
;Scanner input =new Scanner(System.in)
;System.out.println("Please Enter your Name:")
; String str
;)(str=input.next
;System.out.println("Hello "+str)
;System.out .println("Enter your Age:")
;)(int age=input.nextInt
;System.out.println("your age:" +age)
;)(int currentYear=java.time.Year.now().getValue
;int year100=currentYear+(100-age)
;System.out.println("You will turn 100 years :"+year100)

.Create a program to calculate the average of a list of numbers Q2


:Requirements
The program should prompt the user to enter the number of values they •
.want to calculate the average for
The program should then prompt the user to enter each value, one at a •
.time
After all values have been entered, the program should calculate the •
.average and display it to the user
:Here is an example of what the program should output
Enter the number of values to calculate the average for: 5
Enter value 1: 10
Enter value 2: 20
Enter value 3: 30
Enter value 4: 40
Enter value 5: 50
The average is: 30.0
: Answer
;Scanner input =new Scanner(System.in)
;System.out.println("ENter the number you want to do avarge for it:")
;)(int num=input.nextInt
;int i
;float sum=0
{for( i=1;i<=num;i++)
; System.out.println("Enter value "+i)
;)(int n=input.nextInt
;sum=sum+n

}
;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)

;String x=JOptionPane.showInputDialog( "Enter your number")


;int y=Integer .parseInt(x)
{if(y%2==0)
;JOptionPane.showMessageDialog(null,"Even")
}
else
;JOptionPane.showMessageDialog(null,"odd")

}
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)
}
}

.Rewrite the following if-else statement using switch statement


:Convert the following into one switch statement
if (x== 2 || x == 3)
;System.out.println("Two or Three") Q6
else if (x == 4)
;System.out.println("Four")
;else System.out.println("other value")
: Answer

;Scanner input =new Scanner(System.in)


;System.out.println("Enter your number:")

;)(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")
}

}
}

Rewrite the following decision statement using the conditional operator Q7


.):?(
if(number %2 == 0)
;System.out.println(“This is an even number.”)
else
;System.out.println(“This is an odd number.”)
:Answer
;Scanner input =new Scanner(System.in)
;System.out.println("Enter your number:")
;)(int num=input.nextInt

;System.out.println((num%2==0)?+num+" is even":+num+" is odd")

}}
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:")

You might also like