Monday, 30 November 2020

PROGRAM TO ILLUSTRATE INFINITE for LOOP IN JAVA

 // //ILLUSTRATION 1

class NULL_LOOP

{

   public static void main(String args[])

   {

       int i;

       for(i=1;i<=10;i--)

       System.out.println("Hello");

   }

}


/*

//ILLUSTRATION 2

class NULL_LOOP

{

   public static void main(String args[])

   {

       int i;

       for(i=1;;i--)

       System.out.println("Hello");

   }

}

*/

/*

//ILLUSTRATION 3

class NULL_LOOP

{

   public static void main(String args[])

   {

       int i;

       for(;;)

       System.out.println("Hello");

   }

}

*/

Write a Program in Java to accept a Word Or Sentence and print the Second letter.

 import java.util.*;

 class Number

 {

     public static void main(String args[])

     {

         Scanner in=new Scanner(System.in);

         String s;

         System.out.println("Enter a Word or Sentence");

         s=in.nextLine();

         System.out.println("The Second Digit: "+s.charAt(1));

     }

 }

Saturday, 21 November 2020

Write a program in C++ to Create a Single Dimensional Array of n integers. Print only the Armstrong numbers from the Array.

 #include<iostream>

using namespace std;

int main()

    {

        int n,n1,d,cube,sum,i;

        cout<<"Enter the size of the array\n";

        cin>>n;

        int a[n];

        cout<<"Enter "<<n<<" Numbers\n";

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

        cin>>a[i];

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

        {

         n1=a[i];

         sum=0;

         while(a[i]>0)

        {

          d=a[i]%10;

          cube=d*d*d;

          sum=sum+cube;

          a[i]=a[i]/10;

        }

        if(sum==n1)

        cout<<n1<<" is an Armstrong Number\n";

        else

        cout<<n1<<" is not an Armstrong Number\n";

        }

    }

Write a program in Java to Create a Single Dimensional Array of n integers. Print only the Armstrong numbers from the Array.

 /* Armstrong Number: A positive number is called armstrong number if it is equal to the sum of cubes of its digits for example 0, 1, 153, 370, 371, 407 etc.

Let's try to understand why 153 is an Armstrong number.

153 = (1*1*1)+(5*5*5)+(3*3*3)  
where:  
(1*1*1)=1  
(5*5*5)=125  
(3*3*3)=27  
So:  
1+125+27=153  

*/

import java.util.*;

class ArmStrong

{

    public static void main(String args[])

    {

        Scanner in=new Scanner(System.in);

        int n,n1,d,cube,sum,i;

        System.out.println("Enter the size of the array");

        n=in.nextInt();

        int a[]=new int[n];

        System.out.println("Enter "+n+" Numbers");

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

        a[i]=in.nextInt();

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

        {

         n1=a[i];

         sum=0;

         while(a[i]>0)

        {

          d=a[i]%10;

          cube=d*d*d;

          sum=sum+cube;

          a[i]=a[i]/10;

        }

        if(sum==n1)

        System.out.println(n1+" is an Armstrong Number");

        else

        System.out.println(n1+" is not an Armstrong Number");

        }

    }

}


Friday, 20 November 2020

Write a program in C++ to input a Number and Check whether it is Armstrong or not.

 /* Armstrong Number: A positive number is called armstrong number if it is equal to the sum of cubes of its digits for example 0, 1, 153, 370, 371, 407 etc.

Let's try to understand why 153 is an Armstrong number.

153 = (1*1*1)+(5*5*5)+(3*3*3)  
where:  
(1*1*1)=1  
(5*5*5)=125  
(3*3*3)=27  
So:  
1+125+27=153  

*/

#include<iostream>

using namespace std;

int main()

{

        int n,n1,d,cube,sum=0;

        cout<<"Enter a Number\n";

        cin>>n;

        n1=n;

        while(n>0)

        {

          d=n%10;

          cube=d*d*d;

          sum=sum+cube;

          n=n/10;

        }

        if(sum==n1)

        cout<<n1<<" is an Armstrong Number";

        else

        cout<<n1<<" is not an Armstrong Number";

}

Write a program in Java to input a Number and Check whether it is Armstrong or not.

/* Armstrong Number: A positive number is called armstrong number if it is equal to the sum of cubes of its digits for example 0, 1, 153, 370, 371, 407 etc.

Let's try to understand why 153 is an Armstrong number.

153 = (1*1*1)+(5*5*5)+(3*3*3)  
where:  
(1*1*1)=1  
(5*5*5)=125  
(3*3*3)=27  
So:  
1+125+27=153  

*/

import java.util.*;

public class ArmStrong

