C Programming Important
Questions with Solutions
1. Storage Classes in C
1 #include <stdio.h>
2
3 void autoExample() {
4 auto int a = 10;
5 printf("Auto: %d\n", a);
6 }
7
8 void registerExample() {
9 register int r = 5;
10 printf("Register: %d\n", r);
11 }
12
13 void staticExample() {
14 static int s = 0;
15 s++;
16 printf("Static: %d\n", s);
17 }
18
19 int externVar = 100;
20 void externExample() {
21 extern int externVar;
22 printf("Extern: %d\n", externVar);
23 }
, 2. Prime Numbers Using Function
1 #include <stdio.h>
2
3 int isPrime(int n) {
4 for (int i = 2; i <= n / 2; i++)
5 if (n % i == 0) return 0;
6 return n > 1;
7 }
8
9 int main() {
10 int limit;
11 printf("Enter range: ");
12 scanf("%d", &limit);
13 for (int i = 1; i <= limit; i++)
14 if (isPrime(i)) printf("%d ", i);
15 return 0;
16 }
3. Recursive Fibonacci Series
1 #include <stdio.h>
2
3 int fibonacci(int n) {
4 if (n <= 1) return n;
5 return fibonacci(n - 1) + fibonacci(n - 2);
6 }
7
8 int main() {
9 int n;
10 printf("Enter number of terms: ");
11 scanf("%d", &n);
12 for (int i = 0; i < n; i++)
13 printf("%d ", fibonacci(i));
14 return 0;
15 }
Questions with Solutions
1. Storage Classes in C
1 #include <stdio.h>
2
3 void autoExample() {
4 auto int a = 10;
5 printf("Auto: %d\n", a);
6 }
7
8 void registerExample() {
9 register int r = 5;
10 printf("Register: %d\n", r);
11 }
12
13 void staticExample() {
14 static int s = 0;
15 s++;
16 printf("Static: %d\n", s);
17 }
18
19 int externVar = 100;
20 void externExample() {
21 extern int externVar;
22 printf("Extern: %d\n", externVar);
23 }
, 2. Prime Numbers Using Function
1 #include <stdio.h>
2
3 int isPrime(int n) {
4 for (int i = 2; i <= n / 2; i++)
5 if (n % i == 0) return 0;
6 return n > 1;
7 }
8
9 int main() {
10 int limit;
11 printf("Enter range: ");
12 scanf("%d", &limit);
13 for (int i = 1; i <= limit; i++)
14 if (isPrime(i)) printf("%d ", i);
15 return 0;
16 }
3. Recursive Fibonacci Series
1 #include <stdio.h>
2
3 int fibonacci(int n) {
4 if (n <= 1) return n;
5 return fibonacci(n - 1) + fibonacci(n - 2);
6 }
7
8 int main() {
9 int n;
10 printf("Enter number of terms: ");
11 scanf("%d", &n);
12 for (int i = 0; i < n; i++)
13 printf("%d ", fibonacci(i));
14 return 0;
15 }