C Programming Coding Questions

Practice most asked C programming coding interview questions with solutions.

1. Find factorial of a number

Easy
  Input: 5
  Output: 120

  #include <bits/stdc++.h>
  using namespace std;

  int main() {
    int n, fact = 1;
    cin >> n;

    for(int i = 1; i <= n; i++)
      fact *= i;

    cout << fact;
    return 0;
  }
  

2. Check whether a number is prime

Easy
  Input: 7
  Output: Prime

  #include <bits/stdc++.h>
  using namespace std;

  int main() {
    int n;
    cin >> n;

    if(n <= 1) {
      cout << "Not Prime";
      return 0;
    }

    for(int i = 2; i * i <= n; i++) {
      if(n % i == 0) {
        cout << "Not Prime";
        return 0;
      }
    }

    cout << "Prime";
    return 0;
  }
  

3. Print Fibonacci series

Easy
  Input: 7
  Output: 0 1 1 2 3 5 8

  #include <bits/stdc++.h>
  using namespace std;

  int main() {
    int n, a = 0, b = 1;
    cin >> n;

    for(int i = 1; i <= n; i++) {
      cout << a << " ";
      int c = a + b;
      a = b;
      b = c;
    }
    return 0;
  }
  

4. Reverse a number

Easy
  Input: 12345
  Output: 54321

  #include <bits/stdc++.h>
  using namespace std;

  int main() {
    int n, rev = 0;
    cin >> n;

    while(n > 0) {
      rev = rev * 10 + n % 10;
      n /= 10;
    }

    cout << rev;
    return 0;
  }
  

5. Check whether a number is palindrome

Easy
  Input: 121
  Output: Palindrome
  

  #include <bits/stdc++.h>
  using namespace std;

  int main() {
    int n, temp, rev = 0;
    cin >> n;
    temp = n;

    while(n > 0) {
      rev = rev * 10 + n % 10;
      n /= 10;
    }

    if(temp == rev)
      cout << "Palindrome";
    else
      cout << "Not Palindrome";

    return 0;
  }
  

6. Check Armstrong number

Medium
  Input: 153
  Output: Armstrong

  #include <stdio.h>
  #include <math.h>
  int main() {
    int n, temp, sum = 0, digits = 0;
    scanf("%d", &n);
    temp = n;

    while(temp) {
      digits++;
      temp /= 10;
    }

    temp = n;
    while(temp) {
      sum += pow(temp % 10, digits);
      temp /= 10;
    }

    printf(sum == n ? "Armstrong" : "Not Armstrong");
    return 0;
  }
      

7. Find largest of three numbers

Easy
  Input: 10 25 15
  Output: 25

  #include <bits/stdc++.h>
  using namespace std;

  int main() {
    int a, b, c;
    cin >> a >> b >> c;

    cout << max(a, max(b, c));
    return 0;
  }
  

8. Find sum of array elements

Easy
  Input: 5
        10 20 30 40 50
  Output: 150

  #include <bits/stdc++.h>
  using namespace std;

  int main() {
    int n, sum = 0;
    cin >> n;
    int arr[n];

    for(int i = 0; i < n; i++) {
      cin >> arr[i];
      sum += arr[i];
    }

    cout << sum;
    return 0;
  }
  

9. Linear search in array

Easy
  Input: 5
        10 20 30 40 50
        30
  Output: Found

  #include <bits/stdc++.h>
  using namespace std;

  int main() {
    int n, key;
    cin >> n;
    int arr[n];

    for(int i = 0; i < n; i++)
      cin >> arr[i];

    cin >> key;

    for(int i = 0; i < n; i++) {
      if(arr[i] == key) {
        cout << "Found";
        return 0;
      }
    }

    cout << "Not Found";
    return 0;
  }
  

10. Count digits of a number

Easy
  Input: 12345
  Output: 5

  #include <bits/stdc++.h>
  using namespace std;

  int main() {
    int n, count = 0;
    cin >> n;

    while(n > 0) {
      count++;
      n /= 10;
    }

    cout << count;
    return 0;
  }
  

11. Swap two numbers without using third variable

Medium
  Input: 10 20
  Output: 20 10

  #include <bits/stdc++.h>
  using namespace std;

  int main() {
    int a, b;
    cin >> a >> b;

    a = a ^ b;
    b = a ^ b;
    a = a ^ b;

    cout << a << " " << b;
    return 0;
  }
  

12. Find second largest element in array

Medium
Input: 5
10 20 15 25 5
Output: 20

#include <bits/stdc++.h>
using namespace std;

int main() {
  int n;
  cin >> n;
  int a[n];
  for(int i=0;i<n;i++) cin >> a[i];

  int max1=INT_MIN, max2=INT_MIN;
  for(int i=0;i<n;i++){
    if(a[i] > max1){
      max2 = max1;
      max1 = a[i];
    } else if(a[i] > max2 && a[i] != max1){
      max2 = a[i];
    }
  }
  cout << max2;
  return 0;
}

13. Check balanced parentheses

Medium
Input: ((a+b)*(c+d))
Output: Balanced

#include <bits/stdc++.h>
using namespace std;

int main(){
  string s;
  cin >> s;
  int count = 0;
  for(char c : s){
    if(c=='(') count++;
    if(c==')') count--;
    if(count<0){ cout << "Unbalanced"; return 0;}
  }
  cout << (count==0 ? "Balanced" : "Unbalanced");
  return 0;
}

14. Frequency of characters in a string

Medium
Input: hello
Output: h=1
        e=1
        l=2
        o=1

#include <bits/stdc++.h>
using namespace std;

int main(){
  string s;
  cin >> s;
  int freq[256]={0};
  for(char c : s) freq[c]++;
  for(int i=0;i<256;i++)
    if(freq[i]) cout << char(i) << "=" << freq[i] << endl;
  return 0;
}

15. Remove duplicates from array

Medium
Input: 5
1 2 2 3 1
Output: 1 2 3

#include <bits/stdc++.h>
using namespace std;

int main(){
  int n;
  cin >> n;
  int a[n];
  for(int i=0;i<n;i++) cin >> a[i];

  for(int i=0;i<n;i++){
    for(int j=i+1;j<n;j++){
      if(a[i]==a[j]){
        for(int k=j;k<n-1;k++) a[k]=a[k+1];
        n--; j--;
      }
    }
  }

  for(int i=0;i<n;i++) cout << a[i] << " ";
  return 0;
}

16. Binary search in sorted array

Medium
Input: 7
2 5 8 12 16 23 38
16
Output: Found

#include <bits/stdc++.h>
using namespace std;

int main(){
  int n,key;
  cin >> n;
  int a[n];
  for(int i=0;i<n;i++) cin >> a[i];
  cin >> key;

  int l=0,r=n-1;
  while(l<=r){
    int mid=(l+r)/2;
    if(a[mid]==key){ cout << "Found"; return 0;}
    else if(a[mid]<key) l=mid+1;
    else r=mid-1;
  }
  cout << "Not Found";
  return 0;
}