[Go to site: main page, start]

0% found this document useful (0 votes)
2 views6 pages

Java Program Assignment2

The document contains Java programs demonstrating basic programming concepts such as printing messages, performing arithmetic operations, finding the greatest number among three integers, using loops to print names and numbers, calculating the sum of a range of numbers, summing the digits of a number, and reversing a number. Each program is structured with a main method and includes comments explaining the functionality. The document also includes sample outputs for some of the programs.

Uploaded by

Parineeta Deb
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)
2 views6 pages

Java Program Assignment2

The document contains Java programs demonstrating basic programming concepts such as printing messages, performing arithmetic operations, finding the greatest number among three integers, using loops to print names and numbers, calculating the sum of a range of numbers, summing the digits of a number, and reversing a number. Each program is structured with a main method and includes comments explaining the functionality. The document also includes sample outputs for some of the programs.

Uploaded by

Parineeta Deb
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

//Java program to print Hello KIIT

public class DemoApp{


public static void main(String args [])
{
int x=100;
[Link]("Hello KIIT"+x);
}
}

Q1. Assign two values and calculate their sum, product, remainder of division and display.
public class Add{
public static void main(String args[])
{
int a = 10;
int b = 22;
int sum = a + b;
int product = a * b;
int remainder = a % b;
[Link]("\n The sum of the two values given is : " + sum);
[Link]("\n The product of the two values given is : " + product);
[Link]("\n The remainder of the two values given is : " +
remainder);
}
}

Q2. Assign three integer values to different variables, find out the greatest one.
public class Greatest{
public static void main(String args[])
{
int a = 10;
int b = 22;
int c = 42;
if (a>b && a>c)
[Link]("\n" +a+ " is the greatest one ");
if(b>c && b>a)
[Link]("\n" +b+ " is the greatest one ");
if(c>a && c>b)
[Link]("\n" +c+ " is the greatest one ");
}
}

C:\2470472>javac [Link]
C:\2470472>java Greatest
42 is the greatest one

Q3. Print your name 10 times using a loop.


public class PrintName{
public static void main(String args[])
{
//My Name
String name = "Parineeta";
//loop to print the name 10 times
int i;
for(i=1;i<=10;i++)
{
[Link](name);
}
}
}
C:\2470472>javac [Link]
C:\2470472>java PrintName
Parineeta
Parineeta
Parineeta
Parineeta
Parineeta
Parineeta
Parineeta
Parineeta
Parineeta
Parineeta
Q4. Print 1 to 20 using a loop.
public class PrintNumbers{
public static void main(String args[])
{
//loop to print the numbers from 1 to 20
int i;
for(i=1;i<=20;i++)
{
[Link](i);
}
}
}
C:\2470472>javac [Link]
C:\2470472>java PrintNumbers
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Q5. Find the sum of numbers from 10 to 20.
public class Sum{
public static void main(String args[])
{
int sum=0;
//loop to add the numbers from 10 to 20
int i;
for(i=10;i<=20;i++)
{
sum+=i;
}
[Link]("The sum of numbers from 10 to 20 is : " + sum);
}
}
C:\2470472>javac [Link]
C:\2470472>java Sum
The sum of numbers from 10 to 20 is : 165
Q.6. Find sum of digits of a assigned number.
public class SumDigits{
public static void main(String args[])
{
int a = 2346;
int sum=0;
While (a!=0)
{
Sum += a % 10;
a /= 10;
}
[Link](“The sum of the digits of the given number is : “ +sum);
}
}

Output:-
The sum of the two given numbers is 15.

Q.7. Assign a number and reverse it.


public class ReversedNumber{
public static void main(String args[])
{
int num = 2346;
int revnum = 0;
While(num!=0)
{
int digit = num % 10;
revnum = revnum * 10 + digit;
num = num / 10;
}
[Link](“Reversed Number is : “ +revnum);
}
}
Output:-
Reversed Number is : 6432

You might also like