Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

Java Programming for IT 802

The document provides multiple Java programming exercises, including creating a simple calculator using if-else statements, printing numbers using for and while loops, implementing switch-case statements, and generating a pyramid pattern with symbols. Each exercise includes a sample solution demonstrating the required functionality. The document serves as a guide for beginners to practice basic Java programming concepts.

Uploaded by

sanddep0000123
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Java Programming for IT 802

The document provides multiple Java programming exercises, including creating a simple calculator using if-else statements, printing numbers using for and while loops, implementing switch-case statements, and generating a pyramid pattern with symbols. Each exercise includes a sample solution demonstrating the required functionality. The document serves as a guide for beginners to practice basic Java programming concepts.

Uploaded by

sanddep0000123
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Programming Sets for Java.

Read Carefully and Don’t Ignore

1. Write down a program in Java to accept the two numbers from user and accept the
operator (+, -, * , / ) from user and print the resultant value. Make this program with
the help of if-else.
Solution:
import java.util.*;
public class JavaCalc {
public static void main(String[] args) {
Scanner input=new Scanner(System.in)
int fnum, snum, ans;
char sign; //THIS ASSIGNS THE CHARACTER VARIABLE + - * /
System.out.println("Welcome User,This is a Simple Calculator Created
by Victor Using the if statement");
System.out.print("Please Enter your first digit: ");
fnum=input.nextInt(); /*This receive input from user
it receive the first number*/
System.out.print("Enter the second digit: ");
snum=input.nextInt();/*This receive input from user
it receive the second number*/
System.out.print("Enter the mathematical operator to be used +-*/: ");
sign=input.next().charAt(0); //Receive input from user
if (sign == '+' ){
ans=fnum + snum;
System.out.println(fnum +" "+sign +" "+snum +"= "+" "+ ans );
}
else if(sign == '-')/* else if statement for minus sign*/
{
ans=fnum-snum;
System.out.println(fnum +" "+sign +" "+snum +"= "+" "+ ans );
}
else if(sign == '/'){
ans=fnum/snum;
System.out.println(fnum +" "+sign +" "+snum +"= "+" "+ ans );
}
else if(sign == '*'){
ans=fnum*snum;
System.out.println(fnum +" "+sign +" "+snum +"= "+" "+ ans );
}
else
System.out.println("Your Input is not correct,please check it for any
error(s).");
}
}
2. Write down a program in Java to print the number from 1 to 100 by the use of for
loop.
Solution:
public class ForLoop{
public static void main(String[ ] args) {
//Code of Java for loop
for(int i=1;i<=100;i++)
{
System.out.println(i);
}
}
}

3. Write down a program in java to show the use of switch and case in Java
Programming.
Solution:
public class Main {
public static void main(String[] args) {
int expression = 2;
// switch statement to check size
switch (expression) {
case 1:
System.out.println("Case 1");
// matching case
case 2:
System.out.println("Case 2");
case 3:
System.out.println("Case 3");
default:
System.out.println("Default case");
}
}
}
4. Write down a program in java to print number from 100 to 1 with the help of
while loop in Java Programming.
Solution:
public class Whileloop {
public static void main(String args[])
{ int i=100;
System.out.println("Output is=");
while (i>=1)
{
System.out.println(i);
i--;
}
}
}
5. Write down a program in Java to accept the two numbers from user and accept the
operator (+, -, *, / ) from user and print the resultant value. Make this program with the
help of switch and case.
Solution:
import java.util.*;
public class JavaSwtichCase {
public static void main(String args[])
{
double num1, num2;
Scanner scanner = new Scanner(System.in);
System.out.print("Enter first number:");
num1 = scanner.nextDouble();
System.out.print("Enter second number:");
num2 = scanner.nextDouble();
System.out.print("Enter an operator (+, -, *, /): ");
char operator = scanner.next().charAt(0);
scanner.close();
double output;
switch(operator)
{
case '+':
output = num1 + num2;
break;
case '-':
output = num1 - num2;
break;
case '*':
output = num1 * num2;
break;
case '/':
output = num1 / num2;
break;
default:
System.out.printf("You have entered wrong operator");
return;
}

System.out.println(num1+" "+operator+" "+num2+": "+output);


}
}

6. Write down a program in Java Programming to print the Pyramid with symbol.
Example: *
**
***
****
*****
Solution:
public class Pyramid {

public static void main(String[] args) {


int rows = 5;

for (int i = 1; i <= rows; ++i) {


for (int j = 1; j <= i; ++j) {
System.out.print("* ");
}
System.out.println();
}
}
}

You might also like