[go: up one dir, main page]

0% found this document useful (0 votes)
13 views5 pages

Expeiment 1

This document contains 6 Java programs that demonstrate basic programming concepts: 1) A program to calculate the factorial of a number. 2) A program that finds odd and even numbers in an integer array. 3) A program that displays the first n odd numbers, calculates their sum and average. 4) A program that checks if a number is a palindrome. 5) A program that calculates the average of 3 double numbers. 6) A program that determines the number of days in a given month.

Uploaded by

Nesamanikandan S
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)
13 views5 pages

Expeiment 1

This document contains 6 Java programs that demonstrate basic programming concepts: 1) A program to calculate the factorial of a number. 2) A program that finds odd and even numbers in an integer array. 3) A program that displays the first n odd numbers, calculates their sum and average. 4) A program that checks if a number is a palindrome. 5) A program that calculates the average of 3 double numbers. 6) A program that determines the number of days in a given month.

Uploaded by

Nesamanikandan S
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/ 5

NESAMANIKANDAN S

953622104068

EXPERIMENT-1
Basic Java Programs

1.Factorial of the number


import java.util.Scanner;
class Factorial
{
public static void main(String[]args)
{
int num=10;
int factorial=1;
for(int i=1;i<=num;i++)
{
factorial*=i;
}
System.out.println("Factorial of the number is:"+num+factorial);
}
}

OUTPUT:
Factorial of the number is:103628800

2. Find Odd or Even number in an array of integers.


import java.util.Scanner;
class Oddeven
{
public static void main(String[]args)
{
int a[]={22,15,10,46,57};
System.out.println("Odd numbers:");
for(int i=0;i<a.length;i++)
{
if(a[i]%2!=0)
{

1
NESAMANIKANDAN S
953622104068

System.out.println(a[i]);
}
}
System.out.println("Even number:");
for(int i=0;i<a.length;i++)
{
if(a[i]%2==0)
{
System.out.println(a[i]);
}
}
}
}
OUTPUT:
Odd numbers:
15
57
Even number:
22
10
46

3. Write a program in Java to display the first n odd numbers,


calculate its total and average.

import java.util.Scanner;
class Sum
{
public static void main(String[] args)
{
Scanner s=new Scanner(System.in);
int sum=0;
System.out.print("Enter the number:");
int n=s.nextInt();
int a=n;
for(int i=1;n!=0;i=i+2)
{
sum=sum+i;

2
NESAMANIKANDAN S
953622104068

n--;
}
System.out.println("The sum of first odd numbers is:"+sum);
float avg=sum/a;
System.out.println("The average is :"+sum+avg);
}
}
OUTPUT:
Enter the number:4
The sum of first odd numbers is :16
The average is :164.0

4.Palindrome
import java.util.Scanner;
class Palindrome
{
public static void main(String[]args)
{
int num,reverse=0;
System.out.println("Enter the number:");
Scanner s=new Scanner(System.in);
num=s.nextInt();
int remainder;
int original=num;
while(num!=0)
{
remainder=num%10;
reverse=reverse*10+remainder;
num=num/10;
}
if(original==reverse)
{
System.out.println("Palindrome");
}
else
{
System.out.println("Not Palindrome");
}
}
}
3
NESAMANIKANDAN S
953622104068

OUTPUT:
Enter the number:
343
Palindrome

5.Double
import java.util.Scanner;
class Double
{
public static void main(String[] args)
{
Scanner s=new Scanner(System.in);
System.out.print("Enter the first number:");
double n1=s.nextInt();
System.out.print("Enter the second number:");
double n2=s.nextInt();
System.out.print("Enter the third number:");
double n3=s.nextInt();
double sum=n1+n2+n3;
double average=sum/3.0;
System.out.println("The average value is "+average);
}
}
OUTPUT:
Enter the first number:12
Enter the second number:22
Enter the third number:7
The average value is 13.66

6.Calculate the number of Days in the month

import java.util.Scanner;
class month
{
public static void main(String[] args)
{
Scanner s=new Scanner(System.in);
System.out.print("Enter the month number:");
int a=s.nextInt();
switch(a)
4
NESAMANIKANDAN S
953622104068

{
case 1: case 3: case 5: case 7: case 8: case 10: case 12:
System.out.println("Month"+a+" has 31 days..");
break;
case 4: case 6: case 9: case 11:
System.out.println("Month "+a+" has 30 days..");
break;
case 2:
System.out.println("Month"+a+" has 28 or 29 days..");
break;
default:
System.out.println("Invalid month..");

}
}
}

OUTPUT:
Enter the month number:7
Month7 has 31 days..

You might also like