0% found this document useful (0 votes)
431 views16 pages

MCA Sem Java Program Solution

The document contains solutions to 12 Java programming problems. It includes the code and output for each problem. Problem 1 asks to print prime numbers and Fibonacci numbers between a given range. Problem 2 checks if a number is a palindrome. Problem 3 calculates the value of x^n. Problem 4 checks if a number is an Armstrong number. The remaining problems find minimum/maximum numbers, take student names as arguments, count letters and digits in a string, and create a Student class.

Uploaded by

pinkesh bhunatar
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
431 views16 pages

MCA Sem Java Program Solution

The document contains solutions to 12 Java programming problems. It includes the code and output for each problem. Problem 1 asks to print prime numbers and Fibonacci numbers between a given range. Problem 2 checks if a number is a palindrome. Problem 3 calculates the value of x^n. Problem 4 checks if a number is an Armstrong number. The remaining problems find minimum/maximum numbers, take student names as arguments, count letters and digits in a string, and create a Student class.

Uploaded by

pinkesh bhunatar
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 16

MCA SEM-1 Java Program Solution

Java Programs
Program 2 :- Write a program to pass Starting and Ending limit and print all prime numbers
and Fibonacci numbers in between this range.
Code :-
import java.util.Scanner;
class Practical2
{
public static void main(String args[])
{
int sp,ep,i,a=0,b=1,c;
Scanner sc = new Scanner(System.in);

System.out.println("Enter Statring Point : ");


sp = sc.nextInt();

System.out.println("Enter Ending Point : ");


ep = sc.nextInt();

System.out.println("Below Numbers Are Prime Numbers in Between "+sp+" And


"+ep+".......");

for(i=sp; i<=ep; i++)


{
if(i%2!=0)
{
System.out.print(" "+i);
}
}
System.out.print("\n");

System.out.println("Below Numbers Are Fibonacci Series in Between "+sp+" And


"+ep+".......");
System.out.print(a+" "+b);
for(i=sp; i<=ep; i++)
{
c=a+b;
System.out.print(" "+c);
a=b;
b=c;
}
}
}

Output :-
MCA SEM-1 Java Program Solution

Program 3 :- Write a program to check whether number is palindrome or not.


Code :-
import java.util.Scanner;

class palindrome
{
public static void main(String a[])
{
Scanner sc = new Scanner(System.in);
int x,y=0,remainder,palin=0;

System.out.println("Enter a number check whether a number is palindrome or not!");


x = sc.nextInt();

y=x;

while(x!=0)
{
remainder=x%10;
palin=palin*10+remainder;
x=x/10; //here x becomes 0
}

if(palin==y)
System.out.println("It is a Palindrome Number : ");
else
System.out.println("It is not a Palindrome Number : ");
}
}

Output :-
MCA SEM-1 Java Program Solution

Program 4 :- Write a program to print value of x^n.


Code :-
import java.util.Scanner;

class Practical4
{
public static void main(String args[])
{
int x,n,ans=1;

Scanner sc = new Scanner(System.in);

System.out.println("To print value of x^n....");

System.out.println("Enter Value of X : ");


x = sc.nextInt();

System.out.println("Enter Value of N : ");


n = sc.nextInt();

System.out.print("The answer of "+x+"^"+n+" is ");


for(; n!=0; n--)
{
ans*=x;
}
System.out.print(ans);
}
}

Output :-
MCA SEM-1 Java Program Solution

Program 5 :- Write a program to check Armstrong Number


Code :-
import java.util.Scanner;

class armstrong
{
public static void main(String a[])
{
int i,length,cube=0,number;
int[] x = new int[10];

Scanner sc = new Scanner(System.in);

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


number = sc.nextInt();

System.out.println("Now Enter Your Number One by One....");


for(i=0; i<String.valueOf(number).length(); i++)
{
x[i]=sc.nextInt();
}

for(i=0; i<String.valueOf(number).length(); i++)


{
cube=cube+(x[i]*x[i]*x[i]);
}

if(number==cube)
{
System.out.println("Armstrong ");
}
else
{
System.out.println("Not Armstrong ");
}
}
}

Output :-
MCA SEM-1 Java Program Solution

Program 6 :- Write a program to find minimum of three numbers using conditional operator.
Code :-
import java.util.Scanner;

class Practical6
{
public static void main(String args[])
{
int a,b,c,min;

Scanner sc = new Scanner(System.in);

System.out.print("Enter First Number : ");


a = sc.nextInt();

System.out.print("Enter Second Number : ");


b = sc.nextInt();

System.out.print("Enter Third Number : ");


c = sc.nextInt();

if(a<b && a<c)


{
System.out.println("First Number is Smaller!");
}
else if(b<c)
{
System.out.println("Second Number is Smaller!");
}
else
{
System.out.println("Third Number is Smaller!");
}
}
}

Output :-
MCA SEM-1 Java Program Solution

Program 7 :- Write a java program which should display maximum number of given 4
numbers.
Code :-
import java.util.Scanner;

class Practical7
{
public static void main(String args[])
{
int a,b,c,d,max;

Scanner sc = new Scanner(System.in);

System.out.print("Enter Fisrt Number : ");


a = sc.nextInt();

System.out.print("Enter Second Number : ");


b = sc.nextInt();

System.out.print("Enter Third Number : ");


c = sc.nextInt();

System.out.print("Enter Fourth Number : ");


d = sc.nextInt();

if(a>b && a>c && a>d)


{
System.out.println("First Number Is Maximum!");
}
else if(b>c && b>d)
{
System.out.println("Second Number Is Maximum!");
}
else if(c>d)
{
System.out.println("Third Number Is Maximum!");
}
MCA SEM-1 Java Program Solution
else
{
System.out.println("Fourth Number Is Maximum!");
}
}
}

Output :-
MCA SEM-1 Java Program Solution

Program 10 :- Write a Java application which takes several command line arguments, which
are supposed to be names of students and prints output as given below:
(Supposed we enter 3 names then output should be as follows)…Number of arguments = 3
1. First Student Name is = Arun
2. Second Student Name is = Hiren
3. Third Student Name is = Hitesh
Code :-
import java.util.Scanner;

class Practical10
{
void Student(String sname, String sname1, String sname2)
{
System.out.println("\nFirst Student name is = "+sname);
System.out.println("Second Student name is = "+sname1);
System.out.println("ThirdStudent name is = "+sname2);
}
public static void main(String args[])
{
String name,name1,name2;

Scanner sc = new Scanner(System.in);

System.out.print("Enter Fisrt Student Name : ");


name = sc.nextLine();

System.out.print("Enter Second Student Name : ");


name1 = sc.nextLine();

System.out.print("Enter Third Student Name : ");


name2 = sc.nextLine();

Practical10 s1 = new Practical10();


s1.Student(name,name1,name2);
MCA SEM-1 Java Program Solution
}
}

Output :-

Program 11 :- Write a Java application to count and display frequency of letters and digits
from the String given by user as command-line argument.
Code :-
class Practical11
{
public static void main(String[] args)
{
if(args.length<=0)
{
System.out.println("Enter a string using command line");
}
else
{
String text = args[0];
int letters = 0, digits = 0;

text = text.toLowerCase();
for(int i = 0; i < text.length(); ++i)
{
char ch = text.charAt(i);
if((ch >= 'a' && ch <= 'z'))
{
letters++;
}
else if( ch >= '0' && ch <= '9')
{
digits ++;
}
}
System.out.println("String : " + args[0]);
System.out.println("Letters : " + letters);
System.out.println("Digits : " + digits );
}
}
}
MCA SEM-1 Java Program Solution
Output :-

Program 12 :- Create s class “Student” that would contain enrollment No, name and gender
and marks as instance variables and count as static variable which stores the count of the
objects; constructors and display(). Implement construction to initialize instance variables.
Also demonstrate constructors chaining. Create objects of class “Student” and displays all
values of objects.
Code :-
class Student
{
int enrollmentNo;
String name;
String gender;

public Student()
{
enrollmentNo=0;
name="undefine";
gender="male";
}
public Student(int en,String nm, String gn)
{
setEnrollmentNo(en);
setName(nm);
setGender(gn);
}
//------------------------------------------------------------------
public int getEnrollmentNo()
{
return(enrollmentNo);
}
public void setEnrollmentNo(int en)
{
enrollmentNo=en;
}
//------------------------------------------------------------------
public String getName()
MCA SEM-1 Java Program Solution
{
return(name);
}
public void setName(String nm)
{
name=nm.toUpperCase();
}
//------------------------------------------------------------------
public String getGender()
{
return(gender);
}
public void setGender(String gn)
{
gender=gn.toUpperCase();
}
//------------------------------------------------------------------
public void displayStudent()
{
System.out.println("***********************");
System.out.println("Enrollment No : "+getEnrollmentNo());
System.out.println("Student Name : "+getName());
System.out.println("Gender : "+getGender());
System.out.println("***********************");
}
}
class Practical12
{
public static void main(String args[])
{
Student s = new Student();
s.displayStudent();
Student s1 = new Student(1,"bipin","male");
s1.displayStudent();

}
}

Output :-
MCA SEM-1 Java Program Solution

Program 13 :- Write a program Java to demonstrate use of this keyword. Check whether this
can access the Static variables of the class or not. [Refer class Student in Q12 to perform the
task]
Code :-
class Student
{
int enrollmentNo;
String name;
String gender;

public Student()
{
enrollmentNo=0;
name="undefine";
gender="male";
}
public Student(int en,String nm, String gn)
{
this.enrollmentNo=en;
this.name=nm.toUpperCase();
this.gender=gn.toUpperCase();
}
//------------------------------------------------------------------
public int getEnrollmentNo()
{
return(this.enrollmentNo);
}
public void setEnrollmentNo(int en)
{
this.enrollmentNo=en;
}
//------------------------------------------------------------------
public String getName()
{
return(this.name);
}
public void setName(String nm)
{
this.name=nm.toUpperCase();
MCA SEM-1 Java Program Solution
}
//------------------------------------------------------------------
public String getGender()
{
return(this.gender);
}
public void setGender(String gn)
{
this.gender=gn.toUpperCase();
}
//------------------------------------------------------------------
public void displayStudent()
{
System.out.println("***********************");
System.out.println("Enrollment No : "+this.getEnrollmentNo());
System.out.println("Student Name : "+this.getName());
System.out.println("Gender : "+this.getGender());
System.out.println("***********************");
}
}
public class Practical13
{
public static void main(String args[])
{
Student s = new Student();
s.displayStudent();
Student s1 = new Student(1,"bipin","male");
s1.displayStudent();
}
}

Output :-
MCA SEM-1 Java Program Solution

Program 14 :- Create a class “Rectangle” that would contain length and width as an instance
variable and count as a static variables. Define constructors [constructor overloading
(default, parameterized and copy)] to initialize variables of objects. Define methods to
find area and to display variables’ value of objects which are created.
[Note: define initializer block, static initializer block and the static variable and method. Also
demonstrate the sequence of execution of initializer block and static initialize block]
Code :-
class Rectangle
{
//data member
int length=fieldInit();
int width=-1;
static int numOfObject=-1;
//initializer block
{
System.out.println("Initializer block execute");
length=0;
width=0;
}
//static initializer block
static
{
System.out.println("Static block initialize");
numOfObject=0;
}
//default constructor
public Rectangle()
{
length=1;
width=1;
System.out.println("Default constructor invoked");
numOfObject++;
}
//parameterized constructor
public Rectangle(int l, int b)
{
System.out.println("Parameterized constructor invoked");
length=l;
MCA SEM-1 Java Program Solution
width=b;
numOfObject++;
}
//copy constructor
public Rectangle(Rectangle obj)
{
System.out.println("Copy constructor invoked");
length=obj.length;
width=obj.width;
numOfObject++;
}
//method to calculate area of rectangle
public void getArea()
{
System.out.println("Length : "+length);
System.out.println("Width : "+width);
System.out.println("Area : "+length*width);
}
//count number of objects created using static field
public static void getNumOfObject()
{
System.out.println("Number of object created : "+numOfObject);
}

public int fieldInit()


{
System.out.println("Field initialize");
return(-1);
}
}
class Practical14
{
public static void main(String args[])
{
System.out.println("\n********************************\n");
Rectangle s1=new Rectangle();
s1.getArea();
Rectangle.getNumOfObject();
System.out.println("\n********************************\n");
Rectangle s2=new Rectangle(s1);
System.out.println("\n********************************\n");
Rectangle s3=new Rectangle(6,3);
s3.getArea();
Rectangle.getNumOfObject();
System.out.println("\n********************************\n");
}
}

Output :-
MCA SEM-1 Java Program Solution

Program 15 :- Write a java program static block which will be executed before main()
method in a class.
Code :-
class Practical15
{
static
{
System.out.println("static block is invoked");
}
public static void main(String args[])
{
System.out.println("main method is invoked");
}
}

Output :-

You might also like