0% found this document useful (0 votes)
13 views8 pages

C Programming Questions With Programs.2

Uploaded by

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

C Programming Questions With Programs.2

Uploaded by

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

C Programming Questions with Programs int main() {

Easy Level int num = 7;

1. Write a program to print 'Hello, World!' if (num % 2 == 0)

#include <stdio.h> printf("Even\n");

int main() { else

printf("Hello, World!\n"); printf("Odd\n");

return 0; return 0;

} }

2. Write a program to add two numbers 5. Write a program to calculate the


factorial of a number using loop
#include <stdio.h>
#include <stdio.h>
int main() {
int main() {
int a = 5, b = 3;
int n = 5, i;
printf("Sum: %d\n", a + b);
unsigned long long fact = 1;
return 0;
for(i = 1; i <= n; ++i) {
}
fact *= i;
3. Write a program to find the maximum
of two numbers }

#include <stdio.h> printf("Factorial: %llu\n", fact);

int main() { return 0;

int a = 10, b = 20; }

if (a > b) 6. Write a program to reverse a number

printf("%d is greater\n", a); #include <stdio.h>

else int main() {

printf("%d is greater\n", b); int num = 1234, rev = 0;

return 0; while(num != 0) {

} rev = rev * 10 + num % 10;

4. Write a program to find whether a num /= 10;


number is even or odd
}
#include <stdio.h>
printf("Reversed Number: %d\n", rev); int num = 5;

return 0; for(int i = 1; i <= 10; i++)

} printf("%d x %d = %d\n", num, i, num * i);

7. Write a program to check for a leap year return 0;

#include <stdio.h> }

int main() { 10. Write a program to check if a character


is a vowel or consonant
int year = 2024;
#include <stdio.h>
if ((year % 4 == 0 && year % 100 != 0) ||
year % 400 == 0) int main() {

printf("Leap Year\n"); char c = 'e';

else if(c == 'a' || c == 'e' || c == 'i' || c == 'o' ||


c == 'u' ||
printf("Not a Leap Year\n");
c == 'A' || c == 'E' || c == 'I' || c == 'O' || c
return 0;
== 'U')
}
printf("Vowel\n");
8. Write a program to swap two numbers
else
using a temporary variable
printf("Consonant\n");
#include <stdio.h>
return 0;
int main() {
}
int a = 10, b = 20, temp;
Medium Level
temp = a;
11. Write a program to calculate the sum
a = b;
of digits of a number
b = temp;
#include <stdio.h>
printf("a = %d, b = %d\n", a, b);
int main() {
return 0;
int num = 1234, sum = 0;
}
while(num != 0) {
9. Write a program to print the
sum += num % 10;
multiplication table of a number
num /= 10;
#include <stdio.h>
}
int main() {
printf("Sum of digits: %d\n", sum); t2 = next;

return 0; }

} return 0;

12. Write a program to check whether a }


number is prime
14. Write a program to find the LCM of
#include <stdio.h> two numbers

int main() { #include <stdio.h>

int num = 29, i, flag = 0; int main() {

for(i = 2; i <= num / 2; ++i) { int a = 12, b = 18, max;

if(num % i == 0) { max = (a > b) ? a : b;

flag = 1; while(1) {

break; if(max % a == 0 && max % b == 0) {

} printf("LCM = %d\n", max);

} break;

if(flag == 0) }

printf("Prime\n"); ++max;

else }

printf("Not Prime\n"); return 0;

return 0; }

} 15. Write a program to count the number


of vowels in a string
13. Write a program to print Fibonacci
series up to n terms #include <stdio.h>

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

int main() { int main() {

int n = 10, t1 = 0, t2 = 1, next; char str[] = "Hello World";

for(int i = 1; i <= n; ++i) { int count = 0;

printf("%d ", t1); for(int i = 0; i < strlen(str); i++) {

next = t1 + t2; char c = tolower(str[i]);

t1 = t2;
if(c == 'a' || c == 'e' || c == 'i' || c == 'o' || else
c == 'u')
printf("Not Palindrome\n");
count++;
return 0;
}
}
printf("Number of vowels: %d\n", count);
18. Write a program to sort an array in
return 0; ascending order

} #include <stdio.h>

16. Write a program to reverse a string int main() {

#include <stdio.h> int arr[] = {5, 3, 8, 4, 2}, n = 5, temp;

#include <string.h> for(int i = 0; i < n-1; i++) {

int main() { for(int j = i+1; j < n; j++) {

char str[] = "hello"; if(arr[i] > arr[j]) {

int len = strlen(str); temp = arr[i];

for(int i = len - 1; i >= 0; i--) arr[i] = arr[j];

printf("%c", str[i]); arr[j] = temp;

printf("\n"); }

return 0; }

} }

17. Write a program to check whether a for(int i = 0; i < n; i++)


number is palindrome
printf("%d ", arr[i]);
#include <stdio.h>
return 0;
int main() {
}
int num = 121, rev = 0, temp = num;
19. Write a program to find the GCD of
while(temp != 0) { two numbers

rev = rev * 10 + temp % 10; #include <stdio.h>

temp /= 10; int main() {

} int a = 54, b = 24, gcd;

if(rev == num) for(int i = 1; i <= a && i <= b; ++i) {

printf("Palindrome\n"); if(a % i == 0 && b % i == 0)


gcd = i; int low = 0, high = n-1, mid;

} while(low <= high) {

printf("GCD: %d\n", gcd); mid = (low + high) / 2;

return 0; if(arr[mid] == key) {

} printf("Found at index %d\n", mid);

20. Write a program to find second largest return 0;


element in array
} else if(arr[mid] < key) {
#include <stdio.h>
low = mid + 1;
int main() {
} else {
int arr[] = {12, 35, 1, 10, 34, 1};
high = mid - 1;
int first = arr[0], second = -1;
}
for (int i = 1; i < 6; i++) {
}
if (arr[i] > first) {
printf("Not found\n");
second = first;
return 0;
first = arr[i];
}
} else if (arr[i] > second && arr[i] != first) {
22. Write a program to implement matrix
second = arr[i]; multiplication

} #include <stdio.h>

} int main() {

printf("Second largest: %d\n", second); int a[2][2] = {{1, 2}, {3, 4}}, b[2][2] = {{2, 0},
{1, 2}}, c[2][2];
return 0;
for(int i = 0; i < 2; i++) {
}
for(int j = 0; j < 2; j++) {
Hard Level
c[i][j] = 0;
21. Write a program to implement binary
search for(int k = 0; k < 2; k++) {

#include <stdio.h> c[i][j] += a[i][k] * b[k][j];

int main() { }

int arr[] = {2, 4, 6, 8, 10, 12}, n = 6, key = printf("%d ", c[i][j]);


10;
}
printf("\n"); printf("%d ", arr[i]);

} return 0;

return 0; }

} 24. Write a program to evaluate postfix