{

    public static void main(String args[])

    {

        Scanner in=new Scanner(System.in);

        int n,n1,d,cube,sum=0;

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

        n=in.nextInt();

        n1=n;

        while(n>0)

        {

          d=n%10;

          cube=d*d*d;

          sum=sum+cube;

          n=n/10;

        }

        if(sum==n1)

        System.out.println(n1+" is an Armstrong Number");

        else

        System.out.println(n1+" is not an Armstrong Number");

    }

}






















Thursday, 19 November 2020

Write a Program in C++ to accept a word and check whether it is Palindrome or not. (Without ignoring the case)

#include<iostream>

using namespace std;

main()

{

  string s,rev=""; int i, len;

  cout<<"Enter a Word\n";

  cin>>s;

  len=s.length()-1;

  for(i=len;i>=0;i--)

  {

   rev=rev+s.at(i);

  }

  if(rev==s)

    cout<<s<<" is a Palindrome Word";

  else

     cout<<s<<" is not a Palindrome Word";

 }

Write a Program in Java to accept a word and check whether it is Palindrome or not.

 import java.util.*;

class string2

{

public static void main(String args[])

{

Scanner in=new Scanner(System.in);

String s,rev=""; int i,len;

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

s=in.next();

len=s.length()-1;

for(i=len;i>=0;i--)

{

rev=rev+s.charAt(i);

}

if(rev.equalsIgnoreCase(s))

    System.out.println(s+" is a Palimdrome Word");

else

    System.out.println(s+" is not a Palimdrome Word");

}

}

Write a Program in Java to Accept a Sentence and Print only the first word.

import java.util.*;

public class string2

{

 public static void main(String args[])

 {

  Scanner in=new Scanner(System.in);

  String s; int i;

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

  s=in.nextLine();

  for(i=0;i<s.length();i++)

  {

   if(s.charAt(i)==' ')

   break;

   else

   System.out.println(s.charAt(i));

  }

 }

}

Program in Java to accept a String and Print its First and last Character

import java.util.*;

class string

{

public static void main(String args[])

{

Scanner in=new Scanner(System.in);

String s;

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

s=in.nextLine();

System.out.println("The First Letter: "+s.charAt(0));

System.out.println("The Last Letter: "+s.charAt(s.length()-2);

}

}

Write a program in C++ to Accept, add and Print numbers in a 3*4 matrix in a Double Dimensional Array.

 #include<iostream>

using namespace std;

int main()

{

        int i,j,sum=0;

        int a[3][4];

        cout<<"Enter 10 numbers\n";

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

        {

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

        cin>>a[i][j];

        }

        cout<<"The numbers are:\n";

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

        {

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

        cout<<a[i][j]<<" ";

        cout<<endl;

        }

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

        {

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

        sum=sum+a[i][j];

        }

        cout<<"The Sum of the elements in the Matrix is: "<<sum;

    }

Write a program in Java to Accept, Add and Print numbers in a 3*4 matrix in a Double Dimensional Array.

 import java.util.*;

public class DDArray

{

    public static void main(String args[])

    {

        Scanner in=new Scanner(System.in);

        int i,j,sum=0;

        int a[][]=new int[3][4];

        System.out.println("Enter 10 numbers");

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

        {

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

        a[i][j]=in.nextInt();

        }

        System.out.println("The numbers are:");

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

        {

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

        System.out.print(a[i][j]+" ");

        System.out.println();

        }

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

        {

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

        sum=sum+a[i][j];

        }

        System.out.println("The Sum of the elements in the Matrix is: "+sum);

    }

}

Write a program in Java to Accept and Print numbers in a 3*4 matrix in a Double Dimensional Array.

import java.util.*;

public class DDArray

{

    public static void main(String args[])

