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

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