Docsity

Python Notes: Clear Concepts + Practical Examples, Study notes of Web Application Development

Toggle sidebar
bg1
Module 2
PROGRAMMING IN C
Q1. 2 MARKS QUESTIONS.
1. What is the purpose of the printf() function in C.
➢ The printf() function in C is used to display output on the screen.
It prints formatted text, numbers, or variable values to the standard output device
(monitor). It belongs to the stdio.h library and uses format specifiers such as %d,
%f, %c, and %s for different data types. printf() allows combining text with variable
output in one line. It can also include escape sequences like \n for a new line and \t
for a tab space.
Example:
printf("Sum = %d", sum); — displays the value of the variable sum on the screen.
Thus, it is one of the most commonly used functions for displaying program results
in C.
2.What is an arithmetic operation? Give example.
➢ Arithmetic operators are used to perform mathematical calculations on numerical
data. These include addition (+), subtraction (–), multiplication (*), division (/), and
modulus (%). They help perform basic arithmetic operations such as computing
totals, averages, or formulas. These operators work on both integer and floating-
point values. They follow a specific precedence, where multiplication and division
are done before addition and subtraction. Arithmetic operators are part of almost
every C program.
Example:
If a = 10 and b = 5, then a + b = 15, a / b = 2, and a % b = 0.
3. What is a bitwise operator? Give example.
➢ Bitwise operators in C are used to perform operations at the bit level.
They directly manipulate the binary representation of integers (0s and 1s).
The main bitwise operators are: & (AND), | (OR), ^ (XOR), ~ (NOT), << (Left Shift), and
>> (Right Shift). They are widely used in low-level programming, such as system
programming, encryption, and hardware control.
Example:
If a = 5 (0101) and b = 3 (0011), then a & b = 1 (0001) and a | b = 7 (0111).
Bitwise operators help improve efficiency when working with binary data.
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19

Partial preview of the text

Download Python Notes: Clear Concepts + Practical Examples and more Study notes Web Application Development in PDF only on Docsity!

PROGRAMMING IN C

Q1. 2 MARKS QUESTIONS.

  1. What is the purpose of the printf() function in C. ➢ The printf() function in C is used to display output on the screen. It prints formatted text, numbers, or variable values to the standard output device (monitor). It belongs to the stdio.h library and uses format specifiers such as %d, %f, %c, and %s for different data types. printf() allows combining text with variable output in one line. It can also include escape sequences like \n for a new line and \t for a tab space. Example: printf("Sum = %d", sum); — displays the value of the variable sum on the screen. Thus, it is one of the most commonly used functions for displaying program results in C. 2.What is an arithmetic operation? Give example. ➢ Arithmetic operators are used to perform mathematical calculations on numerical data. These include addition (+), subtraction (–), multiplication (*), division (/), and modulus (%). They help perform basic arithmetic operations such as computing totals, averages, or formulas. These operators work on both integer and floating- point values. They follow a specific precedence , where multiplication and division are done before addition and subtraction. Arithmetic operators are part of almost every C program. Example: If a = 10 and b = 5, then a + b = 15, a / b = 2, and a % b = 0.
  2. What is a bitwise operator? Give example. ➢ Bitwise operators in C are used to perform operations at the bit level. They directly manipulate the binary representation of integers (0s and 1s). The main bitwise operators are: & (AND), | (OR), ^ (XOR), ~ (NOT), << (Left Shift), and >> (Right Shift). They are widely used in low-level programming , such as system programming, encryption, and hardware control. Example: If a = 5 (0101) and b = 3 (0011), then a & b = 1 (0001) and a | b = 7 (0111). Bitwise operators help improve efficiency when working with binary data.

PROGRAMMING IN C

4.What is a constant in C? Give example. ➢ A constant is a fixed value in C that does not change during program execution. Constants are used to represent data that remains the same throughout the program. They can be defined using the #define preprocessor directive or the const keyword. Constants improve program readability and prevent accidental changes in values. There are different types of constants such as integer , floating , and character constants. Example: #define PI 3.14 or const int MAX = 100; — both define constant values. Using constants helps make programs more reliable and easier to modify. 5.Differentiate between keyword and identifdier in C. 6.What does puts() function do in C? ➢ The puts() function is used to display a string on the screen. It automatically prints the string followed by a newline character (\n). This function is simpler than printf() because it does not require format specifiers. It is defined in the stdio.h header file. puts() is often used to print messages or lines of text quickly. Example: puts("Welcome to C Programming!"); — displays the message and moves to the next line. Unlike printf(), it always ends with a new line, making it suitable for printing entire strings.

PROGRAMMING IN C

Q2. 5 MARKS QUESTIONS.

