on Leave a Comment

C++ Program to Find Prime Factors of a Number

This article contains C++ program to find prime factors of a number. 

Steps:

(1) Read a positive integer x (using cin>>x) and initialize a variable i to 2.
(2) While x is divisible by i, print i and divide x by i.
(3) After step 2, increment i.
(4) Repeat step 3 and 4, till x>1.

C++ Program to Find Prime Factors of a Number

#include<iostream>
using namespace std;
int main()
{
    int x;
    cout<<"Enter a positive integer : ";
    cin>>x;
    cout<<endl<<"Prime factors of "<<x<<" are : "<<endl;
    for(int i=2;x>1;i++)
    {
        while(x%i==0)
        {
            cout<<i;
            x=x/i;
            cout<<"\t";
        }
    }
    return 0;
}

OUTPUT 1:

Enter a positive integer : 150

Prime factors of 150 are :
2       3       5       5

OUTPUT 2:

Enter a positive integer : 36

Prime factors of 36 are :
2       2       3       3

OUTPUT 3:

Enter a positive integer : 63

Prime factors of 63 are :
3       3       7

on Leave a Comment

C++ Program to Check Leap Year

This article contains C++ program to check whether a year is leap year or not.

Leap year is a year comes after every four year containing 366 days including 29 February as an intercalary day.

A year is a leap year if :

1. Year is divided by 4 but not by 100.
2. Century year (1600, 2000 etc) is divided by 400 and 100.

C++ Program to Check Whether a Year is Leap Year or Not

#include<iostream>
using namespace std;
int main()
{
    int y;
    cout<<"Enter year : ";
    cin>>y;
    if((y%4==0 && y%100!=0) || (y%400==0))
    {
        cout<<endl<<y<<" is a leap year";
    }
    else
        cout<<endl<<y<<" is not a leap year";
    return 0;
}

OUTPUT 1:

Enter year : 2018

2018 is not a leap year


OUTPUT 2:

Enter year : 2008

2008 is a leap year


on Leave a Comment

C++ Program to Check Whether a Character is Alphabet or Not

This article contains C++ program to check whether a character is alphabet or not. A C++ compiler determines characters by its ASCII Values. The alphabetic characters have ASCII values between 65 to 92 or 97 to 122. Alphabets can be of either lowercase or uppercase. 

C++ program to check whether a character is alphabet or not

#include<iostream>
using namespace std;
int main()
{
    char n;
    cout<<"Enter a number : ";
    cin>>n;
    if(n>='a'&& n<='z' || n>='A' && n<='Z')
        cout<<endl<<n<<" is an alphabet";
    else
        cout<<endl<<n<<" is not an alphabet";
    return 0;
}

OUTPUT 1:

Enter a number : a

a is an alphabet


OUTPUT 2:

Enter a number : B

B is an alphabet


OUTPUT 3:

Enter a number : *

* is not an alphabet


OUTPUT 4:

Enter a number : 5

5 is not an alphabet

on Leave a Comment

C++ Program to Check Whether a Number is Palindrome or Not

To understand C++ program to check whether a number is palindrome or not, you should know about - C++ looping.

Palindrome number in C++

If the reversed pattern of the digits of a number is equal to the original number, then the number is called palindrome number.

Note : Palindrome number must be a positive integer

C++ Program to Check Whether a Number is Palindrome or Not

#include<iostream>
using namespace std;
int main()
{
    int n,rem,rev=0,t;
    cout<<"Enter a number : ";
    cin>>n;
    t=n;
    while(n!=0)
    {
        rem=n%10;
        rev=rev*10+rem;
        n=n/10;
    }
    if(t==rev)
        cout<<endl<<t<<" is a palindrome number";
    else
        cout<<endl<<t<<" is not a palindrome number";
    return 0;
}

OUTPUT 1:

Enter a number : 45254

45254 is a palindrome number


OUTPUT 2:

Enter a number : 487

487 is not a palindrome number

on Leave a Comment

C++ Program to Check Number is Armstrong or Not

To understand C++ program to check Armstrong number, you must know - C++ looping

If the sum of cube of an positive integer is equal to that positive integer, then positive integer is called an Armstrong number. 

For example:

407=4*4*4+0*0*0+7*7*7 //Armstrong number
123 is not equal to 1*1*1+2*2*2+3*3*3 //Not an Armstrong number

C++ Program to Check Number is Armstrong or Not