    {

        Scanner in=new Scanner(System.in);

        int i,j;

        int a[][]=new int[3][4];

        System.out.println("Enter 10 numbers");

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

        {

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

        a[i][j]=in.nextInt();

        }

        System.out.println("The numbers are:");

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

        {

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

        System.out.print(a[i][j]+" ");

        System.out.println();

        }

    }

}

Wednesday, 18 November 2020

Java program to accept a String and display the number of Vowels and Number of constants present in it.

import java.util.*;

public class STRING

{

public static void main(String args[])

{

Scanner in=new Scanner (System.in);

String s="",s1; int Length,i,vowel=0,constant=0;

char ch;

s1=s.toLowerCase();

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

s=in.nextLine();

Length=s.length();

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

{

ch=s.charAt(i);

if(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u')

vowel++;

else 

constant++;

}

System.out.println("The Number of Vowels in "+s+" is "+vowel);

System.out.println("The Number of Constants in "+s+" is "+constant);

}

}

Tuesday, 17 November 2020

C++ Program for Arithmetic operations

#include <iostream>

using namespace std;

main()

{

  int a,b;

  cout<<"Enter 2 numbers"<<endl;

  cin>>a>>b;

  cout<<"Addition= "<<a+b<<endl<<"Subtraction= "<<a-b<<endl<<"Multiplication= "<<a*b<<endl<<"Division= "<<a/b<<endl<<"Modulus Division="<<a%b;

}


Write a Program in Java to accept a String and find its length

import java.util.*;

public class Main

{

public static void main(String args[])

{

Scanner in=new Scanner (System.in);

String s; int Length;

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

s=in.next();

Length=s.length();

System.out.println("The length of "+s+" is "+Length);

}

}

Program in Java to accept a Word and Display its Reverse.

import java.util.*;

public class Reverse

{

public static void main(String args[])

{

Scanner in=new Scanner (System.in);

String s,rev=""; int bl,i;

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

s=in.next();

s=s+" ";

bl=s.indexOf(' ');

for(i=bl;i>=0;i--)

rev=rev+s.charAt(i);

rev=rev.trim();

System.out.println("The Reversed Number:\n"+rev);

}

}

C++ Program to accept a Number and check whether it is Palindrome or not.

/* A palindrome number is a number that is same after reverse. For example 545, 151, 34543, 343, 171, 48984 are the palindrome numbers. */

#include <iostream>

using namespace std;

int main()

{
int N,r=0,d,Num;
cout<<"Enter a Number to check whether it is Palindrome or not\n";
cin>>N;
Num=N;
while(N>0)
{
d=N%10;
r=r*10+d;
N=N/10;
}
if(r==Num)
cout<<Num<<" is a Palindrome Number";
else
cout<<Num<<" is not a Palindrome Number";
}

Java Program to accept a number and check whether it is Palindrome or not.

/* A palindrome number is a number that is same after reverse. For example 545, 151, 34543, 343, 171, 48984 are the palindrome numbers. */
import java.util.*;
public class Palindrome
{
public static void main(String args[])
{
Scanner in=new Scanner (System.in);
int N,r=0,d,Num;
System.out.println("Enter a Number to check whether it is Palindrome or not");
N=in.nextInt();
Num=N;
while(N>0)
{
d=N%10;
r=r*10+d;
N=N/10;
}
if(r==Num)
System.out.println(Num+" is a Palindrome Number");
else
System.out.println(Num+" is not a Palindrome Number");
}
}

Program in Java to accept 2 Words and concatenate and display them.

import java.util.*;

public class Concat

{

public static void main(String args[])

{

Scanner in=new Scanner(System.in);

String a,b;

System.out.println("Enter 2 Words");

a=in.next();

b=in.next();

System.out.println(a.concat(b)); //We can also write it as System.out.prinln(x+b);

}

}



Java program to find out the largest number out of 3 numbers entered by the user.

import java.util.*;

public class Largest

{

public static void main(String args[]) 

{

Scanner in=new Scanner(System.in);

int N1,N2,N3;

System.out.println("Enter 3 Numbers");

N1=in.nextInt();

N2=in.nextInt();

N3=in.nextInt();

if(N1>N2&&N1>N3)

System.out.println(N1+" is the Greatest Number");

else if(N2>N1&&N2>N3)

System.out.println(N2+" is the Greatest Number");

else 

System.out.println(N3+" is the Greatest Number");

}

}


C++ Program to find the largest number out of 3 numbers entered by the user.

#include <iostream>

using namespace std;

int main() 

{

int N1,N2,N3;

cout<<"Enter 3 Numbers"<<endl;

cin>>N1>>N2>>N3;

if(N1>N2&&N1>N3)

cout<<N1<<" is the Greatest Number";

else if(N2>N1&&N2>N3)

cout<<N2<<" is the Greatest Number";

else 

cout<<N3<<" is the Greatest Number";

}


C++ program to accept 10 numbers in an array and print the even numbers.

