0% found this document useful (0 votes)
2 views4 pages

Java Expt1

The document contains a series of Java programs demonstrating basic programming concepts such as checking for neon numbers, printing patterns, converting binary to decimal, calculating the sum of digits, and determining leap years. Each section includes code snippets and prompts for user input, along with expected outputs. The conclusion states that the implementation of these basic Java programs using control structures was successful.
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)
2 views4 pages

Java Expt1

The document contains a series of Java programs demonstrating basic programming concepts such as checking for neon numbers, printing patterns, converting binary to decimal, calculating the sum of digits, and determining leap years. Each section includes code snippets and prompts for user input, along with expected outputs. The conclusion states that the implementation of these basic Java programs using control structures was successful.
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

GOA COLLEGE OF ENGINEERING

“Bhausaheb Bandodkar Technical Education Complex Farmagudi”

1)​ Neon Number: Check if the sum of the digits of the square of a number is equal
to the number itself (e.g., 9 \rightarrow 9^2 = 81 \rightarrow 8+1=9).

Code:
import [Link].*;
public class Neon {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter the number to check if its neon number or not: ");
int n = [Link]();
int sq = n*n;
int sum = 0;
while(sq != 0){
int rem = sq%10;
sum += rem;
sq /= 10;
}
if(sum == n){
[Link]("Its a neon number "+n);
}else {
[Link]("Its not a neon number "+n);
}
}
}

Output:

2)​ Print the pattern


Code:
import [Link];
public class Pattern {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter the number of rows for pattern: ");
int n = [Link]();
for(int i=0; i<n; i++){
for(int j=0; j<=i; j++){
[Link]("* ");
}

Chirag Nikant Simepurushkar 24B-CO-015 Page No:


GOA COLLEGE OF ENGINEERING
“Bhausaheb Bandodkar Technical Education Complex Farmagudi”
[Link]();
}
}
}

Output:

3)​ Binary to Decimal: Convert a binary string (e.g., 1011) into its decimal value
manually using powers of 2.

Code:
import [Link].*;
public class BinaryToDecimal{
public static void main(String[] args) {
[Link]("Enter binary number: ");
Scanner sc = new Scanner([Link]);
String str = [Link]();
int n = [Link]();
int sum = 0;
for(int i=0; i<n; i++){
int num = [Link](n-i-1) - '0';
int pow = 1;
for(int j=0; j<i; j++){
pow *= 2;
}
sum += num*pow;
}
[Link]("The decimal number is "+ sum);
}
}

Chirag Nikant Simepurushkar 24B-CO-015 Page No:


GOA COLLEGE OF ENGINEERING
“Bhausaheb Bandodkar Technical Education Complex Farmagudi”

Output:

4)​ Digit Sum: Accept a large number and calculate the sum of all its digits using a
while loop.

Code:
import [Link];
public class DigitSum{
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter the Big number: ");
int n = [Link]();
int val = n;
int sum = 0;
while (val != 0) {
int rem = val % 10;
sum += rem;
val /= 10;
}

[Link]("The sum of digits = "+sum);


}
}

Output:

Chirag Nikant Simepurushkar 24B-CO-015 Page No:


GOA COLLEGE OF ENGINEERING
“Bhausaheb Bandodkar Technical Education Complex Farmagudi”

5)​ Leap Year Plus: Check if a year is a leap year and print the next leap year that
will occur.

Code:
import [Link];
public class LeapYear {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter year to check if its leap year: ");
int year = [Link]();
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
[Link]("Entered year is a leap year " + year);
} else {
[Link]("Entered year is not a leap year " + year);
}
year++;
while (true) {
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
break;
}else {
year++;
}
}
[Link]("The next leap year is in the year "+year);
}
}

Output:

Conclusion: Successfully implemented basic java programs using the control structures.

Chirag Nikant Simepurushkar 24B-CO-015 Page No:

You might also like