0% found this document useful (0 votes)
7 views9 pages

Java Programs for Data Types and More

The document contains code for a Java program that takes input of different data types like integer, string, float etc and stores them in variables of corresponding data types. It then prints the values of all variables to display the data entered by the user.

Uploaded by

217y1a66g4
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views9 pages

Java Programs for Data Types and More

The document contains code for a Java program that takes input of different data types like integer, string, float etc and stores them in variables of corresponding data types. It then prints the values of all variables to display the data entered by the user.

Uploaded by

217y1a66g4
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Aim: Java program on Data types.

Program:
import [Link].*;
class Infoo
{
public static void main(String argv[])
{
Scanner Obj = new Scanner([Link]);
[Link]("Enter name: ");
String name = [Link]();
[Link]("Enter Rollno: ");
int rollno = [Link]();
[Link]("Enter your age: ");
short age = [Link]();
[Link]("Enter Sno: ");
byte sno = [Link]();
[Link]("Enter your gender (F/M): ");
char gender = [Link]().charAt(0);
[Link]("Enter your ph no : ");
long phno = [Link]();
[Link]("Enter your GPA: ");
Float gpa = [Link]();
[Link]("Enter your attendances ");
double attend = [Link]();
[Link]("Do you have back logs true or false ");
boolean backlogs = [Link]();
[Link]("[Link]: " + sno+"Name: "+name+"\n [Link]: " +rollno+"\n Age: "+age+"\nGPA:
"+gpa+"\nAttendance: "+attend+"\nGender: "+ gender + "\nPhone no: "+phno + "\nBacklogs: "+backlogs);
}
}
OUTPUT:
C:\Users\MLRITM\Desktop\217Y1A0531>javac [Link]
C:\Users\MLRITM\Desktop\217Y1A0531>java Infoo
enter sno
100
enter marks
90
enter phoneno
1234567890
enter accno
234567
enter avg
99
enter attendence
99
enter result
true
enter name
mounika
sno : 100
name :mounika
marks :90
phoneno :1234567890
accno :234567
avg :99.0
attendence :99.0
grade :A
result :true
Aim: Java program to generate the Fibonacci series up to given
value. Program:
import [Link].*;
import [Link].*;
class Fibo1
{
public static void main(String argv[])
{
int a=0,b=1,c=a+b,n;
Scanner obj= new Scanner([Link]);
[Link]("Enter a value: ");
n=[Link]();
[Link]("FIbonacci series: ");
[Link](a+" "+b);
while(c<=n)
{
[Link](" "+c);
a=b;
b=c;
c=a+b;
}
}
}

Output:
Aim: Write a Java program to find factorial of a number

Program:
import [Link].*;
class Fact1
{
public static void main(String argv[])
{
Scanner obj=new Scanner([Link]);
[Link]("Enter a number: ");
int n=[Link](),feb=1;
for(int i=1;i<=n;i++)
feb*=i;
[Link]("Factorial of "+n+" is "+feb);
}
}

Output:
Aim: Write a Java program to check a String is palindrome or not

Program:
import [Link].*;
import [Link].*;
class Palindrome1
{
public static void main(String argv[])
{
String str,reverse="";
Scanner obj=new Scanner([Link]);
[Link]("Enter a string: ");
str=[Link]();
int len=[Link]();
for(int i=len-1;i>=0;i--)
reverse=reverse+[Link](i);
if([Link](reverse))
[Link](str+" is a palindrome");
else
[Link](str+" is not a palindrome");
}
}

Output:
Aim: write a Java program to print an array.
Program:
import [Link].*;
class Array
{
public static void main(String argv[])
{
int arr[]={10,20,30,40,50};
[Link]("Elements of an array: ");
for(int i=0;i<[Link];i++)
[Link](arr[i]+" ");
[Link]("\nElements of an array using for each: ");
for(int i:arr)
[Link](i+" ");

int arr1[]=new int[7];


arr1[0]=30;
arr1[1]=40;
arr1[2]=1000;
arr1[3]=20;
arr1[4]=200;
arr1[5]=100;
arr1[6]=67;
[Link]("\elements of array:");
for(int i:arr1)
[Link](i+" ");

}}

Output:
Aim: Write a Java program to find Quadratic equation.
Program:
import [Link].*;
class Quad
{
public static void main(String arg[])
{
double root1,root2;
Scanner obj = new Scanner([Link]);
[Link]("Enter a value: ");
int a =[Link]();
[Link]("Enter b value: ");
int b = [Link]();
[Link]("Enter c value: ");
int c= [Link]();
int dis=(b*b)-(4*a*c);
if(dis==0)
{
root1=root2=(-b)/(2*a);
[Link]("Roots are real and equal: \nRoot1: "+root1+"\nRoot2:
"+root2);
}
else if(dis>0)
{
double sq=[Link](dis);
root1=((-b)-sq)/(2*a);
root2=((-b)+sq)/(2*a);
[Link]("Roots are real and not equal: \nRoot1: "+root1+"\nRoot2: "+root2);
}
else
{
double usq=[Link](-(dis));
[Link]("Roots are Imaginary: \nRoot1: "+(-b/(2*a))+"-
i"+(usq/(2*a))+"\nRoot2: "+(-b/(2*a))+"+i"+(usq/(2*a)));
}}}
OUTPUT:
AIM:
Write a program to print electric bill
PROGRAM:
import [Link].*;
import [Link].*;
class Billpay
{
public static void main(String args[])
{
double billpay=0;
Scanner sc=new Scanner([Link]);
[Link]("enter customer name");
String name=[Link]();
[Link]("enter usc");
int usc=[Link]();
[Link]("enter month");
String month=[Link]();
[Link]("enter units");
int units=[Link]();
if (units<100)
{
billpay=units*1.20;
}
else if(units>100||units<200)
{
billpay=units*2.20;
}
else if(units>200)
{
billpay=units*3.30;
}
[Link]("The customer has to pay the bill of" +month +" is"
+billpay);
}
}
Output:
Aim: Java program on Data types.
Program:
class Info
{
public static void main(String argv[])
{
Int sno =101;
String name =”monika";
short marks = 90; int phoneno =1234567891;
Long accno=234566543;
Float avg= 9.8f;
double attendance= 98.0;
Char grade=”A”;
boolean backlogs = true;
[Link]("[Link]: " + sno+"Name: "+name+"\n [Link]: " +rollno+"\n
Age: "+age+"\nGPA: "+gpa+"\nAttendance: "+attend+"\nGender: "+ gender +
"\nPhone no: "+phoneno + "\nBacklogs: "+backlogs);
}
}
Output:
Aim: Java program to print range of Data types.
Program:
import [Link].*;
class DatatypeRange
{
public static void main(String argv[])
{
[Link]("Byte Range: ( "+Byte.MIN_VALUE+" ,
"+Byte.MAX_VALUE+" )\n");
[Link]("Short Range: ( "+Short.MIN_VALUE+" ,
"+Short.MAX_VALUE+" )\n");
[Link]("Int Range: ( "+Integer.MIN_VALUE+" ,
"+Integer.MAX_VALUE+" )\n");
[Link]("Long Range: ( "+Long.MIN_VALUE+" ,
"+Long.MAX_VALUE+" )\n");
[Link]("Float Range: ( "+Float.MIN_VALUE+" ,
"+Float.MAX_VALUE+" )\n");
[Link]("Double Range: ( "+Double.MIN_VALUE+" ,
"+Double.MAX_VALUE+" )”);
}
}
OUTPUT:

Common questions

Powered by AI

The Java program prints arrays using two methods: a traditional for loop and a for-each loop. In the traditional loop, it iteratively accesses each element by its index position (`arr[i]`). The for-each loop simplifies the task by directly accessing elements sequentially, which makes the code more concise and readable. This approach, however, does not provide access to the index of each element, making it unsuitable for certain operations but sufficient for simply displaying array contents .

The Java program calculates electricity bills by applying different rates based on the units consumed. It uses conditional statements to determine the rate: for units less than 100, the charge is 1.20 per unit; for units between 100 and 200, the rate is 2.20 per unit; and for anything above 200 units, the rate charged is 3.30 per unit. This tiered rate structure is implemented using a series of if-else statements .

The Java Fibonacci series program uses an iterative approach to generate the series. It employs a while loop starting with initial values `a=0` and `b=1`, calculating the next term by adding the previous two (c = a + b). To prevent exceeding the input limit, n, the program checks within the loop if the next term, c, is less than or equal to n before printing and iteratively updating values of a and b .

The palindrome check program takes an input string and reverses its order. This is done by iterating from the end of the string to the beginning and concatenating each character to form a reversed string. After constructing the reversed version, the program compares it to the original string using the `equals` method. If they match, the input string is identified as a palindrome; otherwise, it is not .

The program displays the range constants for each primitive Java data type. This design aids programmers by providing immediate reference to data limits, facilitating informed decisions on type selection based on the expected ranges of values in specific use cases. This direct showcase is particularly beneficial in debugging and in educational contexts where comprehending data type limits is crucial. However, its utility in production environments is limited since such values are constants available through Java documentation, suggesting it might serve better as a learning tool than an application component .

The program uses `Scanner` to process user inputs sequentially, with each input stored in a variable corresponding to its data type. While this method is simple and effective for smaller-scale applications, its scalability is limited by its linear and procedural input handling. For large-scale systems, this approach can result in cumbersome code with higher maintenance challenges and limited flexibility, suggesting that more modular and abstracted input handling designs, possibly using Java's Streams or event-driven inputs, would be more scalable .

The program uses a variety of data types to store user details: `String` for names, `int` for roll numbers, `short` for age, `byte` for serial number, `char` for gender, `long` for phone number, `float` for GPA, `double` for attendance, and `boolean` for backlogs. Each type is chosen to optimize memory usage and appropriately represent the expected range of values. For instance, `long` is necessary for phone numbers due to their length, while `short` suffices for age. However, the use of `float` for GPA might risk precision errors, suggesting `double` could be a more accurate choice. The structured use of types shows deliberate selection but reflects a trade-off between memory efficiency and precision .

The Java program calculates quadratic equation roots by checking the discriminant (dis). If the discriminant is negative (dis < 0), the roots are imaginary. The program then calculates the imaginary roots using the square root of the negative discriminant, represented as: root1 = (-b/(2*a)) - i(usq/(2*a)) and root2 = (-b/(2*a)) + i(usq/(2*a)), where usq is the square root of the absolute value of dis .

The factorial program uses a `for` loop to multiply a sequence of integers ranging from 1 to n, storing the ongoing product in the variable `feb`. An iterative approach like this offers simplicity and clarity, avoiding the overhead of recursive method calls. This iterative method ensures easy understanding of the operation and manages larger numbers more predictably due to controlled loop bounds rather than potential recursive depth issues .

The `short` data type in Java is used in the program to store age because it provides a sufficient range (-32,768 to 32,767) to represent ages realistically. It uses only 16 bits, making it more memory efficient than `int`, although such memory efficiency is often unnecessary given the normally ample memory availability in modern applications. Choosing `short` for age reflects an understanding of typical age ranges, optimizing memory without risking overflow when storing typical human ages .

You might also like