0% found this document useful (0 votes)
6 views3 pages

C Coding Interview Questions

The document contains six C programming examples demonstrating basic algorithms: reversing a string, checking for prime numbers, calculating factorial using recursion, swapping two numbers without a temporary variable, generating a Fibonacci series iteratively, and checking if a string is a palindrome. Each example includes code snippets and prompts for user input. These examples serve as fundamental exercises for learning C programming concepts.

Uploaded by

aram62549
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views3 pages

C Coding Interview Questions

The document contains six C programming examples demonstrating basic algorithms: reversing a string, checking for prime numbers, calculating factorial using recursion, swapping two numbers without a temporary variable, generating a Fibonacci series iteratively, and checking if a string is a palindrome. Each example includes code snippets and prompts for user input. These examples serve as fundamental exercises for learning C programming concepts.

Uploaded by

aram62549
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

1.

Reverse a String
#include <stdio.h>
#include <string.h>

int main() {
char str[100], temp;
int i, j;

printf("Enter a string: ");


gets(str);

i = 0;
j = strlen(str) - 1;

while(i < j) {
temp = str[i];
str[i] = str[j];
str[j] = temp;
i++;
j--;
}

printf("Reversed string: %s\n", str);


return 0;
}

2. Check Prime Number


#include <stdio.h>

int main() {
int num, i, flag = 0;
printf("Enter a number: ");
scanf("%d", &num);

if (num <= 1)
flag = 1;

for(i = 2; i <= num / 2; ++i) {


if(num % i == 0) {
flag = 1;
break;
}
}
if(flag == 0)
printf("%d is a prime number.\n", num);
else
printf("%d is not a prime number.\n", num);

return 0;
}

3. Factorial using Recursion


#include <stdio.h>

int factorial(int n) {
if(n == 0) return 1;
return n * factorial(n - 1);
}

int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);

printf("Factorial of %d is %d\n", num, factorial(num));


return 0;
}

4. Swap Two Numbers Without Temp Variable


#include <stdio.h>

int main() {
int a, b;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);

a = a + b;
b = a - b;
a = a - b;

printf("After swapping: a = %d, b = %d\n", a, b);


return 0;
}

5. Fibonacci Series (Iterative)


#include <stdio.h>
int main() {
int n, t1 = 0, t2 = 1, nextTerm;

printf("Enter number of terms: ");


scanf("%d", &n);

printf("Fibonacci Series: ");


for (int i = 1; i <= n; ++i) {
printf("%d ", t1);
nextTerm = t1 + t2;
t1 = t2;
t2 = nextTerm;
}

return 0;
}

6. Palindrome String Check


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

int main() {
char str[100];
int i, len, flag = 1;

printf("Enter a string: ");


gets(str);

len = strlen(str);
for(i = 0; i < len / 2; i++) {
if(str[i] != str[len - i - 1]) {
flag = 0;
break;
}
}

if(flag)
printf("Palindrome\n");
else
printf("Not a palindrome\n");

return 0;
}

You might also like