Here are the answers to all your 5-mark C programming questions compiled for PDF:
1. Structure of a C Program
Structure: 1. Preprocessor Directives 2. Global Declarations (Optional) 3. main() Function 4. Local
Declarations 5. Statements & Expressions 6. Function Definitions (Optional)
Example:
#include <stdio.h>
int main() {
int a, b, sum;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
sum = a + b;
printf("Sum = %d\n", sum);
return 0;
}
2. Data Types in C
Types: 1. Basic: int, float, double, char 2. Derived: Arrays, Pointers, Functions 3. Enumeration: enum 4.
Void: void
Example:
int a = 10;
float f = 3.14;
char c = 'A';
enum Day {Mon, Tue, Wed};
void display() { printf("Hello"); }
3. Types of Operators in C
1. Arithmetic: +, -, *, /, %
2. Relational: ==, !=, >, <, >=, <=
3. Logical: &&, ||, !
4. Assignment: =, +=, -=, *=, /=, %=
5. Increment/Decrement: ++, --
6. Miscellaneous: ?, sizeof(), ,
Example:
1
int a = 5, b = 3;
int max = (a > b) ? a : b;
4. Decision Making in C
1. if
2. if-else
3. if-else if-else ladder
4. switch
Examples:
if(a>0) printf("Positive");
else printf("Negative");
switch(day){case 1: printf("Mon"); break; default: printf("Other");}
5. Looping in C
1. while (entry-controlled)
2. do-while (exit-controlled)
3. for (entry-controlled)
Examples:
for(int i=1;i<=5;i++) printf("%d ", i);
int i=1; while(i<=5){printf("%d ",i); i++;}
int i=1; do{printf("%d ",i); i++;}while(i<=5);
6. Largest of Three Numbers Using if-else
#include <stdio.h>
int main(){
int a,b,c;
scanf("%d %d %d", &a,&b,&c);
if(a>b && a>c) printf("Largest: %d", a);
else if(b>c) printf("Largest: %d", b);
else printf("Largest: %d", c);
return 0;
}
2
7. Check Prime Number
#include <stdio.h>
int main(){
int num, i, flag=0;
scanf("%d", &num);
if(num<=1) printf("Not prime");
else{
for(i=2;i<=num/2;i++){
if(num%i==0){ flag=1; break; }
}
if(flag==0) printf("Prime");
else printf("Not prime");
}
return 0;
}
8. Entry-Controlled vs Exit-Controlled Loops
Feature Entry-Controlled Exit-Controlled
Condition Before execution After execution
Executes at least once? No Yes
Examples while, for do-while
9. Difference Between switch and if-else
Feature if-else switch
Condition Complex Discrete values only
Syntax if, else if, else switch, case, default
Flow Sequential Direct jump to matching case
Default else default
Examples:
if(marks>=90) printf("A"); else printf("B");
switch(day){case 1: printf("Mon"); break; default: printf("Other");}