EVEN OR ODD
import [Link];
public class IfElseExample
public static void main(String[ ] args)
Scanner sc = new Scanner([Link]);
[Link]("Enter a number: ");
int num = [Link]();
if (num % 2 == 0)
[Link](num + " is Even");
} else
[Link](num + " is Odd");
}
[Link]();
MULTIPLICATION TABLE
import [Link];
public class TableProgram {
public static void main(String[ ] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a number: ");
int num = [Link]();
[Link]("Multiplication Table of " + num);
for (int i = 1; i <= 10; i++) {
[Link](num + " x " + i + " = " + (num * i));
}
[Link]();
}
}
1. CONSTRUCTORS AND STATIC MEMBERS
AIM
To write a Java program to demonstrate the use of constructors and static members by accepting
student details from the user and displaying them.
ALGORITHM
1. Start the program.
2. Import the Scanner class.
3. Create a Student class with:
4. Instance variables: rollNo, name, and course.
5. Static variable: college.
6. Create a constructor to initialize the student details.
7. Create a display() method to print the student details.
8. In the main() method:
9. Create a Scanner object.
10. Read the roll number, name, and course from the user.
11. Create a Student object using the constructor.
12. Call the display() method.
13. Stop the program.
PROGRAM
import [Link];
class Student {
int rollNo;
String name;
String course;
static String college = " ABC College ";
// Constructor
Student(int r, String n, String c) {
rollNo = r;
name = n;
course = c;
void display( )
[Link]("\nStudent Details");
[Link]("Roll No : " + rollNo);
[Link]("Name : " + name);
[Link]("Course : " + course);
[Link]("College : " + college);
public static void main(String[ ] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter Roll No: ");
int roll = [Link]();
[Link]();
[Link]("Enter Name: ");
String name = [Link]();
[Link]("Enter Course: ");
String course = [Link]();
Student s1 = new Student(roll, name, course);
[Link]();
[Link]();
Sample Output
Enter Roll No: 101
Enter Name: John
Enter Course: BCA
Student Details
Roll No : 101
Name : John
Course : BCA
College : ABC College