Computer Fundamentals and C Programming - Answers
1. Formatted Input and Output Statements in C
Definition:
Formatted input/output statements in C are used to read and display data in a specific format.
They allow control over how the data is presented or read from the user.
Input:
- Function: scanf()
Syntax:
scanf("format_specifier", &variable);
Example:
int age;
scanf("%d", &age);
Output:
- Function: printf()
Syntax:
printf("format_specifier", variable);
Example:
printf("Age = %d", age);
Common Format Specifiers:
%d - int
%f - float
%c - char
%s - string
------------------------------------------------------------
2. Various Types of Operators in C
1. Arithmetic Operators: +, -, *, /, %
2. Relational Operators: ==, !=, >, <, >=, <=
3. Logical Operators: &&, ||, !
4. Assignment Operators: =, +=, -=, *=, /=, %=
5. Increment/Decrement Operators: ++, --
6. Bitwise Operators: &, |, ^, ~, <<, >>
7. Conditional Operator: ?:
------------------------------------------------------------
3. Short Note on Character Set
Character set in C includes:
- Letters: A-Z, a-z
- Digits: 0-9
- Special Characters: + - * / = < >
- White Spaces: space, tab, newline
- Escape Sequences: \n, \t, \\
------------------------------------------------------------
4. C Program to Check Prime Number
#include <stdio.h>
int main() {
int num, i, isPrime = 1;
printf("Enter a number: ");
scanf("%d", &num);
if (num <= 1) {
isPrime = 0;
} else {
for (i = 2; i <= num / 2; i++) {
if (num % i == 0) {
isPrime = 0;
break;
}
}
}
if (isPrime)
printf("%d is a Prime Number", num);
else
printf("%d is not a Prime Number", num);
return 0;
}
------------------------------------------------------------
5. Different Data Types in C
int - 2 or 4 bytes
float - 4 bytes
double - 8 bytes
char - 1 byte
void - 0 bytes
------------------------------------------------------------
6. Decision Making Statements in C
if statement:
if (a > b) { printf("a is greater"); }
if-else statement:
if (a > b) printf("a is greater");
else printf("b is greater");
if-else if ladder:
if (mark >= 90) printf("Grade A");
else if (mark >= 75) printf("Grade B");
else printf("Grade C");
switch statement:
switch (day) {
case 1: printf("Monday"); break;
case 2: printf("Tuesday"); break;
default: printf("Invalid");
}
------------------------------------------------------------
7. C Program to Find Factorial Using Iteration
#include <stdio.h>
int main() {
int n, i;
long long fact = 1;
printf("Enter a number: ");
scanf("%d", &n);
for (i = 1; i <= n; i++) {
fact *= i;
}
printf("Factorial of %d = %lld", n, fact);
return 0;
}