0% found this document useful (0 votes)
13 views18 pages

Java

The document contains solutions to 10 Java programming assignments involving printing output, taking user input to perform basic math operations like addition and subtraction, checking if a number is even/odd or prime, reversing a number, using command line arguments, checking if a number is Armstrong or palindrome, printing the Fibonacci series, and demonstrating the 'this' keyword. Each assignment includes the full code solution and sample output.

Uploaded by

camoleb835
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
13 views18 pages

Java

The document contains solutions to 10 Java programming assignments involving printing output, taking user input to perform basic math operations like addition and subtraction, checking if a number is even/odd or prime, reversing a number, using command line arguments, checking if a number is Armstrong or palindrome, printing the Fibonacci series, and demonstrating the 'this' keyword. Each assignment includes the full code solution and sample output.

Uploaded by

camoleb835
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 18

ASSIGNMENT - 1

1. Write a java program to print “ Hello World . “


Ans.
import java.io.*;
class a2
{
public static void main(String args[])
{
System.out.println("Hello World");
}
}

Output:

2. Write a java program to take input from user and perform operation
Addition , Subtraction , Multiplication , Division .
Ans.
import java.io.*;
class add
{
public static void main(String args[]) throws IOException
{
int a,b,c;
DataInputStream x=new DataInputStream(System.in);
System.out.println(" Enter the value of a:");
a=Integer.parseInt(x.readLine());
System.out.println(" Enter the value of b:");
b=Integer.parseInt(x.readLine());
c=a+b;
System.out.println("Addition of :"+c);
}
}

Output:

Subtration:
import java.io.*;
class sub
{
public static void main(String args[]) throws IOException
{
int a,b,c;
DataInputStream x=new DataInputStream(System.in);
System.out.println(" Enter the value of a:");
a=Integer.parseInt(x.readLine());
System.out.println(" Enter the value of b:");
b=Integer.parseInt(x.readLine());
c=a-b;
System.out.println("Subtration of :"+c);
}
}

Output:

Multiplication :
import java.io.*;
class mul
{
public static void main(String args[]) throws IOException
{
int a,b,c;
DataInputStream x=new DataInputStream(System.in);
System.out.println(" Enter the value of a:");
a=Integer.parseInt(x.readLine());
System.out.println(" Enter the value of b:");
b=Integer.parseInt(x.readLine());
c=a*b;
System.out.println("Multiplication of :"+c);
}
}

Output:

Division :
import java.io.*;
class div
{
public static void main(String args[]) throws IOException
{
int a,b,c;
DataInputStream x=new DataInputStream(System.in);
System.out.println(" Enter the value of a:");
a=Integer.parseInt(x.readLine());
System.out.println(" Enter the value of b:");
b=Integer.parseInt(x.readLine());
c=a/b;
System.out.println("Division of :"+c);
}
}

Output:

3. Write a java Program to take input from user to check whether


number is even or odd .
Ans .
import java.io.*;
class condition
{
public static void main(String args[])throws IOException
{
int n;
DataInputStream x=new DataInputStream(System.in);
System.out.println(" Enter the value of n:");
n=Integer.parseInt(x.readLine());
if(n%2==0)
{
System.out.println("Number is even:"+n);
}
else
{
System.out.println("Number is odd:"+n);
}
}
}

Output:

4. Write a java program to take input from user check wheather number
is prime or not .
Ans.
import java.io.*;
class prime
{
public static void main(String args[])throws IOException
{
int n;
DataInputStream x=new DataInputStream(System.in);
System.out.print(" Enter the value of n:");
n=Integer.parseInt(x.readLine());
if(isprime(n))
{
System.out.println("Number is Prime:"+n);
}
else
{
System.out.println("Number is not prime:"+n);
}
}
public static boolean isprime(int n)
{
if(n<=1)
{
return false;
}
for(int i=2;i<=Math.sqrt(n);i++)
{
if(n%i==0)
{
return false;
}
}
return true;
}
}

5. Write a java program to take input from user any number and reverse
it .
Ans.
import java.io.*;
class rev
{
public static void main(String args[])throws IOException
{
int n,r,rev;
rev=0;
DataInputStream x=new DataInputStream(System.in);
System.out.print(" Enter the value of n:");
n=Integer.parseInt(x.readLine());
while(n!=0)
{
r=n%10;
rev=rev*10+r;
n=n/10;
}
System.out.println("Reverse Number is :" +rev);
}
}

Output:

6. Write a java program to take input from user and perform addition
using command Line argument.
Ans.
import java.io.*;
class cmdl
{
public static void main(String args[])
{
int a=Integer.parseInt(args[0]);
int b=Integer.parseInt(args[1]);
int sum=a+b;
System.out.println("Sum of two numbers:"+sum);
}
}
Output:
7. Write a java Program to take input from user and check whether the
number is Armstrong or not.
Ans.
import java.io.*;
class armstr
{
public static void main(String args[]) throws IOException
{
int n,count=0,a,b,c,sum=0;
DataInputStream x=new DataInputStream(System.in);
System.out.println("enter value of n:");
n=Integer.parseInt(x.readLine());
a=n;
c=n;
while(a>0)
{
a = a / 10;
count++;
}
while(n > 0)
{
b = n % 10;
sum = (int) (sum+Math.pow(b, count));
n = n / 10;
}
if(sum == c)
{
System.out.println(c+ " is an Armstrong number");
}
else
{
System.out.println(c+ " is not an Armstrong number");
}
}
}

Output

8. Write a java Program to take input from user and check whether the
number is Palindrome or not.
Ans.
import java.io.*;
class palindrom
{
public static void main(String args[]) throws IOException
{
int r,rev=0,n,s;
DataInputStream x=new DataInputStream(System.in);
System.out.println("Enter N");
n=Integer.parseInt(x.readLine());
s=n;
while(n!=0)
{
r=n%10;
rev=rev*10+r;
n=n/10;
}
if(rev==s)
{
System.out.println("Palindrom");
}
else
{
System.out.println("Not Palindrom");
}
}
}
Output:

9. Write a java Program to take input from user and print Fibonacci Series.
Ans.
import java.io.*;
class febo
{
public static void main(String args[]) throws IOException
{
int a=0, b=1,c,n,i;
DataInputStream x=new DataInputStream(System.in);
System.out.println("Enter N:");
n=Integer.parseInt(x.readLine());
for(i=1;i<=n;i++)
{
System.out.print("" +a);
c=a+b;
a=b;
b=c;
}
}
}
Output:

10. Write a java program to demonstrate this keyword.


Ans.
import java.io.*;
class a
{
int rollno;
String name;
void get(int rollno,String name)
{
this.rollno=rollno;
this.name=name;
}
void display()
{
System.out.println("Enter Rollno:"+rollno);
System.out.println("Enter Name:"+name);
}
}
class This
{
public static void main(String args[])
{
a a1=new a();
a1.get(116,"Vijay");
a1.display();
}
}
OR Constructor This()
import java.io.*;
class add
{
int r;
String n;
add()
{
System.out.println("Default constructor");
}
add(int r,String n)
{
this();
this.r=r;
this.n=n;
}
void display()
{
System.out.println("Rollno:"+r);
System.out.println("Name:"+n);
}
}
class c
{
public static void main(String args[])
{
add a1=new add(138,"Vijay");
a1.display();
}
}

Output:

You might also like