0% found this document useful (0 votes)
64 views14 pages

Java Programs: Chapter 1: Basic Syntactical Construct in Java

The document discusses 13 Java programs that cover basic programming concepts like calculating the sum of digits, reversing a number, generating the Fibonacci series, checking if a number is even/odd/prime, checking if a number is Armstrong/palindrome, calculating percentage and grade, finding numbers divisible by 7, counting even digits, bitwise operators, converting between Celsius and Fahrenheit, and calculating factorials. Code examples and sample outputs are provided for each program.

Uploaded by

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

Java Programs: Chapter 1: Basic Syntactical Construct in Java

The document discusses 13 Java programs that cover basic programming concepts like calculating the sum of digits, reversing a number, generating the Fibonacci series, checking if a number is even/odd/prime, checking if a number is Armstrong/palindrome, calculating percentage and grade, finding numbers divisible by 7, counting even digits, bitwise operators, converting between Celsius and Fahrenheit, and calculating factorials. Code examples and sample outputs are provided for each program.

Uploaded by

Raj Debadwar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Java Programs

Chapter 1: Basic syntactical construct in Java


1. WAP to Calculate sum of digits of a Number.
Code:
import java.util.*;
class digadd {
public static void main(String arg[]) {
int no,r,sum=0;
Scanner obj=new Scanner(System.in);
System.out.print("\nEnter a number : ");
no=obj.nextInt();
while(no!=0) {
r=no%10;
sum=sum+r;
no=(no-r)/10;
}
System.out.print("\nAddition of digits of entered number : "+sum);
}
}

Output:
2. WAP to reverse the number:
Code:
import java.util.*;
class reverse {
public static void main(String arg[]) {
int no,r,rev=0;
Scanner obj=new Scanner(System.in);
System.out.print("\nEnter a number : ");
no=obj.nextInt();
while(no!=0) {
r=no%10;
rev=(rev*10)+r;
no=(no-r)/10;
}
System.out.print("\nAddition of digits of entered number : "+rev);
}
}

