-->




c++ recursive function to print numbers in descending order

1 comment
Write a program in which a function takes input from user and print numbers in descending order using recursion. Make proper prototype with one integer as argument and return an integer. 
hint: int decrement(int);



Program Logic Explanation

  • In this program we have a function having an if-else statement 
  • If and else both have return on the basis of certain conditions.
  • If condition has the base case which means that program output will not return result
    until the base case is true
  • As we know function returns from where it has called so if user enters 5 then function will be
    called 5 times one from the outside and 4 times form else statement when base case becomes
    true it returns in else to 4 times and finally 5th time to main body

C++ Source Code

#include<iostream>
using namespace std;
int decrement(int);
int main()
{
 int number;
 cout<<"Enter number to get decrement using recursion";
 cin>>number;
 cout<<decrement(number);
 cout<<" "<<endl;
 return 0;
}
int decrement(int num)
{
 if(num==1)
 {

  return 1;
 }
 else
 {
  cout<<num<<" ";
  return decrement(num-1);
 }
}


Example input output
c++ recursive function example to output numbers in descending order

It is recommended to change the code, dry run on a paper and examine the output.
See more examples here
C++ Simple example for beginners
Read More...

search a character in a string strchr c++ example

1 comment
strchr is a string class function which helps to find a character in a string. It takes two parameters first is string and second is character to search. 
It returns NULL if specified character not found else it returns a pointer value. Remember it is case sensitive "C" and 'c' are different characters for this function

Write a c++ program which takes a string from user and a character then search a character 



C++ program source code


#include <iostream>
#include <string.h>
#include <stdio.h>

using namespace std;

int main()
{
    char stringObj[100], ch;
    char *result = NULL;

    cout<<"\nEnter a String: ";
    gets(stringObj);
    cout<<"\nEnter character to search in string:   ";
    cin>>ch;
    result = strchr(stringObj, ch);
    if(result == NULL) {
         cout<<"\nCharacter ' "<<ch<<" ' NOT FOUND in : "<<stringObj<<endl;
    } else {
         cout<<"\nCharacter ' "<<ch<<" ' FOUND in : "<<stringObj<<endl;
    }
          return 0;
}


program output: 
strchr c++ example

 Program with case sensitive output
strchr c++ example


See also:



Read More...

strncat function c++ example source code

Leave a Comment
strncat function used to concatenate two strings upto specified number of characters. It takes 3 parameters first and second is string third is n where n is number of characters to append first string from second string.
For example
first_string: Hello
second_string: World

strncat( first_string, second_string, 3);

The result will be: Hello Wor

Example program
Write a c++ program which takes 3 inputs from two strings and third is the value of n( the number of characters to append from second string) and display the resultant string.

strncat function C++ source code


#include <iostream>
#include <string.h>
#include <stdio.h>

using namespace std;

int main()
{
    char str1[100],str2[100];

    cout<<"\nEnter first string: ";
    gets(str1);

    cout<<"\nEnter second string: ";
    gets(str2);

    int n;
    cout<<"\nEnter number of characters to concatenat: ";
    cin>>n;
    strncat(str1, str2, n);

    cout<<"\n\n\t\Resultant first String after concatenation: "<<endl;
    cout<<"\n\n\t\t\t"<<str1<<"\n\n\t\t\t";

          return 0;
}


Program output
strncat string class function source code example
  
It is recommended to change the code and examine the output of program.
See Also: strcat function example c++ source code
Read More...

string function strcat c++ example

Leave a Comment
Strcat function comes from the string class with string.h header file. It takes two strings as parameters and return second string append to the first string with putting null character at the end of the string.


Example C++ program 
A C++ program which takes two strings as input from user each of type char and concatenates them with the help of strcat function and displays the result.

See also: concatenate two string without using strcat

strcat example C++ source code

#include <iostream>
#include <string.h>
#include <stdio.h>

using namespace std;

int main()
{
    char str1[100],str2[100];

    cout<<"\nEnter first string: ";
    gets(str1);

    cout<<"\nEnter second string: ";
    gets(str2);

    strcat(str1, str2);

    cout<<"\n\n\t\Resultant first String after concatenation: "<<endl;
    cout<<"\n\n\t\t\t"<<str1<<"\n\n\t\t\t";

          return 0;
}


Input Output
strcat example c++ source code

Its a simple program example it is recommended to change the code and examine the output. Happy learning C++
Read More...