3.1.21

JAVA program pattern 2

import java.util.Scanner;

public class pattern3

{

public static void main(String args[])

{

int i,j;

Scanner sc=new Scanner(System.in);

System.out.println("Enter the number");

int n=sc.nextInt();

for(i=0;i<n;i++)

{

for(j=n-i;j>1;j--)

{

System.out.print(" ");

}

for(j=0;j<=i;j++)

{

System.out.print("*");

}

System.out.println();

}

}

}


OUTPUT

Enter a number 

6

           *

         **

       ***

     ****

   *****

******

JAVA program for pattern 1

 import java.util.Scanner;

public class pattern1

{

public static void main(String args[])

{

int i,j;

Scanner sc=new Scanner(System.in);

System.out.println("Enter the number");

int n=sc.nextInt();

for(i=1;i<=n;i++)

{

for(j=1;j<=i;j++)


System.out.print("*");

System.out.println();

}

}

}

OUTPUT

Enter a number 

6

*
**
***
****
*****
******

JAVA program for Fibonacci

class Fibonacci

{

public static void main(String args[])

{

int n1=0,n2=1,n3,i,count=10;

System.out.println(n1+""+n2);

for(i=2;i<count;i++)

{

n3=n1+n2;

System.out.println(""+n3);

n1=n2;

n2=n3;

}

}

}

OUTPUT

01

1

2

3

5

8

13

21

34

JAVA program for ASCII values

import java.util.Scanner;

public class ascii 

{

public static void main(String args[])

{



System.out.println("Enter a character");

Scanner sc=new Scanner(System.in);

char ch=sc.next().charAt(0);

int asciivalue=ch;

System.out.println("the ascii value is:" +asciivalue);

}

}

OUTPUT
Enter a character 

a

the ascii value is 97


JAVA program for palindrome

import java.util.Scanner;

public class palindrome 

{

public static void main(String args[])

{

int i,sum=0,temp,r;

Scanner sc=new Scanner(System.in);

System.out.println("Enter the number");

int n=sc.nextInt();

temp=n;

while(n>0)

{

r=n%10;

sum=(sum*10)+r;

n=n/10;

}

if(temp==sum)

{

System.out.println("palindrome");

}

else

System.out.println("not palindrome");

}

}

OUTPUT
Enter the number 5454

not palindrome

JAVA program for factorial

import java.util.Scanner;

public class factorial 

{

public static void main(String args[])

{

int i,fact=1;

Scanner sc=new Scanner(System.in);

System.out.println("Enter the number");

int num=sc.nextInt();

for(i=1;i<=num;i++)

{

fact=fact*i;

}

System.out.println("Factorial of"+num+"is:" +fact);

}

}

OUTPUT

Enter the number

5

Factorial of 5 is 120

JAVA program for armstrong number

import java.util.Scanner;

public class armstrong 

{

public static void main(String args[])

{

int c=0,a,temp;

Scanner sc=new Scanner(System.in);

System.out.println("Enter the number");

int n=sc.nextInt();

temp=n;

while(n>0)

{

a=n%10;

n=n/10;

c=c+(a*a*a);

}

if(temp==c)

System.out.println("armstrong number");

else

System.out.println("not a armstrong number");

}

}

OUTPUT

Enter a number

6

not a armstrong number

JAVA program for prime

import java.util.Scanner;

public class prime

{

public static void main(String args[])

{

int i,m=0,flag=0;

Scanner sc=new Scanner(System.in);

System.out.println("Enter the number");

int n=sc.nextInt();

m=n/2;

if(n==0||n==1)

{

System.out.println(n+" is not a prime number");

}

else

{

for(i=2;i<=m;i++)

{

if(n%i==0)

{

System.out.println(n+"is not a prime  number");

flag=1;

break;

}

}

if(flag==0)

System.out.println(n+ "is prime number");

}

}

}


OUTPUT

Enter the number 5

5 is prime number

JAVA simple hello program

 class Simple

{

public static void main(String args[])

{

System.out.println("Hello Java");

}

}

OUTPUT

javac Simple.java

java Simple

Hello Java

How to run Java program

To run the java program first write the code in notepad and save it with the extension .java

 and then open command prompt and compile the program (eg : javac Simple.java) After the compilation, to run the java code we need to call the Java interpreter ,to call the java interpreter we use java command and u need to [ass the filename you know which contains the byte code(eg:- java Simple) 



  

C program to convert char into int and int to char

#include <stdio.h>

#include <stdlib.h>

 

int main()

{

    char ch='B';

    int n=ch;

    printf("The ASCII code of the character %c is : %d\n",ch,n);

 

    return 0;

}

OUTPUT

The ASCII code of the character B is : 66


c program to convert int to float point number

PROGRAM

#include <stdio.h>

#include <stdlib.h>

 

int main()

{

    int n1=2598;

    float n2=n1;

    printf("The float value is : %.2f",n2);

 

    return 0;

}

OUTPUT
The float value is: 2598.00


C program to check the given number is factor of first number or not

PROGRAM

#include <stdio.h>

#include <stdlib.h>

 

int main()

{

int n1,n2;

printf("Enter two numbers");

scanf("%d%d",&n1,&n2);

if(n1%n2==0)

    printf("second number is factor of first number");

else

    printf("second number is not factor of first number");

 

return 0;

}

OUTPUT

Enter two numbers 12 6

second number is factor of first number


JAVA program pattern 2

import java.util.Scanner; public class pattern3 { public static void main(String args[]) { int i,j; Scanner sc=new Scanner(System.in); Syste...