expression
23. Write a program to implement quick
sort #include <stdio.h>

#include <stdio.h> #include <ctype.h>

void quicksort(int arr[], int low, int high) { int stack[20], top = -1;

int i = low, j = high, temp; void push(int x) { stack[++top] = x; }

int pivot = arr[(low + high) / 2]; int pop() { return stack[top--]; }

while(i <= j) { int main() {

while(arr[i] < pivot) i++; char exp[] = "231*+9-";

while(arr[j] > pivot) j--; int i = 0, op1, op2;

if(i <= j) { while(exp[i]) {

temp = arr[i]; if(isdigit(exp[i]))

arr[i] = arr[j]; push(exp[i] - '0');

arr[j] = temp; else {

i++; j--; op2 = pop();

} op1 = pop();

} switch(exp[i]) {

if(low < j) quicksort(arr, low, j); case '+': push(op1 + op2); break;

if(i < high) quicksort(arr, i, high); case '-': push(op1 - op2); break;

} case '*': push(op1 * op2); break;

int main() { case '/': push(op1 / op2); break;

int arr[] = {3, 6, 8, 10, 1, 2, 1}; }

int n = 7; }

quicksort(arr, 0, n-1); i++;

for(int i = 0; i < n; i++) }


printf("Result = %d\n", pop()); TOH(n-1, from, aux, to);

return 0; printf("Move disk %d from %c to %c\n", n,


from, to);
}
TOH(n-1, aux, to, from);
25. Write a program to implement a
simple calculator using switch }

#include <stdio.h> int main() {

int main() { TOH(3, 'A', 'C', 'B');

char op; return 0;

double a = 5.0, b = 2.0; }

op = '/'; 27. Write a program to merge two sorted


arrays
switch(op) {
#include <stdio.h>
case '+': printf("%.1f\n", a + b); break;
int main() {
case '-': printf("%.1f\n", a - b); break;
int a[] = {1, 3, 5}, b[] = {2, 4, 6}, c[6], i=0,
case '*': printf("%.1f\n", a * b); break;
j=0, k=0;
case '/': printf("%.1f\n", a / b); break;
while(i<3 && j<3) {
default: printf("Invalid operator\n");
if(a[i] < b[j]) c[k++] = a[i++];
}
else c[k++] = b[j++];
return 0;
}
}
while(i<3) c[k++] = a[i++];
26. Write a program to implement tower
while(j<3) c[k++] = b[j++];
of Hanoi
for(i=0; i<6; i++) printf("%d ", c[i]);
#include <stdio.h>
return 0;
void TOH(int n, char from, char to, char
aux) { }

if(n == 1) { 28. Write a program to implement


selection sort
printf("Move disk 1 from %c to %c\n",
from, to); #include <stdio.h>

return; int main() {

} int a[] = {64, 25, 12, 22, 11}, n = 5, i, j, min,


temp;
for(i = 0; i < n-1; i++) { freq[arr[i]]++;

min = i; for(int i = 0; i < 100; i++)

for(j = i+1; j < n; j++) if(freq[i] > 0)

if(a[j] < a[min]) min = j; printf("Element %d: %d times\n", i,


freq[i]);
temp = a[i]; a[i] = a[min]; a[min] = temp;
return 0;
}
}
for(i = 0; i < n; i++) printf("%d ", a[i]);

return 0;

29. Write a program to convert a decimal


number to binary

#include <stdio.h>

int main() {

int num = 10, binary[32], i = 0;

while(num > 0) {

binary[i++] = num % 2;

num /= 2;

for(int j = i - 1; j >= 0; j--)

printf("%d", binary[j]);

return 0;

30. Write a program to count frequency of


each element in array

#include <stdio.h>

int main() {

int arr[] = {1, 2, 2, 3, 3, 3}, freq[100] = {0},


n = 6;

for(int i = 0; i < n; i++)

You might also like