 #include <iostream>

using namespace std;

int main() 

{

int a[10];

int i;

cout<<"Enter 10 numbers\n";

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

cin>>a[i];

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

if(a[i]%2==0)

cout<<a[i]<<endl;

}


C++ Program to add 2 numbers by taking input from the user.

#include <iostream>

using namespace std;

int main() 

{

int a, b,sum;

cout<<"Enter 2 numbers\n";

cin>>a>>b;

sum=a+b;

cout<<"Sum= "<<sum<<"\n";

}


Friday, 13 November 2020

Menu Driven Program to find area of a Triangle as per the user's choice.

import java.util.*;
public class Triangle
{
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
double area=0.0D,s,b,a,m,n,p;
int choice;
System.out.println("Enter your Choice : 1 for Equilateral Triangle, 2 for Isosceles Triangle and 3 for Scalene Triangle");
choice=in.nextInt();
switch(choice)
{
case 1: System.out.println("Enter the side of the Equilateral Triangle");
s=in.nextDouble();
area=(Math.sqrt(3)/4)*s*s;
break;
case 2: System.out.println("Enter the side and base of the Isosceles Triangle");
a=in.nextDouble();
b=in.nextDouble();
area=(1.0/4.0)*b*Math.sqrt(4*a*a-b*b);
break;
case 3:System.out.println("Enter the sides of the scalene Triangle");
m=in.nextDouble();
n=in.nextDouble();
p=in.nextDouble();
s=(m+n+p)/2.0;
area=Math.sqrt(s*(s-m)*(s-n)*(s-p));
break;
default:System.out.println("Wrong Choice");
}
System.out.println("Area= "+area);
}
}

















 

Java Program on Conditional Statements



import java.util.*;
public class Discount
{
public static void main(String args[])
Scanner in=new Scanner(System.in);
int cost;
double amt=0.0,disc=0.0;
System.out.println("Enter the amount of purchase :");
cost=in.nextInt();
if(cost<=2000)
{
disc=5.0/100.0*cost;
System.out.println("Gift : Wall Clock");
}
if(cost>=2001&&cost<=5000)
{
disc=10.0/100.0*cost;
System.out.println("Gift : School Bag");
}
if(cost>=5001&&cost<=10000)
{
disc=15.0/100.0*cost;
System.out.println("Gift : Electric Iron");
}
if(cost>10000)
{
disc=20.0/100.0*cost;
System.out.println("Gift : Wrist Watch");
}
amt=cost-disc;
System.out.println("Amount of Purchase : Rs."+cost);
System.out.println("Discount : Rs."+disc);
System.out.println("Amount to be paid : Rs."+amt);
}
}












































































































Wednesday, 11 November 2020

Write a program in Java to generate the following pattern

 /* Pattern :

A

B B

C C C

D D D D

E E E E E

*/

public class Pattern_3

{

public static void main(String args[])

{

int i,j;

for(i=65;i<=69;i++)

{

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

System.out.print((char)i+" ");

System.out.println();

}

}

}

Java Program to find the sum of 2 numbers entered by the user using InputStreamReader Class

import java.io.*; 

public class Sum_IOStream

{

public static void main(String args[]) throws IOException

{

InputStreamReader read=new InputStreamReader(System.in);

BufferedReader in=new BufferedReader(read);

int N1,N2,Sum;

System.out.println("Enter 2 Numbers");

N1=Integer.parseInt(in.readLine());

N2=Integer.parseInt(in.readLine());

Sum=N1+N2;

System.out.println("The Sum of "+N1+" and "+N2+" is "+Sum);

}

}



Java Program to add 2 Numbers input by the user using Scanner Class

import java.util.*; 

public class Sum_scanner 

{

public static void main(String args[]) 

{

Scanner in=new Scanner(System.in);

int N1,N2,Sum;

System.out.println("Enter 2 Numbers");

N1=in.nextInt();

N2=in.nextInt();

Sum=N1+N2;

System.out.println("The Sum of "+N1+" and "+N2+" is "+Sum);

}

}

Write a Program to generate the following pattern

/* Pattern :

A

A B

A B C 

A B C D

A B C D E

*/

public class Pattern_2

{

public static void main(String args[])

{

int i,j;

for(i=65;i<=69;i++)

{

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

System.out.print((char)j+" ");

System.out.println();

}

}

}

Write a Program in Java to print the letters from A to Z along with their numeric values from 1 to 26 respectively.

public class Letters

{

public static void main(String[] args) 

{

    int i,val;

    for(i=65,val=1;i<=90;i++,val++)

    System.out.println((char)i+"\t"+val);

}

}


C++ Program to Display Tables

  /* Program to print tables in the following FORMAT

Input: 4

4 x 1 = 4

4 x 2 = 8

4 x 3 = 12

4 x 4 = 16

4 x 5 = 20

4 x 6 = 24

4 x 7 = 28

4 x 8 = 32

4 x 9 = 36

4 x 10 = 40

*/

#include<iostream>

using namespace std;

int main()

{

int n,i;

cout<<"Enter a Number";

cin>>n;

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

cout<<n<<" x "<<i<<" = "<<(i*n)<<endl;

}


