C++ Program to generate random number [DEVCPP/GCC]

RAND FUNCTION

It is a pseudo random generator which generates a random number between 0 and RAND_MAX. In order to generate random number in user defined range, this function can be used with modulus operator.

// Generates random number between 0 to 99      
int r1 = rand() % 100;                          

// Generates random number between 1 to 100     

int r2 = rand() % 100 + 1;                      

PROGRAM

/* Program to make user guess a number and comparing it with randomly generated number */

#include <iostream>
#include <stdlib.h>

using namespace std;

int main()
{
        int count=0,rno,num;

        rno = rand()%10 + 1;

        do
        {

                cout<<"\nENTER A NUMBER BETWEEN 1 TO 10: ";
                cin>>num;

                if(num==rno)
                {
                          cout<<"\nBINGO!";
                }
                else if(num<rno)
                {
                          cout<<"\nRAISE YOUR NUMBER!";
                }
                else
                {
                          cout<<"\nLOW YOUR NUMBER!";
                }

                count++;

         } while(num!=rno);

          cout<<"\nYOUR SCORE IS "<<count;

           return 0;
}

OUTPUT

C++ Program to generate random number with output

C++ Program to display the pattern of diamond [DEVCPP/GCC]

PROGRAM

//Program to display the pattern of diamond!

# include <iostream>
using namespace std;

int main()
{
int rows,uh,lh,i,j,k;

cout<<"ENTER THE NUMBER OF ROWS:";
cin>>rows;

//Calculating the rows to be dedicated for the upper triangular part
uh=(rows/2)+1;  

       //Calculating the rows to be dedicated for the lower triangular part
       lh=(rows-uh);      

for(i=1;i<=uh;i++)          //Printing upper triangle
{
for(j=(uh-i);j>0;j--)
{
cout<<" ";
}
for(k=1;k<(2*i);k++)
{
cout<<"*";
}
cout<<endl;
}

for(i=lh;i>0;i--)               //Printing lower triangle
{
for(j=0;j<=(lh-i);j++)
{
cout<<" ";
}
for(k=1;k<(2*i);k++)
{
cout<<"*";
}
cout<<endl;
}

return 0;
}


OUTPUT

C++ Program to display the pattern of diamond with output

C++ Program to remove duplicate elements from an array [DEVCPP/GCC]

PROGRAM

// Program to remove duplicate elements from an array

#include <iostream>

using namespace std;

int main()
{
        int i,j,k,len=0;

        cout<<"ENTER THE NUMBER OF ELEMENTS: ";
        cin>>len;

        int num[len];

        cout<<"\nENTER ELEMENTS: ";

        for(i=0 ; i<len ; i++)
        {
                   cin>>num[i];
         }

        for(i=0 ; i<len ; i++)
        {
                for(j=i+1 ; j<len ; j++)
                {
                        if(num[i]==num[j])
                        {
                                 for(k=j ; k<len ; k++)
                                 {
                                            num[k]=num[k+1];
                                  }

                                  len--;
                                  j--;
                       }
                }
          }

          cout<<"\nDISTINCT ELEMENTS: ";

          for(i=0 ; i<len ; i++)
          {
                  cout<<num[i]<<" ";
           }

            return 0;
}

OUTPUT

C++ Program to remove duplicate elements from an array with output


C++ Program to sum elements passed through Command Line Arguments [DEVCPP/GCC]

COMMAND LINE ARGUMENTS

Command Line Arguments are optional string type arguments which can be passed to the program by the Operating System when it is launched.

The full declaration of main is

int main( int argc, char *argv[])

Here, argc refers to Argument Count which contains the number of arguments passed to the main function including a parameter as path of the program. argv is an array of character pointers which contains the arguments. The first argument argv[0] contains the path of program.

To pass Command Line Arguments in DevC++, you need to do the following:

C++ Program to sum elements passed through Command Line Arguments
                         

                           C++ Program to sum elements passed through Command Line Arguments

PROGRAM

//Program to sum elements passed through Command Line Arguments

#include <iostream>
#include <stdlib.h>

using namespace std;

int main(int argc, char *argv[])
{
        int i,sum=0;

        cout<<"\nARGUMENT COUNT: "<<argc;
        cout<<"\nARGUMENT VALUES: ";

        for(i=0;i<argc;i++)
        {
                cout<<" "<<argv[i];
                if(i!=0)
                {
                           sum=sum+atoi(argv[i]);
                }
        }

        cout<<"\nSUM OF THE ELEMENTS: "<<sum;

        return 0;
}

OUTPUT

C++ Program to sum elements passed through Command Line Arguments with output

C++ Program to check whether given string is palindrome or not [DEVCPP/GCC]


PALINDROME

A palindrome is a word or phrase which is read same from forward as well as from backwards. Spaces or punctuations are usually not taken into consideration for Palindromes.

WORD PALINDROMES
  • Madam
  • Racecar
  • Level
  • Radar
  • Refer
PHRASE PALINDROMES
  • Draw pupil's lip upwards.
  • Rise to vote, Sir!
  • Step on no pets!

PROGRAM

// Program to check whether given string is palindrome or not

#include <iostream>
#include <string>

using namespace std;

int main()
{
int i,len;
string str,strws;
bool flag=true;
cout<<"ENTER A STRING: ";
getline(cin,strws);
len=strws.length();
/*Removing Spaces and Punctuations From String and Converting into Lowercase*/
for(i=0;i<len;i++)
{
if('A'<=strws[i] && strws[i]<='Z')
{
strws[i]=strws[i]+32;
}
if('a'<=strws[i] && strws[i]<='z')
{
str.push_back(strws[i]);
}
}

len=str.length();

for(i=0;i<=(len/2);i++)
{
if(str[i]!=str[len-i-1])
{
flag=false;
break;
}
}
if(flag)
{
cout<<"\nGIVEN STRING IS A PALINDROME!";
}
else
{
cout<<"\nGIVEN STRING IS NOT A PALINDROME!";
}
return 0;
}

OUTPUT

C++ Program to check whether given string is palindrome or not with output

C++ Program to add the distinct elements of an array [DEVCPP/GCC]


PROGRAM

//Program to add the distinct elements of an array

# include <iostream>
using namespace std;

int main()
{
int arr[5],i,j,sum=0;
     bool flag;

cout<<"ENTER THE ELEMENTS OF AN ARRAY:\n";
for(i=0;i<5;i++)     // Input an array
{
cin>>arr[i];
}

for(i=0;i<5;i++)
{
flag=true;
for(j=0;j<i;j++)
{
if(arr[i]==arr[j]) //check for uniqueness
{
flag=false;
break;
}
}
if(flag)
{
sum=sum+arr[i]; //summation of distinct elements
}
}

cout<<"SUM IS:"<<sum;
return 0;
}

OUTPUT

C++ Program to add the distinct elements of an array with output

FIND US ON FACEBOOK!