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


30.12.20

C PROGRAM TO PERFORM ALL RELATIONAL OPERATORS AVAILABLE IN C

    #include<stdio.h>

int main()

{

int m,n;

printf("Enter any two values");

scanf("%d%d",&m,&n);

if(m<n)

{

printf("%d is greater than %d",m,n);

}

else if(m<n)

{

printf("%d is less than %d",m,n);

}

else if(m==n)

{

printf("%d is equal to %d",m,n);

}


return 0;

OUTPUT:

Enter any two values 40 20

40 is greater than 20


C PROGRAM TO CHECK THE GIVEN YEAR IS LEAP YEAR OR NOT

 

#include <stdio.h>

#include <stdlib.h>

 

 

int main()

{

 

int year;

printf("enter a year");

scanf("%d",&year);

if(year%400==0)

    {

    printf("%d is a leap year",year);

}

else if(year%100==0)

{

    printf("%d is not leap year",year);

    }

    else if(year%4==0)

    {

 

        printf("%d is a leap year",year);

    }

    else

    {

        printf("%d is not leap year");

    }

return 0;

}

OUTPUT

Enter a year 2032

2032 is a leap year

C PROGRAM TO PERFORM ALL BITWISE OPERATORS AVAILABLE IN C

#include <stdio.h>

#include <stdlib.h>

 

 

int main()

{

 

int a,b;

int c=0;

printf("Enter a and b value ");

scanf("%d%d",&a,&b);

c=a&b;

printf("value of c is %d\n",c);

 

c=a|b;

printf("value of c is %d\n",c);

c=a^b;

printf("value of c is %d\n",c);

c=a<<2;

printf("value of c is %d\n",c);

c=a>>2;

printf("value of c is %d\n",c);

    return 0;

}

OUTPUT

Enter a and b value 60 13

Value of c is 12

Value of c is 61

Value of c is 49

Value of c is 240

Value of c is 15

c program to perform all increment and decrement operators available in c

 

INCREMENT OPERATOR

#include <stdio.h>

#include <stdlib.h>

 

 

int main()

{

 

int i=1;

while(i<10)

{

    printf("%d",i);

    i++;

    }

    return 0;

}

OUTPUT

123456789

DECREMENT OPERATOR

#include <stdio.h>

#include <stdlib.h>

 

 

int main()

{

 

int i=20;

while(i>10)

{

    printf("%d",i);

    i--;

    }

    return 0;

}

OUTPUT

20191817161514131211

C PROGRAM TO PERFORM ALL ASSIGNMENT OPERATORS AVAILABLE in c

#include <stdio.h>

#include <stdlib.h>

 

 

int main()

{

int a,c;

printf("enter a  value");

scanf("%d",&a);

c=a;

printf("value of c with operator = is %d\n",c);

c += a;

printf("value of c with operator += is %d\n",c);

c-=a;

printf("value of c with operator -= is %d\n",c);

c*=a;

printf("value of c with operator *= is %d\n",c);

c/=a;

printf("value of c with operator /= is %d\n",c);

c=200;

c%=a;

printf("value of c with operator %= is %d\n",c);

c<<=a;

printf("value of c with operator <<= is %d\n",c);

c>>=a;

printf("value of c with operator >>= is %d\n",c);

c&=a;

printf("value of c with operator &=  is %d\n",c);

c^=a;

printf("value of c with operator ^= is %d\n",c);

c|=a;

printf("value of c with operator |= = is %d\n",c);

 

    return 0;

}

OUTPUT

Enter a value 21

Value of c with operator = is 21

Value of c with operator += is 42

Value of c with operator -= is 21

Value of c with operator *= is 441

Value of c with operator /= is 21

Value of c with operator %= is 11

Value of c with operator <<= is 23068672

Value of c with operator >>= is 11

Value of c with operator &= is 1

Value of c with operator ^= is 20

Value of c with operator |= is 21

 


C PROGRAM TO PERFORM ALL LOGICAL OPERATORS AVAILABLE IN C

#include <stdio.h>

#include <stdlib.h>

 

int main()

{

int a,b,c;

printf("enter a and b values");

scanf("%d%d",&a,&b);

if(a&&b)

{

    printf("condition 1 is true ");

}

if(a||b)

{

    printf("condition 2 is true");

}

  return 0;

}

 

OUTPUT

Enter a and b values 0 10

Condition 2 is true

 


C PROGRAM TO PRINT SIZE ,MINIMUM VALUE,MAXIMUM VALUE AND PRECISION VALUE OF THE FLOAT USING SIZEOF(),FLT_MIN,FLT_MAX AND FLT_DIG.

 

#include <stdio.h>

#include <float.h>

 

int main()

{

    float a;

printf("the min value of float is %e\n",FLT_MIN);

printf("the max value of float is %e\n",FLT_MAX);

printf("the number of digits in the number is %e\n",FLT_DIG);

printf("the size of float is %lu\n",sizeof f(a));

 

    return 0;

}

OUTPUT

the min value of float is 1.175494e-38

the max value of float is 3.402823e+38

the number of digits in the number is 3.402823e+38

the size of float is 4

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...