Google Add

Search

C Program to Compare Two Strings Using strcmp() Function

Write a c program to compare two strings using strcmp() function. In this tutorial, we are going to write a c code which take two strings as an input and compare it with inbuilt strcmp() function.

The strcmp() function compares two strings lexicographically.

i) When both the strings are equal,  then it returns zero.

ii) When first string is lexicographically greater than second string, it returns positive value.

iii) When first string is lexicographically lesser than second string, it returns negative value.

C program to check whether two strings are anagrams of each other

C program to find length of a string without using strlen


C Program to Compare Two Strings Using strcmp() Function


C Program to Compare Two Strings Using strcmp() Function


In this programming question, Let's first take two input strings from a user and then use the inbuilt strcmp() function to compare two strings.


#include <stdio.h>

int main(void) {
    
   char str1[100], str2[100];
 
   printf("Enter a first string\n");
   gets(str1);
 
   printf("Enter a second string\n");
   gets(str2);
 
   // If string is equal it returns zero
   if( strcmp(str1, str2) == 0 ) {
      printf("Both strings are equal.\n");
   
   } else if( strcmp(str1, str2) < 0) {
      printf("Str1 is lexicographically smaller than str2 \n");
      
   } else {
         printf("Str1 is lexicographically greater than str2 \n");
   }
      
    return 0;
}


Output:

Enter a first string
c

Enter a second string
programming

Str1 is lexicographically smaller than str2


Programming questions on strings

C program to check whether a number is palindrome or not

Java programming questions

C++ Program to Print Factorial of a Number using Recursion

Write a c++ program to print factorial of a number using recursion. In this programming question, we are going to write a c++ code which takes an input number and print factorial of a number using recursion.

This question is mostly asked in an interviews. There are two approaches to print factorial of a number (Iterative & Recursive).

i) C++ program to print factorial of a number - Iterative approach

ii) Second approach is to print factorial of a number using recursion. In this tutorial, we are going to discuss second approach.

C program to print factorial of a number

Java program to print factorial of a number

C program to print factorial of a number using recursion

C++ Program to Find Factorial of a Number using Recursion



Let's write our code.

i)  First, take an input number from a user.
ii) Then call a method which recursively prints the factorial of a number.


#include <iostream>
using namespace std;

int calculateFactorial (int num) {
 
  if ( num == 0 || num == 1) {
   
     return 1;
   
   } 
  
   return num * calculateFactorial(num-1);;
}


int main() {
 
   int num, fact = 0;
 
   cout << "Enter a number \n";
   cin  >> num;
 
   fact = calculateFactorial (num);
 
   cout << "Factorial of a number " << num << " is " << fact;
 
   return 0;
}


Output :

Enter a number : 4

Factorial of a number  4 is 24

Explanation : Suppose we have entered 4 so how this program is going to be executed.

4 * calculateFactorial (4-1)
4 * 3 * calculateFactorial (3-1)
4 * 3 * 2 * calculateFactorial (2-1)
4 * 3 * 2 * 1

Print Factorial of a Number using Recursion in C

Write a program to print factorial of a number using recursion in C.  In this program, we have to write a code which takes an input number and print factorial of a number using recursion.

Before solving this problem, let's first understand what is recursion? And the difference between recursion and iteration.

What is Recursion?


In Recursion, A function calls itself until the base condition is reached. Using recursion, we can write much cleaner and shorter as compared to iterative code. You can learn more about recursion using following tutorials.


MCQ on Recursion for Practice

In this tutorial, we are going to use recursion to print factorial of a number. In my previous post, i have explained how to print factorial of a number in c using iterative approach.

C program to print factorial of a number

Java program to print factorial of a number

C++ program to print factorial of a number


C Program to Print Factorial of a Number using Recursion


Print Factorial of a Number using Recursion in C



Let's write a c code to print factorial of a number using recursive approach. In this program, first we take input number from a user then we call a calculateFactorial() method to calculate factorial of a number.


#include <stdio.h>

int calculateFactorial (int num) {
 
  /* If number is zero or 1 then return 1 */

  if ( num == 0 || num == 1) {
   
     return 1;
   
   } 
  
   /* Recursive function call */

   return num * calculateFactorial(num-1);
}

int main() {
 
  int num, fact = 0;
 
  printf ("Enter a number \n");
  scanf ("%d", &num);
 
  /* Function call */

  fact = calculateFactorial(num);
 
  printf ("Factorial of a number %d is %d ",num,fact);
  return 0;
}


Output :

Enter number :  5

Factorial of a number 5 is 120


Explanation :

Suppose you have entered 5 so how this program is going to be executed.

5 * calculateFactorial (5-1)
5 * 4 * calculateFactorial (4-1)
5 * 4 * 3 * calculateFactorial (3-1)
5 * 4 * 3 * 2 * calculateFactorial (2-1)
5 * 4 * 3 * 2 * 1 

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

Write a c++ program to check whether a number is palindrome or not. In this program, we write a code to take an input number from a user and check whether a number is palindrome or not.

Let's first understand what is a palindrome number?

A palindrome number is a number that remains same when it's digits are reversed.

For example -

i) 121 is a palindrome number.

ii) 12321 is a palindrome number.


In my previous post, I have explained what is palindrome and how to check whether a number

C program to check whether a number is palindrome or not

How to check whether an input number is palindrome or not?


i) Take an input number from a user.

ii) Reverse an input number.

iii) Compare input number and reverse of an input number. If both are equal then entered number is a palindrome number.


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


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


#include<iostream>
using namespace std;
  
int main(){

    int num, temp, rev = 0, digit;
    
    //Input number
    cout << "Enter a number \n";
    cin  >> num;
    
    temp = num;
    
    //Reverse a number  
    while (temp > 0) {
        
        digit = temp % 10;   
        rev  = rev * 10 + digit; 
        temp = temp / 10; 
    }
   
    if ( num == rev) {
        cout << num << " is a palindrome number";
    } else {
        cout << num << " is not a palindrome number";
    }
               
    return 0;     
}





C++ program to delete an element from an array

Programming questions on strings

Programming questions on recursion