

















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


















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.
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
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:
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
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; }
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.
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.
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:
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.
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.
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
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.
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.