26. Program to find ASCII number and Value of entered character:
package sam2;
/* Program to find ASCII Number and Value of entered character
*
*/
import java.util.Scanner;
public class AsciiValue {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
System.out.println("Enter a Character");
char ch = sc.next().charAt(0);
int asciinumber = ch;
int asciivalue = (int)ch;
System.out.println("Ascii Number of "+ch+" = "+asciinumber);
System.out.println("Ascii Value of "+ch+" = "+asciivalue);
}
}
27. Program to convert Celcius to Fahrenheit.
package sam2;
/*Java Program to Convert Celsius to Fahrenheit
* By Rahul Kundu
* °F = °C * (9/5) + 32
*/
import java.util.Scanner;
public class Celcius_Farenhite {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
System.out.println("Enter the temperature in Celsius : ");
float c = sc.nextFloat();
// Converting Celsius to Fahrenheit
float f = c * (9.0f/5.0f) + 32;
System.out.println("The temperature is "+f+" degree Fahrenheit.");
}
}
28. Program to find factorial of a number:
package sam2;
/*
* Java Program to Find Factorial of a Number
*/
import java.util.Scanner;
public class Factorial {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
System.out.println("Enter value of n: ");
int num = sc.nextInt();
if(num < 0){
System.out.println("Factorial of negative numbers cannot be calculated.");
} else if(num == 0){
System.out.println("Factorial of 0 is 1.");
}
else{
long factorial = 1;
for(int i=num; i>1 ; i--){
factorial *= i;
}
System.out.println("Factorial of "+num+" is "+factorial);
}
}
}
29. Program to print fibonacci series.
package sam2;
import java.util.Scanner;
/*
* Java Program to Print Fibonacci Series Using Loops
*/
public class Fibonacci {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
System.out.println("Enter Value of n: ");
int n = sc.nextInt();
int nextValue = 0;
int first = 0;
int second = 1;
System.out.print("0, 1, ");
for(int i=3; i<=n ;i++){
nextValue = first+second;
first = second;
second = nextValue;
System.out.print(""+nextValue+", ");
}
}
}
30. Program to check if entered year is leap year or not:
package sam2;
/* Program to check whether entered year is Leap year or not
* By Rahul Kundu
*/
import java.util.Scanner;
public class LeapYear {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
System.out.println("Enter the Year : ");
int year = sc.nextInt();
boolean isLeap = false;
if( ((year%4 == 0)&&(year%10!=0))||(year%400 == 0)){
isLeap = true;
}else{
isLeap = false;
}
if(isLeap == true){
System.out.println(""+year+" is a Leap Year");
}else{
System.out.println(""+year+" is Not a Leap Year");
}
}
}