C Programming
C Programming
PART-A
1. Define an Algorithm.
This loop starts with i as 10 and reduces it by 1 in each iteration. It continues until i becomes less
than 1, printing numbers from 10 to 1 in descending order.
A header file in C contains definitions and declarations of functions, macros, and variables,
which can be included in programs using the #include directive. For example, stdio.h is used for
input/output operations, and math.h provides mathematical functions. These header files make
code reusable and reduce redundancy.
plaintext
CopyEdit
Start
Input number
If number % 2 == 0
Print "Even"
Else
Print "Odd"
End
This pseudocode checks if the remainder when the number is divided by 2 is zero. If true, the
number is even; otherwise, it is odd.
#include<stdio.h>
int main()
{
int number;
for(number=1;number<=10;number++)
{
printf("%d\n",number);
}
return 0;
}
A variable in C is a named memory location used to store a value that can change during
program execution. For example, int age = 25; declares a variable age of type integer, which
stores the value 25. Variables allow the program to store, retrieve, and manipulate data
dynamically.
The break statement is used to exit a loop or switch statement prematurely when a specific
condition is met. The continue statement skips the remaining code in the current iteration of the
loop and proceeds to the next iteration. Both statements control the flow of loops, enhancing
flexibility in programming.
Keywords are reserved words in C that have predefined meanings and specific purposes, such as
int, while, and return. Using them as identifiers would cause conflicts, as the compiler interprets
them for their predefined functionalities. This would result in syntax errors and make the
program invalid.
if (condition) {
// Statements if condition is true
} else {
// Statements if condition is false
}
The if part executes when the condition evaluates to true, while the else part executes if the
condition is false. This allows the program to make decisions based on conditions.
Variable names must begin with a letter or an underscore but not a digit. They can include
letters, digits, and underscores but must not match C keywords. Variable names are case-
sensitive, meaning Age and age are treated as different variables.
12. List the different format specifiers used in C for input and output operations.
Format specifiers define the type of data to be read or printed in C programs. Common ones
include %d for integers, %f for floating-point numbers, %c for characters, %s for strings, and
%lf for double-precision floating-point numbers. These specifiers are used with functions like
scanf and printf.
C operators are categorized into arithmetic, relational, logical, bitwise, and assignment operators.
Arithmetic operators like + and - perform basic calculations, while relational operators like < and
> compare values. Logical operators such as && and || enable complex condition evaluations,
and assignment operators like = assign values to variables.
In the while loop, the condition is evaluated before executing the loop body, so it may not
execute if the condition is initially false. In the do-while loop, the body executes at least once
because the condition is evaluated after the loop. Thus, do-while guarantees at least one iteration.
15. Write a C program to perform logical operations on two numbers provided by the user.
#include <stdio.h>
int main() {
int a, b;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
printf("Logical AND (a && b): %d\n", a && b);
printf("Logical OR (a || b): %d\n", a || b);
printf("Logical NOT (!a): %d\n", !a);
return 0;
}
This program performs logical operations like AND, OR, and NOT on two input numbers. It
demonstrates how logical operators work on integer inputs in C.
PART-B
1. Explain the general structure of a C program with an example. Discuss each part of the
structure in detail.
Example:
// User-defined function
void greet() {
printf("Hello from the function!\n");
}
int main() {
// Local variable declaration
int localVar = 5;
// Statements
printf("Global Variable: %d\n", globalVar);
greet();
● The #include <stdio.h> directive allows the use of input-output functions like printf.
● The global variable globalVar can be accessed by any function in the program.
● The greet() function is a user-defined function, which is called from the main() function
to display a message.
● Inside the main() function, a local variable localVar is declared and used only within the
scope of the function.
This structure ensures clarity and modularity, making the program easy to understand and
maintain.
2. Draw a flowchart to find the maximum of three numbers and explain it.
Factorial of a number n is the product of all positive integers from 1 to n. It is denoted as n!. For
example, 5! = 5 × 4 × 3 × 2 × 1 = 120.
Pseudocode:
START
INPUT number n
SET factorial = 1
FOR i = 1 to n
factorial = factorial * i
END FOR
PRINT factorial
STOP
Explanation of Pseudocode:
4. Discuss the different categories of operators in the C programming language with relevant
examples.
Operators in C programming are symbols used to perform specific operations on variables and
values. These operations include arithmetic, comparison, logical evaluation, and more. Operators
are broadly categorized into several types:
1. Arithmetic Operators:
These operators are used for basic mathematical calculations like addition, subtraction,
multiplication, division, and modulus.
Operator Meaning Example Output
+ Addition 5+3 8
- Subtraction 5-3 2
* Multiplication 5*3 15
/ Division 15 / 3 5
% Modulus (Remainder) 15 % 4 3
Example Code:
int a = 10, b = 3;
printf("Addition: %d\n", a + b);
printf("Modulus: %d\n", a % b);
2. Relational Operators:
Relational operators are used to compare two values. The result is either true (1) or false (0).
Operator Meaning Example Output
== Equal to 5 == 3 0
Operator Meaning Example Output
!= Not equal to 5 != 3 1
> Greater than 5>3 1
< Less than 5<3 0
>= Greater than or equal to 5 >= 3 1
<= Less than or equal to 5 <= 3 0
Example Code:
int x = 10, y = 20;
if (x < y)
{
printf("x is smaller than y\n");
}
3. Logical Operators:
Logical operators combine two or more conditions.
Operator Meaning Example Output
&& Logical AND (5 > 3) && (4 > 2) 1
` Logical OR ‘5
! Logical NOT !(5 > 3) 0
Example Code:
int a = 10, b = 5;
if (a > b && b > 0) {
printf("Both conditions are true\n");
}
4. Bitwise Operators:
Bitwise operators perform operations at the bit level.
Operator Meaning Example Output
& Bitwise AND 5&3 1
` ` Bitwise OR `5
^ Bitwise XOR 5^3 6
~ Bitwise NOT ~5 -6
5. Assignment Operators:
Assignment operators are used to assign values to variables.
Operator Meaning Example Output
= Assign value a = 10 10
+= Add and assign a += 5 15
-= Subtract and assign a -= 2 8
*= Multiply and assign a *= 3 30
/= Divide and assign a /= 2 5
%= Modulus and assign a %= 3 1
Example Code:
int a = 10;
a += 5; // Equivalent to a = a + 5
printf("Updated value of a: %d\n", a);
Example Code:
int a = 5, b = 10;
int max = (a > b) ? a : b;
printf("Maximum value: %d\n", max);
8. Special Operators:
C includes some special operators for specific tasks:
Example Code:
int a = 5;
printf("Size of int: %zu\n", sizeof(a)); // Size in bytes
printf("Address of a: %p\n", &a); // Prints the address of a
Conclusion:
C programming offers a wide range of operators for performing operations efficiently.
Understanding these operators is essential for building robust and logical programs. Each type of
operator serves a unique purpose, enabling developers to write optimized and readable code.
5. Discuss the character set used in C and explain the different data types available in C
with examples.
Character Set in C:
The character set in C includes letters, digits, special symbols, and white spaces that can be used
to construct programs.
1. Letters:
o Uppercase letters: A-Z
o Lowercase letters: a-z
2. Digits:
o Numbers: 0-9
3. Special Symbols:
o ~, !, @, #, $, %, ^, &, *, (, ), {}, [], ;, : etc.
4. White Space Characters:
o Space, tab (\t), newline (\n), carriage return (\r).
The character set ensures that programs can be written using human-readable symbols.
Data Types in C:
C provides a variety of data types to handle different kinds of data. These are broadly classified
into four categories:
int a = 10;
int *p = &a; // Pointer to a
return 0;
}
Output:
Integer: 10
Float: 5.50
Character: A
Double: 3.14159
6. Write a program to calculate the grade of a student based on marks in five subjects.
Grading Criteria:
Program:
#include <stdio.h>
int main() {
int marks[5], i;
float total = 0, percentage;
char grade;
// Print results
printf("Total Marks: %.2f\n", total);
printf("Percentage: %.2f%%\n", percentage);
printf("Grade: %c\n", grade);
return 0;
}
Sample Input and Output:
Enter marks for subject 1: 85
Enter marks for subject 2: 78
Enter marks for subject 3: 92
Enter marks for subject 4: 74
Enter marks for subject 5: 88
int main() {
float a, b, c, discriminant, root1, root2, realPart, imagPart;
// Input coefficients
printf("Enter coefficients a, b, and c: ");
scanf("%f %f %f", &a, &b, &c);
// Calculate discriminant
discriminant = (b * b) - (4 * a * c);
return 0;
}
Explanation of Code:
Algorithm:
1. Step 1: Start.
2. Step 2: Input N, the number of natural numbers to be printed.
3. Step 3: Initialize a counter variable, i = 1.
4. Step 4: Repeat the following steps while i <= N:
o Print the value of i.
o Increment i by 1.
5. Step 5: Stop.
9. Enumerate the difference between ‘else-if ladder’ and ‘switch-case’ statements with
appropriate C programs.
switch (choice) {
case 1:
printf("You chose 1\n");
break;
case 2:
printf("You chose 2\n");
break;
case 3:
printf("You chose 3\n");
break;
default:
printf("Invalid choice\n");
}
return 0;
}
Sample Output:
css
CopyEdit
Enter a number (1-3): 2
You chose 2