19 September 2018

C++ -Calculate perfect number


Perfect number : is a positive integers which is equal to the sum of its proper positive divisors, that is, the sum of its positive divisors excluding the number itself

(or)

Perfect number : is a positive integers which is equal to the sum of its proper positive factors, that is, the sum of its positive factors excluding the number itself

For example 
 
 6 has factors 1,2,3 and 6(number itself)

//C++ program to calculate perfect number
#include<iostream>
using namespace std;
  int main()
    {
    int n,sum=0;
    cout<<"Enter a number: ";
    cin>>n;   
    for(int i=1;i<n;i++)
        {
        if(n%i==0)
          sum+=i;
        }
    if(n==sum)
        cout<<n<<" is a Perfect number"<<endl;
    else
        cout<<n<<" is not Perfect number"<<endl;
return 0;
}
Similar program(s) :

C++ Program to find a given number is STRONG or not.
   

18 September 2018

C++ program to calculate x^n using recursion

//C++ program to calculate x^n using recursion
#include<iostream>
using namespace std;
int xn(int,int);
  int main()
    {
    int n,x,k;
    cout<<"Enter X value : ";
    cin>>x;
    cout<<"Enter N value : ";
    cin>>n;
    k=xn(x,n);
    cout<<"Power("<<x<<","<<n<<") = "<<k<<endl;
return 0;
}
    int xn(int x,int n)
   {
    int y=1;
    if(n>0 && n!=1)
       return (y*x)*xn(x,n-1);
    else if(n==0)
       return y;
    else
       return x*y;
   }
Output will be similar to this :



Similar program(s):
C++ program to calculate x power n(XN) 
   

C++ program to calculate x power n(x^n)

//C++ program to calculate x^n
#include<iostream>
using namespace std;
  int main()
    {
    int n,x,k;
    cout<<"Enter X value : ";
    cin>>x;
    cout<<"Enter N value : ";
    cin>>n;
    int y=1;
    for(int i=0;i<n;i++)
        {
        y=y*x;
        k=y;
        }
    cout<<"Power("<<x<<","<<n<<") = "<<k<<endl;
return 0;
}
Output will be similar to this :



Similar program(s):
C++ program to calculate x power n(X^N) using recursion
   

17 September 2018

C++ Program to check given string is palindrome without library function

 
A Palindrome is a word, number, or other sequence of 

characters which reads the same backward as forward
 
Example :

MOM,MADAM,MALAYALAM




//Program to check given string is palindrome (or) not
//Without string library function
#include<iostream>
using namespace std;
int main()
    {
    char s[50];
    int count=0;
    cout<<"\n enter a string : ";
    cin>>s;
    int i=0,j,is_palin=1;
    while(s[count]!='\0')
    {
     count++;
        }
    j=count-1;
    while(i<j)
        {
        if(s[i]!=s[j])
          {
            is_palin=0;
            break;
           }
        else
          {
            ++i;
            --j;
          }
        }
    if(is_palin)
        cout<<"\n It is palindrome"<<endl;
    else
        cout<<"\n It is not palindrome"<<endl;
return 0;
}
Similar program(s):
C++ Program to check given string is palindrome (or) not.

C++ Program to print prime factors of given number

//Program to print prime factors of given number
#include<iostream>
using namespace std;
int main()
     {
    int a,is_prime;
    cout<<"Enter a number to find its Prime factors : ";
    cin>>a;
    cout<<"\n Prime factors of "<<a<<endl;
    for(int i=2;i<=a;i++)
    {
      if(a%i==0)
       {
       is_prime=1;
    for(int j=2;j<i;j++)
           {
               if(i%j==0)
               is_prime=0;
            }
        if(is_prime)
           cout<<i<<endl;
      }
    }
cout<<endl;
return 0;
    }
Output will be similar to this:

Similar programs :
C++ Program to find a given number is Prime (or) not.
C++ Program to find prime number between 1 and n
C++ Program to find FACTORS of a given number

C++ program on passing array and its size as parameters

//program on passing array and its size as parameters
//displays the index value of minimum element
#include<iostream>
using namespace std;
int k;
int minindex(int a[],int n)
    {
    int mini=a[0];
    for(int i=0;i<n;i++)
       {
         if(mini>a[i])
        {
                   mini=a[i];
               k=i;//Storing index value
        }
      
        }
 return k;// Returning index value
    }
  int main()
    {
    int a[20],n,min;
    cout<<"How many numbers you want to enter"<<endl;
    cin>>n;
    cout<<"Enter a list of "<<n<<" integers"<<endl;
    for(int i=0;i<n;i++)
      {
        cin>>a[i];
      }
    min=minindex(a,n);//Passing(as parameters)array and its size
    cout<<"Minimum element is present at index "<<min;
    cout<<endl;
   return 0;
    }

14 September 2018

C++ Program to generate nth term in Fibonacci series

