Write a program which will display a message:
1."Hi",if a particular entered value is equal to 1.
2."Hello",if a particular entered value is equal to 2.
3."No match found",if none of the conditions are matching
Answers
Answer:
import Java. until.*;
public class Display
{
public static void main ()
{
Scanner sc = me new Scanner (system.in)
int n ;
System.out.println ("ENTER YOUR choose");
n= sc.nextInt
Switch (n)
{
case1:
System.out.println ("Hi");
break ;
case 2
System.out.println ("Hello ");
break ;
default:
System.out.println ("no match found");
}
}
}
Answer:
This is the required program for the question. See the attachment for output.
1. In Java.
import java.util.*;
public class Java {
public static void main(String[] args) {
int n;
Scanner sc=new Scanner(System.in);
System.out.print("Enter a number: ");
n=sc.nextInt();
if(n==1)
System.out.print("Hi.");
else if(n==2)
System.out.print("Hello.");
else
System.out.print("Invalid Number.");
sc.close();
}
}
2. In Python.
n=int(input("Enter a number: "))
if n==1:
print("Hi.")
elif n==2:
print("Hello.")
else:
print("Invalid Number.")
3. In C.
#include <stdio.h>
void main() {
int n;
printf("Enter a number: ");
scanf("%d", &n);
if(n==1)
printf("Hi.");
else if(n==2)
printf("Hello.");
else
printf("Invalid Number.");
}
4. In C++
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter a number: ";
cin >> n;
if(n==1)
cout << "Hi.";
else if(n==2)
cout << "Hello.";
else
cout << "Invalid Number.";
return 0;
}
Steps To Solve:
- Ask the user to enter a number.
- If the number is 1, display "Hi".
- If the number is 2, display "Hello".
- If the number is neither 1 nor 2, display the message that says - "Invalid Input".
See the attachment for output ☑.