Write a Program in Java to accept a set of any 10 characters. Calculate and print the sum of ASCII codes of the characters.

 import java.util.*;

public class ASCII_Sum

{

public static void main(String args[])

{

Scanner in=new Scanner(System.in);

char ch; int i,sum=0;

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

{

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

    ch=in.next().charAt(0);

    sum=sum+(int)ch;

}

System.out.println("The Sum of ASCII values of the entered Characters is: "+sum);

}

}

Java Program to Print Tables by taking input from the user

 /* Program to print tables in the following FORMAT

Input: 4

4 x 1 = 4

4 x 2 = 8

4 x 3 = 12

4 x 4 = 16

4 x 5 = 20

4 x 6 = 24

4 x 7 = 28

4 x 8 = 32

4 x 9 = 36

4 x 10 = 40

*/

import java.util.*;

public class Table

{

    public static void main(String args[])

    {

        Scanner in=new Scanner(System.in);

        int n,i;

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

        n=in.nextInt();

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

        System.out.println(n+" x "+i+" = "+(i*n));

    }

}

Java Program to Print Factorial of a Number entered by the user

 /* The factorial of a positive integer n, denoted by n!, is the product of all positive integers less than or equal to n */

import java.util.*;

public class FACTORIAL

{

        void Find()

    {

        Scanner in=new Scanner(System.in);

        int N,f=1,i;

        System.out.println("Enter a Number to find its Factorial");

        N=in.nextInt();

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

        f=f*i;

        System.out.println("The factorial of "+N+" is : "+f);

    }

}

Tuesday, 10 November 2020

First C++ Program

#include <iostream>

using namespace std;

main ()

{

    cout<<"Welcome to the World of C++"<<endl;

    cout<<"Hello";

}

TYPES OF COMMENTS IN JAVA

 TYPES OF COMMENTS IN JAVA

Comments are statements which do not get executed and are written to explain the program clearly to the users.

There are 3 ways of writing comments in Java:

1. Single line comment: These are used when a comment can be expressed in a single line.

Example:

//Comment

2. Multi-line Comment: These are used when the comments are more than one line.

Example:

/*Comment........................................................................................................................................................................................................................................................................Comment*/

3. Documentation Comment: During the course of programming, the user may need to include some information about himself and his program. This documentation may not be a discussion about the program logic but might serve some some information to be viewed by other users who may need to execute your program.

Example:

/**.................................................................................................................................................................................................................................................................................................................................................................................................................................................................................Comment*/




  

Java Program on Linear Search

 //Linear Search in an array of size 10

import java.util.*;

public class Linear_Search

{

    public static void main(String args[])

    {

        Scanner in=new Scanner(System.in);

        int m[]=new int[10];

        int search,i,k=0;

        System.out.println("Enter 10 elements");

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

        m[i]=in.nextInt();

        System.out.println("Enter the Search Element");

        search=in.nextInt();

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

        {

            if(m[i]==search)

            {

            k=1;

            break;

            }

        }    

        if(k==1)

        System.out.println(search+" is present in the array");

        else 

        System.out.println(search+" is not present in the array");

    }

}

Java String Pattern Program

/*Program to display the String entered by the user in the following pattern:
J
JA
JAV
JAVA
*/

import java.util.*;
public class Pattern1
{
    public static void main(String[] args)
    {
        Scanner in=new Scanner(System.in);
        System.out.println("Enter a String");
        String s=in.nextLine();
        int i,j; 
        
        for(i=0;i<=s.length()-1;i++)
        {
            for(j=0;j<=i;j++)
            System.out.print(s.charAt(j));
            System.out.println();
        }
        
    }
}

Java Program to find Prime Number

import java.util.*; //importing the package

public class Prime // creating the class

{

  public static void main(String args[])//creating the main Function

  {

    Scanner in = new Scanner(System.in); //creating the scanner object

    int n,i,c=0; //declaring and initializing the variables

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

    n=in.nextInt();// Taking input from the user

    for(i=1;i<=n;i++)// loop which runs from 0 till the entered number

      if(n%i==0)//checking whether the number is divisible or not

      c++; //counter variable to count the number of factors

      if(c==2)//checking whether there are only 2 factors

      System.out.println(n+" is a Prime Number");

      else 

      System.out.println(n+" is not a Prime Number");

  }

}

    

First Java Program (To Print Hello World)

 public class MyFirstProgram // Creating a Class

{

  public static void main(String args[])// Creating the main Function

  {

   System.out.println("Hello World");// To print Hello World

  }

}

Java Program to count the total number of characters in a string

 //Java Program to count the total number of characters in a string import java.util.*; public class CountChars {     public static void mai...