Output:
3. WAP a program to generate fibonacci series.
Code:
import java.util.*;
class fibo {
public static void main(String arg[]) {
int no1,no2,no3,l,i;
Scanner obj=new Scanner (System.in);
System.out.print("\nEnter the length of fibonacci series : ");
l=obj.nextInt();
no1=0;
no2=1;
System.out.print("\nFibbonacci Series : "+no1+"\t"+no2);
for(i=2;i<l;i++) {
no3=no1+no2;
System.out.print("\t"+no3);
no1=no2;
no2=no3;
}
}
}
Output :
4. WAP to check number is even or odd.
Code:
import java.util.*;
class evenodd {
public static void main(String arg[]) {
int no;
Scanner obj=new Scanner (System.in);
System.out.print("\nEnter the number : ");
no=obj.nextInt();
if(no%2==0) {
System.out.print("\nEntered number "+no+" is an even
number");
}
else {
System.out.print("\nEntered number "+no+" is an odd
number");
}
}
}
Output:
5. WAP to check number is prime or not.
Code:
import java.util.*;
class Prime {
public static void main(String arg[]) {
int no,flag=0;
Scanner obj=new Scanner(System.in);
System.out.print("\nEnter the number : ");
no=obj.nextInt();
for(int i=2;i<=no/2;i++) {
if(no%i==0) {
flag=1;
break;
}
}
if(flag==1) {
System.out.print("\nEntered number "+no+" is not a prime
number");
}
else {
System.out.print("\nEntered number "+no+" is a prime
number");
}
}
}
Output:
6.Write a program to check whether number is Armstrong or not.
Code:
import java.util.*;
class Arm {
public static void main(String arg[]) {
int no,no1=0,r,temp;
Scanner obj=new Scanner(System.in);
System.out.print("\nEnter the number : ");
no=obj.nextInt();
temp=no;
while(no!=0) {
r=no%10;
no1=no1+(r*r*r);
no=(no-r)/10;
}
if(temp==no1)
System.out.print("\nEntered number "+temp+" is an armstrong
number");
else
System.out.print("\nEntered number "+temp+" is not an
armstrong number");
}
}

Output:

6.Write a program to check whether an entered number is palindrome or not.


Code:
import java.util.*;
class Pale {
public static void main(String arg[]) {
int no,r,rev=0,temp;
Scanner obj=new Scanner(System.in);
System.out.print("\nEnter a number : ");
no=obj.nextInt();
temp=no;
while(no!=0) {
r=no%10;
rev=(rev*10)+r;
no=(no-r)/10;
}
if(temp==rev)
System.out.print("\nEntered number "+temp+" is a
palendrome number");
else
System.out.print("\nEntered number "+temp+" is not a
palendrome number");
}
}
Output:

8.A program to calculate percentage for 4 subjects and decide the grade.
Code:
import java.util.*;
class MPercent {
public static void main(String arg[]) {
int s[]=new int[5];
int p,sum=0,i;
Scanner obj=new Scanner (System.in);
System.out.print("\nEnter the marks of 5 subjects : ");
for(i=0;i<5;i++) {
s[i]=obj.nextInt();
}
for(i=0;i<5;i++) {
sum=sum+s[i];
}
p=(sum/5);
if(p>90) {
System.out.print("\nGrade : A1");
}
else {
if(p>80) {
System.out.print("\nGrade : A2");
}
else {
if(p>70) {
System.out.print("\nGrade : B1");
}
else {
if(p>60) {
System.out.print("\nGrade : B2");
}
else {
if(p>50) {
System.out.print("\nGrade :
C1");
}
else {
if(p>35) {
System.out.print("\
nGrade : C2");
}
else {
System.out.print("\
nFail!!!\nTry next time");
}
}
}
}
}
}
}
}
Output:

9.Write a program to print all numbers from 100 to 200 divisible by 7.


Code:
import java.util.*;
class div7
{
public static void main(String arg[])
{
System.out.print("\nNumbers divisible by 7 between 100 and 200 :
\n");
for(int i=100;i<=200;i++)
{
if(i%7==0)
{
System.out.print("\t"+i);
}
}
}
}
Output:

10.A program to input a long number from keyboard and count total number of
even digits in it.
Code:
import java.util.*;
class {
public static void main(String arg[]) {
long no,r,count=0;
Scanner obj=new Scanner(System.in);
System.out.print("\nEnter the long number : ");
no=obj.nextInt();
while(no!=0) {
r=no%10;
if(r%2==0) {
count++;
}
no=(no-r)/10;
}
System.out.print("\nTotal even digits in entered number :
"+count);
}
}
Output:

11.Write a program for bitwise operator.


Code:
import java.util.*;
class bit {
public static void main(String arg[]) {
int no,no1;
Scanner obj=new Scanner(System.in);
System.out.print("\nEnter the number : ");
no=obj.nextInt();
no1=no>>1;
System.out.print("\nEntered number : "+no);
System.out.print("\nNumber divided by two : "+no1);
}
}
Output:

12.A program to convert C˚ to F˚ using formula C=(F-32)/1.8


Code:
import java.util.*;
class CF {
public static void main(String arg[]) {
double C,F;
Scanner obj=new Scanner(System.in);
System.out.print("\nEnter the temperature in Celcius : ");
C=obj.nextInt();
F=(C*1.8)+32;
System.out.print("\nTemoeratur in Farenheit : "+F);
}
}
Output:

13.Write a program to find factorial of number.


Code:
import java.util.*;
class fact {
public static void main(String arg[]) {
int no,i,f=1;
Scanner obj=new Scanner (System.in);
System.out.print("\nEnter the number : ");
no=obj.nextInt();
for(i=no;i>0;i--)
{
f=f*i;
}
System.out.print("\nFactorial of number ="+f);
}
}
Output:

You might also like