//Program to generate nth term in Fibonacci series
#include<iostream>
using namespace std;
int main()
      {
    int f1=1,f2=1,f3=0,n,i;
    cout<<"enter the term you want to find : ";
    cin>>n;
    if(n==1)
      cout<<f1;
    else if(n==2)
      cout<<f2;
    else if(n>=3)
         {
        for(i=3;i<=n;i++)
            {
            f3=f1+f2;
            f1=f2;
            f2=f3;
            }
        cout<<f3<<"\n";
         }
return 0;
      }
A similar program to this:
 C++ Program to generate Fibonacci series

12 September 2018

Java Program for concatenation of two strings

//Java Program for concatenation of two strings
import java.util.*;
class concatenate
   {
  public static void main(String args[])
    {
    String a="Welcome to ";   
    String b="Copymyprograms blog";
    System.out.println("String a :"+a);
    System.out.println("String b :"+b);
    String c=a+b;//Java automatically adds two strings using + operator
    System.out.println("After concatenation \n String c :"+c);
    }
 }

Output will be similar to this :

A similar program to this :

 Java program to compare two strings

Java program to compare two strings

//Java program to compare two strings
import java.util.*;
class string1
   {
  public static void main(String args[])
    {
    String a="Welcome to Java";
    String b="Welcome to C++";
    String c="Welcome to Java";
   
    //comparing strings a and b
    if(a.equals(b))
      System.out.println("\t "+a+" and "+b+" are equal ");
    else
      System.out.println("\t "+a+" and "+b+" are not equal");
   
    //comparing strings a and c
    if(a.equals(c))
      System.out.println("\t "+a+" and "+c+" are equal ");
    else
      System.out.println("\t "+a+" and "+c+" are equal ");
    }
 }
Output will be similar to this :











A similar program to this :

Java Program for concatenation of two strings

11 September 2018

Java Program to generate nth term in Fibonacci series

//Program to generate nth term in Fibonacci series
import java.util.*;
class nfibo
     {
   public static void main(String args[])
      {
    int f1=1,f2=1,f3=0,n,i;
    System.out.print("enter the term you want to find : ");
    Scanner obj = new Scanner(System.in);
    n=obj.nextInt();
    if(n==1)
      System.out.println(f1);
    else if(n==2)
       System.out.println(f2);
    else if(n>=3)
         {
        for(i=3;i<=n;i++)
            {
            f3=f1+f2;
            f1=f2;
            f2=f3;
            }
        System.out.println(f3);
         }
        }
      }
A similar program to this
 Java Program to generate Fibonacci series

Java Program to calculate area of circle using SWITCH statement

//Program to calculate area of circle using SWITCH statement
import java.util.*;
class switch1
     {
public static void main(String args[])
      {
    float r,d,area;
    int n;
    System.out.println(" You want to calculate area of circle using");
    System.out.println(" 1.Radius \n 2.Diameter");
    System.out.println(" Enter your choice:");
    Scanner obj=new Scanner(System.in);
    n=obj.nextInt();
    switch(n)
      {
    case 1:
          {
        System.out.println(" Enter radius:");
        r=obj.nextFloat();       
        System.out.println(" Area of circle = "+3.14*2*r);
        break;
          }
    case 2:
          {
        System.out.println(" Enter diameter:");
        d=obj.nextFloat();       
        System.out.println(" Area of circle = "+3.14*d);
        break;
          }
    default:
        System.out.println(" Invalid choice ");
     }
       }
     }
A similar program to this
Java Program to calculate area of circle using if-else statement

08 September 2018

Java Program to find word count in a given text

//Program to find word count in a given text
import java.util.*;
class wcount
     {
   public static void main(String args[])
      {
    String s;
    int i,w=1;
    System.out.println(" Enter text : ");
    Scanner obj=new Scanner(System.in);
    s=obj.nextLine();
    char a[]=s.toCharArray();
    if(a.length==0)
       w=0;
    else
      {
    for(i=0;i<a.length;i++)
       {
        if(a[i]==' '||a[i]=='\0')
            w++;
       }
      }
    System.out.println("\n Word count is : "+w);
    System.out.println();
     }
}
The Output will be similar to this


 when you don't enter anything













           

Java Program to find frequency count of letters in a given text

//Program to find frequency count of letters in a given text
import java.util.*;
class fcount
     {
   public static void main(String args[])
      {
    char ch;
    String s;
    int i,f=0;
    System.out.println(" Enter a string : ");
    Scanner obj=new Scanner(System.in);
    s=obj.nextLine();
    char a[]=s.toCharArray();
    System.out.println("Enter a character to find its frequency : ");
    ch=obj.next().charAt(0);
    for(i=0;i<a.length;i++)
       {
        if(ch==a[i])
            f++;
       }
    System.out.println("\n Frequency count of "+ch+" is : "+f);
    System.out.println();
     }
}
The Output will be similar to this

 when you enter a character which is not entered in the string

