PCC LAB
PCC LAB
1
AIM: Implement the following in JAVA: Variables, Data Types,
Operators and Control Statements.
THEORY: In Java, variables are containers for storing data, declared
with a specific datatype like int, float, or String. Operators perform
operations on variables, such as arithmetic (+, -, *,/) or logical (&&,
||). Control statements regulate the flow of a program, including if-
else for conditional branching and loops like for and while for
repetitive tasks. By combining these elements effectively, Java
programs can manipulate data and control program flow to achieve
desired outcomes.
1. Variables: In Java, variables are placeholders for storing data
values. They are declared using a specific datatype (int, float, double,
char, boolean, etc.) followed by a name and optional initial value.
2. Datatypes: Datatypes specify the type of data that a variable can
hold. Java has two categories of datatypes: primitive and reference.
Primitive datatypes include int, float, double, char, boolean, etc.,
while reference datatypes include objects, arrays, and classes. Each
datatype has specific size and range constraints.
3. Operators: Operators in Java are symbols that perform operations
on operands. They can be categorized into arithmetic, relational,
logical, bitwise, and assignment operators. Arithmetic operators
perform mathematical operations like addition, subtraction,
multiplication, division, and modulus. Relational operators compare
two values and return a boolean result. Logical operators perform
logical AND, OR, and NOT operations. Bitwise operators perform
operations at bit-level. Assignment operators assign a value to a
variable.
4. Control Statements: Control statements in Java regulate the flow of
execution in a program. They include conditional statements (if-else,
switch-case) for decision making and loop statements (for, while, do-
while) for repetitive execution. Conditional statements allow the
program to make decisions based on certain conditions, while loop
statements execute a block of code repeatedly until a specified
condition is met.
/*Shreya Gokhale
D2A 19
Aim:Program to print a given pattern
Date of Preparation:18/01/2024
Date of Submission:25/01/2024
*/
public class Pattern
{
public static void main(String[] args)
{
int i, j;
for (i = 0; i < 5; i++)
{
for (j = 5 - i; j > 1; j--)
{
System.out.print(" ");
}
for (j = 0; j <= i; j++)
{
System.out.print("* ");
}
System.out.println();
}
}
}
OUTPUT:
/*Shreya Gokhale
D2A 19
Aim:Program to calculate factorial of a given number
Date of Preparation:18/01/2024
Date of Submission:25/01/2024
*/
import java.util.Scanner;
public class Factorial
{
public static void main(String[] args)
{
int i,fact=1;
Scanner sc=new Scanner(System.in);
System.out.println("Enter a number to calculate
factorial:");
int n=sc.nextInt();
for(i=1;i<=n;i++)
{
fact=fact*i;
}
System.out.println("The factorial of given number
is:"+fact);
}
}
OUTPUT:
/*Shreya Gokhale
D2A 19
Aim:Program to find the largest of 3 numbers
Date of Preparation:18/01/2024
Date of Submission:25/01/2024
*/
import java.util.Scanner;
public class Largest
{
public static void main(String[] args)
{
System.out.println("Enter 3 numbers:");
Scanner sc=new Scanner(System.in);
int n1=sc.nextInt();
int n2=sc.nextInt();
int n3=sc.nextInt();
if(n1>n2 && n1>n3)
{
System.out.println("The largest number is "+n1);
}
else if(n2>n1 && n2>n3)
{
System.out.println("The largest number is "+n2);
}
else
{
System.out.println("The largest number is "+n3);
}
}
}
OUTPUT:
/*Shreya Gokhale
D2A 19
Aim:Program to display Fibonacci Series
Date of Preparation:18/01/2024
Date of Submission:25/01/2024
*/
import java.util.Scanner;
public class Fibonacci
{
public static void main(String[] args)
{
int i,n1=0,n2=1,n3;
System.out.println("Enter length of fibonacci series:");
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
System.out.println("Fibonacci Series:");
System.out.print(n1+","+n2+",");
for(i=1;i<=n;i++)
{
n3=n1+n2;
System.out.print(n3+",");
n1=n2;
n2=n3;
}
}
}
OUTPUT:
/*Shreya Gokhale
D2A 19
Date:18 Jan
Aim:Program to display student info and calculate average marks
obtained
Date of Preparation:18/01/2024
Date of Submission:25/01/2024
*/
import java.util.Scanner;
public class Student
{
//method to display student details
public void displayInfo()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter your name:");
String name=sc.next();
System.out.println("Enter your rollno:");
int rollno=sc.nextInt();
System.out.println("Enter your division:");
String division=sc.next();
System.out.println("Student details:");
System.out.println(name);
System.out.println(rollno);
System.out.println(division);
}
//method to calculate avg marks
public void avgMarks()
{
Scanner obj=new Scanner(System.in);
System.out.println("Enter marks obtained in Physics:");
int m1=obj.nextInt();
System.out.println("Enter marks obtained in Chemistry:");
int m2=obj.nextInt();
System.out.println("Enter marks obtained in Maths:");
int m3=obj.nextInt();
System.out.println("Enter marks obtained in English:");
int m4=obj.nextInt();
System.out.println("Enter marks obtained in CS:");
int m5=obj.nextInt();
float avg=(m1+m2+m3+m4+m5)/5.0F;
System.out.println("Average marks obtained:");
System.out.println(avg);
}
public static void main(String[] args)
{
Student st=new Student();
st.displayInfo();
st.avgMarks();
}
}
OUTPUT:
CONCLUSION:
I have developed decent understanding of the variables, data types,
operators, and control statements in Java programming by
successfully performing and implementing these concepts in
practical examples.These concepts provide the foundation for
manipulating data and controlling program flow efficiently, enabling
us to create robust solutions to diverse programming challenges.