1.What are data types in C? Explain the difference between primar and derived data types with example. ➢ In C programming, a data type defines the type of data that a variable can store and the operations that can be performed on it. It determines the size, range, and type of values that can be used in a program. C provides several types of data to handle integers, floating-point numbers, characters, and more. A. Primary (Basic) Data Types

  • int : Stores integers (e.g., int age = 21;)
  • float : Single-precision floating-point numbers (e.g., float salary = 3000.5;)
  • double : Double-precision floating-point numbers (e.g., double pi = 3.141592;)
  • char : Single characters (e.g., char grade = 'A';) These types represent single values and are directly supported by the C language. B. Derived Data Types Derived data types are built from the basic types:
  • Array : Collection of elements of the same type (e.g., int marks[5];)
  • Pointer : Variable that stores the address of another variable (e.g., int* ptr;)
  • Structure : User-defined data grouping different types (e.g., struct student { char name[20]; int roll; };)
  • Union : Like structures but memory efficient, stores one member at a time.

PROGRAMMING IN C

2.What are operators in C? Classify operators and explain three categories with examples. ➢ Operators in C are symbols that perform operations on variables or values. They are used to perform calculations, comparisons, assignments, and logical decisions. Classification of Operators in C:

  1. Arithmetic Operators
  2. Relational Operators
  3. Logical Operators
  4. Assignment Operators
  5. Bitwise Operators
  6. Unary Operators
  7. Conditional (Ternary) Operator **Three categories with examples are as follows:
  8. Arithmetic Operators: Used to perform mathematical operations on data. Example: +, - , *, /, % int a = 10, b = 5; printf("Sum = %d", a + b); // Output: 15
  9. Relational Operators: Used to compare two values and return true (1) or false (0). Example: ==, !=, <, >, <=, >= if(a > b) printf("a is greater");
  10. Logical Operators: Used to combine conditions in decision-making statements. Example: && (AND), || (OR),! (NOT) if(a > 0 && b > 0) printf("Both are positive");**

PROGRAMMING IN C

Example Program: #include #include int main() { int a = 5 , b = 3 , c = 2 , result; result = a + b * c; printf("Result = %d", result); getch(); return 0 ; } Output: Result = 11 Explanation: b * c → 3 * 2 = 6 Then a + 6 = 11

PROGRAMMING IN C

4.Ramesh basic salary is input through the keyboard, his dearness allowance is 40% and rent allowance is 20% of his basic salary, Calculate his gross salary. ➢ Algorithm: Step 1: Start Step 2: Declare variables basic, da, hra, gross. Step 3: Input basic salary using scanf(). Step 4: Calculate da = 0.4 * basic and hra = 0.2 * basic. Step 5: Calculate gross = basic + da + hra. Step 6: Display gross salary using printf(). Step 7: Stop Example Code: #include #include int main(){ float basic, da, hra, gross; printf(“Enter Basic Salary”); scanf(“%f”, &basic); da = 0.4 * basic; hra = 0.2 * basic; gross = basic + hra + da; printf(“Gross Salary is :”, &gross); getch(); return 0; }

PROGRAMMING IN C

6.Write a C program to perform basic arithmetic operators. ➢ Arithmetic operations are used to perform mathematical calculations such as addition, subtraction, multiplication, division, and modulus. In this program, we will take two numbers as input and display all the arithmetic results. Algorithm: Step 1: Start Step 2: Declare variables a, b, sum, diff, prod, div, mod. Step 3: Read values of a and b. Step 4: Perform calculations using +, - , *, /, and %. Step 5: Display all results using printf(). Step 6: Stop *Example: #include #include int main(){ int a, b; printf(“Enter two numbers:”); scanf(“%d %d”, &a, &b); printf(“Sum = %d\n”, a + b); printf(“Difference = %d\n”, a – b ); printf(“Product = %d\n”, a * b); printf(“Quotient = %d\n”, a / b); printf(“Remainder = %d\n”, a % b); getch(); return 0; } Explanation: This program demonstrates the use of arithmetic operators: +, - , , /, and %. It takes two numbers as input and performs all five operations.

PROGRAMMING IN C

7.Discuss the structure of C program. ➢ A C program follows a specific structure made up of several sections. Each section has its own purpose and must appear in a particular order. The structure helps organize the program logically and makes it easy to read and debug. General Structure of a C Program: Documentation Section Link Section Definition Section Global Declaration Section main() Function Section User-defined Function Section

1. Documentation Section: Used for comments that describe the program’s purpose. Example: /* Program to find the sum of two numbers */ 2. Link Section: Includes header files that contain predefined functions. Example: #include 3. Definition Section: Used to define constants using #define. Example: #define PI 3. 4. Global Declaration Section: Contains global variables that can be used in all functions.

PROGRAMMING IN C