Similar program:-
Java Program to find word count in a given text



           

Java Program to check given string is palindrome (or) not.

//Program to check given string is palindrome (or) not.
import java.util.*;
class palindrome
     {
   public static void main(String args[])
      {
        String s;
    Scanner in=new Scanner(System.in);
    System.out.println(" Enter your text: ");
    s=in.nextLine();
    char a[]=s.toCharArray();//converting string to array.
        int i=0,is_palin=1,j=a.length;//j stores the length of array.
        for(i=0,j=j-1;i<j;i++,j--)
          {
           if(a[i]!=a[j])
             {
               is_palin=0;
               break;
             }
          }
    if(is_palin==1)
       {
        System.out.println("It is palindrome");
       }
    else
        {
          System.out.println(" It is not palindrome");
        }
      }
    }

The Output will be similar to this
   when you enter WELCOME and MADAM


Java Program to sort a list of names in Descending order

//Program to sort a list of names in Descending order
import java.util.*;
class Descendnames
     {
public static void main(String args[])
      {
    int n,i,j;
    System.out.println(" How many names you want to sort in Descending order:");
    Scanner obj=new Scanner(System.in);
    n=obj.nextInt();
    String temp,a[]=new String[n];//Declaring array of Strings
    Scanner obj1=new Scanner(System.in);
    System.out.println(" Enter names");
    for(i=0;i<n;i++)
        {
        a[i]=obj1.nextLine();
        }
    System.out.println(" Unsorted names : ");
    for(i=0;i<n;i++)
          {
        System.out.println(a[i]);
          }
    for (i = 0; i < n; i++)
            {
            for (j = i + 1; j < n; j++)
             {
                if (a[i].compareTo(a[j])<0) //compareTo() method is used for  comparing two //strings lexicographically.
                {
                    temp = a[i];
                    a[i] = a[j];
                    a[j] = temp;
                }
            }
        }
    System.out.println("\n Sorted names in Descending order ");
    for(i=0;i<n;i++)
          {
        System.out.println(a[i]);
          }
     System.out.println( );
      }
    }
The output will be similar to this


Java Program to sort a list of names in ascending order

//Program to sort a list of names in ascending order
import java.util.*;
class Ascendnames
     {
public static void main(String args[])
      {
    int n,i,j;
    System.out.println(" How many names you want to sort in Ascending order:");
    Scanner obj=new Scanner(System.in);
    n=obj.nextInt();
    String temp,a[]=new String[n];//Declaring array of Strings
    Scanner obj1=new Scanner(System.in);
    System.out.println(" Enter names");
    for(i=0;i<n;i++)
        {
        a[i]=obj1.nextLine();
        }
    System.out.println(" Unsorted names : ");
    for(i=0;i<n;i++)
          {
        System.out.println(a[i]);
          }
    for (i = 0; i < n; i++)
            {
            for (j = i + 1; j < n; j++)
             {
                if (a[i].compareTo(a[j])>0) //compareTo() method is used for comparing two //strings lexicographically.
                {
                    temp = a[i];
                    a[i] = a[j];
                    a[j] = temp;
                }
            }
        }
    System.out.println("\n Sorted names in ascending order ");
    for(i=0;i<n;i++)
          {
        System.out.println(a[i]);
          }
     System.out.println( );
      }
    }
The output will be similar to this:


Java Program to illustrate method overloading

//Program to illustrate method overloading
import java.util.*;
class overload
     {
 
     public static void print(int a,int b)
    {
      System.out.println("Printing int values");
      System.out.println("Before swapping ");
      System.out.println("\t a = "+a+" b = "+b);
      int temp;
      temp=a;
      a=b;
      b=temp;
      System.out.println("After swapping ");
      System.out.println("\t a = "+a+" b = "+b);
    }
    public static void print(char a,char b)
    {
      System.out.println("Printing char values");
      System.out.println("Before swapping ");
      System.out.println("\t a = "+a+" b = "+b);
      char temp;
      temp=a;
      a=b;
      b=temp;
      System.out.println("After swapping ");
      System.out.println("\t a = "+a+" b = "+b);
    }
    public static void print(double a,double b)
    {
      System.out.println("Printing real values");
      System.out.println("Before swapping ");
      System.out.println("\t a = "+a+" b = "+b);
      double temp;
      temp=a;
      a=b;
      b=temp;
      System.out.println("After swapping ");
      System.out.println("\t a = "+a+" b = "+b);
    }
       public static void main(String args[])
        {
      System.out.println(" ");
      print(10,20);
      print('X','Y');
      print(10.2,12.5);
        }
     }

To convert from Upper case to lowercase(A to a)

//converting from Upper to lower(A to a) import java.util.*; class case { public static void main(String args[]) { System.out.pr...