BESCK104E - Introduction to C Programming (Important Questions & Programs)
1. Define a C token. List types of tokens in C.
A C token is the smallest individual unit in a C program.
Types:
1. Keywords
2. Identifiers
3. Constants
4. Strings
5. Operators
6. Special symbols
2. Define data types in C with examples.
Data types specify the type of data a variable can hold.
Primary: int a=10; char ch='A'; float x=3.14;
Derived: arrays, pointers
User-defined: struct, union
3. Explain if, if-else, nested if-else.
if (a>0) { ... }
if-else: if (a%2==0) {...} else {...}
nested if-else: if (a>0) {...} else if (a<0) {...} else {...}
4. What are loops? Name types.
Loops repeat code.
Types: for, while, do-while.
5. Difference between while and do-while.
while: entry-controlled; may not run if condition false.
do-while: exit-controlled; runs at least once.
6. Explain switch with syntax.
switch(expr) { case val1: break; ... default: }
7. Explain printf and scanf.
printf("%d", a); -> output
scanf("%d", &a); -> input
8. Define array with syntax.
BESCK104E - Introduction to C Programming (Important Questions & Programs)
Array: collection of same data types.
int arr[5];
9. What is a function? Advantages.
Function = block of code for a task.
Advantages: reuse, modularity, easier debugging.
10. Call by value vs Call by reference.
Value: copy passed. Reference: address passed.
Original not changed (value), may change (reference).
BESCK104E - Introduction to C Programming (Important Questions & Programs)
Important Programs
1. Fibonacci Series
#include <stdio.h>
int main() {
int n, a=0, b=1, c;
printf("Enter n: ");
scanf("%d", &n);
printf("%d %d ", a, b);
for(int i=3; i<=n; i++) {
c = a + b;
printf("%d ", c);
a = b;
b = c;
}
return 0;
}
2. Palindrome Check
#include <stdio.h>
int main() {
int num, rev=0, temp, digit;
scanf("%d", &num);
temp = num;
while(temp != 0) {
digit = temp % 10;
rev = rev*10 + digit;
temp = temp / 10;
}
if(num == rev) printf("Palindrome");
else printf("Not Palindrome");
return 0;
}
3. Factorial
#include <stdio.h>
int main() {
int n, fact = 1;
scanf("%d", &n);
for(int i=1; i<=n; i++)
fact *= i;
BESCK104E - Introduction to C Programming (Important Questions & Programs)
printf("Factorial = %d", fact);
return 0;
}
4. Prime Number
#include <stdio.h>
int main() {
int n, flag=0;
scanf("%d", &n);
for(int i=2; i<=n/2; i++) {
if(n % i == 0) {
flag = 1;
break;
}
}
if(flag == 0) printf("Prime");
else printf("Not Prime");
return 0;
}
5. Array Sum
#include <stdio.h>
int main() {
int a[100], n, sum=0;
scanf("%d", &n);
for(int i=0; i<n; i++) {
scanf("%d", &a[i]);
sum += a[i];
}
printf("Sum = %d", sum);
return 0;
}
6. Reverse a String
#include <stdio.h>
#include <string.h>
int main() {
char str[100];
gets(str);
int len = strlen(str);
BESCK104E - Introduction to C Programming (Important Questions & Programs)
for(int i=len-1; i>=0; i--)
printf("%c", str[i]);
return 0;
}
7. Largest of 3
#include <stdio.h>
int main() {
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
if(a > b && a > c)
printf("%d", a);
else if(b > c)
printf("%d", b);
else
printf("%d", c);
return 0;
}
8. Swap Numbers
#include <stdio.h>
int main() {
int a, b, temp;
scanf("%d%d", &a, &b);
temp = a; a = b; b = temp;
printf("a = %d, b = %d", a, b);
return 0;
}
9. Sum of Digits
#include <stdio.h>
int main() {
int n, sum=0;
scanf("%d", &n);
while(n != 0) {
sum += n % 10;
n /= 10;
}
printf("Sum = %d", sum);
return 0;
BESCK104E - Introduction to C Programming (Important Questions & Programs)
10. Simple Calculator
#include <stdio.h>
int main() {
char op;
float a, b;
scanf(" %c", &op);
scanf("%f%f", &a, &b);
switch(op) {
case '+': printf("%.2f", a + b); break;
case '-': printf("%.2f", a - b); break;
case '*': printf("%.2f", a * b); break;
case '/': if(b != 0) printf("%.2f", a / b);
else printf("Error"); break;
default: printf("Invalid");
}
return 0;
}