#include<iostream>
using namespace std;
int main()
{
    int num,n,d,s=0;
    cout<<"Enter a number : ";
    cin>>num;
    n=num;
    while(num!=0)
    {
        d=num%10;
        s=s+d*d*d;
        num/=10;
    }
    if(s==n)
        cout<<endl<<n<<" is an Armstrong number";
    else
        cout<<endl<<n<<" is not an Armstrong number";
    return 0;
}

OUTPUT 1:

Enter a number : 407

407 is an Armstrong number

OUTPUT 2:

Enter a number : 123

123 is not an Armstrong number

on Leave a Comment

C++ Program for Binary Search

Binary Search in C++

Binary search in c++ uses for statement and if-else statement. Binary search is performed on sorted array. You can enter sorted array values directly and you can also sort through bubble sort or any other sorting technique. Binary search is better searching technique as compared to linear search because binary search takes less time for searching.  

In binary search, we use three variables start, last and mid to represent starting, ending and middle value of an array. In while loop, every time we compare variable mid to searching item. If array middle value is less than searching item, then variable start is assigned to variable mid+1. Else if variable mid is equal to searching item, then is printed on screen. Else, variable last is assigned to mid-1. After that mid is assigned to (start+last)/2. It is continued to while till start<=last.


C++ Program for Binary Search

#include<iostream>
using namespace std;
int main()
{
    int arr[10],s,i,l,start,last,mid;
    cout<<"Enter the length of an array : ";
    cin>>l;
    cout<<endl<<"Enter array elements in sorted values (asc/dsc)"<<endl;
    for(i=0;i<l;i++)
    {
        cout<<endl<<"Enter the value of element no "<<i+1<<" : ";
        cin>>arr[i];
    }
    cout<<endl<<"Enter the value to be search : ";
    cin>>s;
    start=0;
    last=l-1;
    mid=(start+last)/2;
    while(start<=last)
    {
        if(arr[mid]<s)
        {
            start=mid+1;
        }
        else if(arr[mid]==s)
        {
            cout<<endl<<s<<" is present in array at location "<<mid+1;
            break;
        }
        else
            last=mid-1;
        mid=(start+last)/2;
    }
    if(start>last)
    {
        cout<<endl<<s<<" is not present in array";
    }
    return 0;
}

OUTPUT 1:

Enter the length of an array : 5

Enter array elements in sorted values (asc/dsc)

Enter the value of element no 1 : 2

Enter the value of element no 2 : 8

Enter the value of element no 3 : 9

Enter the value of element no 4 : 4

Enter the value of element no 5 : 6

Enter the value to be search : 9

9 is present in array at location 3


OUTPUT 2:

Enter the length of an array : 5

Enter array elements in sorted values (asc/dsc)

Enter the value of element no 1 : 4

Enter the value of element no 2 : 8

Enter the value of element no 3 : 6

Enter the value of element no 4 : 2

Enter the value of element no 5 : 3

Enter the value to be search : 1

1 is not present in array

on Leave a Comment

C++ Program for Linear Search

Linear search in C++

In C++, we can search a number in an array using linear search and binary search. In linear search algorithm, we compare number to every element of an array using for statement and if-else statement. Linear search can take long time for searching because it compares number to every individual element of an array. 

C++ Program for Linear Search

#include<iostream>
using namespace std;
int main()
{
    int arr[10],s,i,l,flag=0;
    cout<<"Enter the length of an array : ";
    cin>>l;
    for(i=0;i<l;i++)
    {
        cout<<endl<<"Enter the value of element no "<<i+1<<" : ";
        cin>>arr[i];
    }
    cout<<endl<<"Enter the value to be search : ";
    cin>>s;
    for(i=0;i<l;i++)
    {
        if(arr[i]==s)
        {
            cout<<endl<<s<<" is present in array at element no "<<i+1;
            flag=1;
            break;
        }
    }
    if(flag==0)
    {
        cout<<endl<<s<<" is not present in array";
    }
    return 0;
}

OUTPUT 1:

Enter the length of an array : 5

Enter the value of element no 1 : 8

Enter the value of element no 2 : 4

Enter the value of element no 3 : 6

Enter the value of element no 4 : 7

Enter the value of element no 5 : 2

Enter the value to be search : 7

7 is present in array at element no 4

OUTPUT 2:

Enter the length of an array : 5

Enter the value of element no 1 : 5

Enter the value of element no 2 : 6

Enter the value of element no 3 : 1

Enter the value of element no 4 : 2

Enter the value of element no 5 : 3

Enter the value to be search : 10

10 is not present in array