8.The distance between two cities is in kilometres, calculate the distance in meters, feet, inches and centimetres. ➢ This program converts a distance in kilometers into meters, feet, inches, and centimeters. Algorithm: Step 1: Start Step 2: Declare variables km, m, ft, in, cm. Step 3: Input the distance in kilometers. Step 4: Convert using formulas:

  • meters = km * 1000
  • centimeters = km * 100000
  • feet = km * 3280.
  • inches = km * 39370. Step 5: Display all converted values. Step 6: Stop C Program: #include #include int main() { float km, m, cm, ft, in; printf("Enter distance in kilometers: "); scanf("%f", &km); m = km * 1000; cm = km * 100000; ft = km * 3280.84; in = km * 39370.1; printf("\nDistance in meters = %.2f", m); printf("\nDistance in centimeters = %.2f", cm); printf("\nDistance in feet = %.2f", ft); printf("\nDistance in inches = %.2f", in); getch(); return 0; }

PROGRAMMING IN C

Output Example: Enter distance in kilometers: 2 Distance in meters = 2000. Distance in centimeters = 200000. Distance in feet = 6561. Distance in inches = 78740. Explanation: The program demonstrates unit conversions using multiplication formulas. It uses float data type to handle decimal values accurately.

PROGRAMMING IN C

10.If lengths of three sides of a triangle are input through the keyboard, write a program to find the area of the triangle. ➢ The area of a triangle can be calculated using Heron’s formula : If the sides are a, b, and c, and s is the semi-perimeter, then s = (a + b + c) / 2 Area = √(s × (s – a) × (s – b) × (s – c)) Algorithm: Step 1: Start Step 2: Declare variables a, b, c, s, area. Step 3: Input the three sides. Step 4: Calculate s = (a + b + c) / 2. Step 6: Calculate area = sqrt(s * (s - a) * (s - b) * (s - c)). Step 7: Display area. Step 8: Stop C Program: #include #include int main() { float a, b, c, s, area; printf("Enter the three sides of the triangle: "); scanf("%f %f %f", &a, &b, &c); s = (a + b + c) / 2; area = sqrt(s * (s - a) * (s - b) * (s - c)); printf("Area of the triangle = %.2f", area); getch(); return 0; } Output Example: Enter the three sides of the triangle: 5 6 7 Area of the triangle = 14. Explanation: The program uses the math.h library for the sqrt() function. It calculates the semi- perimeter and applies Heron’s formula to find the area accurately.

PROGRAMMING IN C

Q3. 10 MARKS QUESTIONS.

1.Explain different types of constants in C. Provide examples of each type. ➢ In C programming, a constant is a fixed value that cannot be changed during the execution of a program. Constants are used to represent values that remain the same throughout the program and are defined at the beginning of the code to improve readability and maintainability. They make a program more reliable and prevent accidental modification of important values. A constant can be either numeric or non-numeric. Constants are broadly classified into the following types:

1. Numeric Constants Numeric constants are those which represent numbers (either whole numbers or decimal values). They are further divided into Integer Constants and Floating-Point Constants. (a) Integer Constants Integer constants are whole numbers without any fractional part. They can be positive or negative but cannot contain decimal points. They must not include commas or spaces. There are three types of integer constants. Example: int a = 10; int b = 012; // Octal int c = 0xA2; // Hexadecimal (b) Floating-Point Constants Floating-point constants represent real numbers that contain decimal points or are expressed in exponential form. They are used when more precision is required in calculations. Example: float pi = 3.14; float rate = - 0.005; float exp = 2.5e3; // means 2.5 × 10³ = 2500

PROGRAMMING IN C

5. Constant Variables In addition to #define, C also provides a way to declare constants using the const keyword. When a variable is declared as const , its value cannot be changed once assigned. Example: const int x = 10; const float pi = 3.14; If you try to modify a constant variable (e.g., x = 20;), the compiler will show an error. This ensures that the value remains constant throughout the execution.

PROGRAMMING IN C

2.Explain the difference between Logical and Bitwise operators. ➢ In C programming, operators are symbols used to perform operations on data or variables. Among them, Logical operators and Bitwise operators are important types used for decision-making and data manipulation. Although both are used for comparisons or conditions, they operate differently on data. Logical operators work on logical or relational expressions and return either True (1) or False (0). Bitwise operators, on the other hand, work on the binary (bit) representation of integers, manipulating bits directly.

1. Logical Operators Logical operators are used to combine multiple conditions in decision-making statements such as if, while, or for. They produce a logical result — either true (1) or false (0). These operators are mainly used in conditional and control statements. Operator Name Description && Logical AND Returns true if both conditions are true ! Logical NOT Reverses the logical value of the condition Example: if(a > 0 && b > 0) printf("Both numbers are positive"); Here, the statement executes only if both conditions are true.