0% found this document useful (0 votes)
4 views

UNIT1 c

The document provides an overview of fundamental concepts in C programming, including the purpose of scanf() and printf(), rules for naming variables, data types, and the structure of a C program. It also explains algorithms, flowcharts, and pseudocode, along with examples and comparisons of various operators and constants in C. Additionally, it includes flowcharts for finding the greatest and smallest of three numbers, as well as a C program to calculate the sum of five non-negative numbers.

Uploaded by

roopanjali.1509
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

UNIT1 c

The document provides an overview of fundamental concepts in C programming, including the purpose of scanf() and printf(), rules for naming variables, data types, and the structure of a C program. It also explains algorithms, flowcharts, and pseudocode, along with examples and comparisons of various operators and constants in C. Additionally, it includes flowcharts for finding the greatest and smallest of three numbers, as well as a C program to calculate the sum of five non-negative numbers.

Uploaded by

roopanjali.1509
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

UNIT1------------------------------------------------------------------------------------------------------

1. What is the purpose of scanf() and printf() statements?

• scanf() is used to take input from the user, while printf() is used to display output to the user.

2. List the rules for naming variables in C language.

• A variable name must start with a letter or an underscore.

• It can contain letters, digits, and underscores.

• It cannot be a keyword.

• It is case-sensitive.

3. Show the output of the following C program.

#include <stdio.h>

int main()

int xyz = -6;

printf(xyz > 0 ? "Positive" : "Negative");

return 0;

Output:
Negative

4. List the C Tokens available in C.

• Keywords, identifiers, constants, string literals, operators, punctuation.

5. Compare the statement between a = 5 and a == 5 in C language.

• a = 5 is an assignment, while a == 5 is a comparison operator.

6. Name any four commonly used keywords in the C language.

• int, if, while, return.


7. Define time complexity.

• Time complexity refers to the time an algorithm takes to execute as a function of the size of the
input.

8. Compare algorithm and pseudocode.

• Algorithm: A step-by-step procedure to solve a problem.

• Pseudocode: A high-level description of an algorithm using a mixture of natural language and


programming concepts.

9. Find an algorithm to swap two numbers.

1. Store a in a temporary variable.

2. Assign b to a.

3. Assign the temporary variable to b.

10. Show the different data types available in C.

• int, float, char, double, void.

11. What is the difference between = and == operator?

• = is an assignment operator, while == is a comparison operator.

12. Evaluate the following C expression: A = 8 + 11 * 2 / 5 - 2 + 4

• Answer: A = 14.

13. List the characteristics of an algorithm.

1. Finiteness

2. Definiteness

3. Input

4. Output

5. Effectiveness

14. Define the term keyword and identifier.

• Keyword: Reserved word with a predefined meaning.


• Identifier: A user-defined name for variables, functions, etc.

15. Calculate the output of the following pseudocode for input: p = 3, q = 8, r = 1.

Pseudocode:

Integer p, q, r, sum

Read p, q, r

Set sum = p + q + r

if ((p NOT EQUALS 0) and (sum EQUALS 11) and (q EQUALS 4) and (r NOT EQUALS 0))

Print "Success"

Otherwise

Print "Fail"

End if

Output:
Fail

12 marks----------------------------------------------------------------------------------

1. Explain in detail the various data types used in C, and write a C program to calculate the sum of five non-
negative numbers.

Various Data Types in C:

C language supports a rich set of data types. They are classified into:

1. Basic Data Types:

o int: Represents integer values. Example: int a = 5;

o char: Represents a single character. Example: char letter = 'A';

o float: Represents a floating-point number. Example: float price = 10.5;

o double: Represents a double-precision floating-point number. Example: double pi = 3.141592;

2. Derived Data Types:

o Arrays: A collection of elements of the same data type.

o Pointers: Stores the memory address of a variable.

o Structures: A collection of different data types grouped together.

3. Enumerated Data Type:

o enum: A data type that consists of a set of named values. Example:

o enum day {Monday, Tuesday, Wednesday};

4. Void Data Type:


o Represents an empty or no type. It’s often used in functions that don’t return a value.

C Program to Calculate the Sum of Five Non-Negative Numbers:

#include <stdio.h>

int main() {

int num1, num2, num3, num4, num5, sum;

// Reading the five non-negative numbers

printf("Enter five non-negative numbers: ");

scanf("%d %d %d %d %d", &num1, &num2, &num3, &num4, &num5);

// Calculating the sum

sum = num1 + num2 + num3 + num4 + num5;

// Displaying the sum

printf("The sum of the numbers is: %d", sum);

return 0;

This program prompts the user to input five non-negative numbers and then calculates and prints their sum.

2. Explain the structure of a C program with a suitable example.

A C program consists of the following basic components:

1. Preprocessor Directives:

o These are instructions given to the preprocessor. They are used to include header files, define
constants, etc.

o Example: #include <stdio.h>

2. Global Declarations:

o Variables and functions that are declared outside of any function, typically at the top.

3. Main Function:

o This is the entry point of every C program. It is where execution begins.

o Example: int main() { }

4. Local Declarations:

o Variables that are declared inside functions (usually main()).


5. Statements and Expressions:

o These are the actual instructions that the program executes.

6. Return Statement:

o Indicates the termination of the main() function and returns control to the operating system.

Example:

#include <stdio.h> // Preprocessor directive

int main() { // Main function

int a = 5, b = 10, sum;

sum = a + b; // Statement to calculate sum

printf("Sum: %d", sum); // Output the result

return 0; // End the program

3. Explain with example, the various C tokens in C language.

C Tokens are the smallest units in C programs. They can be classified as:

1. Keywords:

o Reserved words that have special meaning in C. Example: int, return, if.

2. Identifiers:

o Names given to various program elements like variables, functions, etc.

o Example: int number;

3. Constants:

o Literals representing fixed values.

o Example: 5, 'A', 3.14

4. String Literals:

o A sequence of characters enclosed in double quotes.

o Example: "Hello, World!"

5. Operators:

o Special symbols that perform operations on variables or values.

o Example: +, -, *, /

6. Punctuation:

o Symbols used to separate statements and define the structure of the program.

o Example: {, }, ;, ,
4. Illustrate the basic organization of a computer system and its components with a neat sketch.

A computer system can be broadly divided into hardware and software components. Here is an illustration:

1. Hardware:

o Central Processing Unit (CPU): Executes instructions.

o Memory (RAM): Temporary storage for data and instructions.

o Input Devices: Keyboard, Mouse, etc.

o Output Devices: Monitor, Printer, etc.

o Storage Devices: Hard Drive, SSD, etc.

2. Software:

o System Software: Operating systems, device drivers.

o Application Software: Word processors, browsers.

Sketch:

+---------------------------------+

| Input Devices |

| (Keyboard, Mouse, etc.) |

+---------------------------------+

+---------------------------------+

| Central Processing Unit |

| (CPU, ALU, Control Unit) |

+---------------------------------+

+---------------------------------+

| Memory (RAM) |

+---------------------------------+

+---------------------------------+

| Storage Devices (Hard disk, SSD) |

+---------------------------------+

V
+---------------------------------+

| Output Devices (Monitor, Printer) |

+---------------------------------+

5. Explain with example, the various constants available in C language.

In C, constants are fixed values used in the program that do not change during execution.

1. Integer Constants:

o Example: 5, 100, -12

2. Floating-point Constants:

o Example: 3.14, -0.001

3. Character Constants:

o Represent single characters enclosed in single quotes.

o Example: 'A', '7'

4. String Constants:

o A sequence of characters enclosed in double quotes.

o Example: "Hello"

5. Escape Constants:

o Special characters representing certain control actions.

o Example: \n (new line), \t (tab)

6. Discuss the basic structure of a computer system with a neat sketch.

The basic structure of a computer system consists of hardware, software, and users interacting with the system.

1. Hardware: Includes the CPU, memory, and input/output devices.

2. Software: Operating systems and application programs that interact with the hardware.

3. Users: People who interact with the system via input devices and receive output.

Sketch:

+------------------+ +-------------------+ +------------------+

| Users | <----> | Software | <----> | Hardware |

| (Input/Output) | | (OS, Apps, Drivers)| | (CPU, Memory, IO)|

+------------------+ +-------------------+ +------------------+

7. Summarize the concepts of algorithms, flowcharts, and pseudocode, providing suitable examples for each.

• Algorithm: A step-by-step procedure to solve a problem.

o Example: To find the sum of two numbers:


1. Start

2. Read two numbers, A and B

3. Add A and B, store in sum

4. Display sum

5. End

• Flowchart: A diagrammatic representation of an algorithm.

o Example: For finding the largest of three numbers:

o Start -> Read A, B, C -> If A > B & A > C -> Print A

o Else if B > C -> Print B

o Else -> Print C -> End

• Pseudocode: A plain language description of the steps in an algorithm.

o Example:

o Start

o Read A, B, C

o If A > B and A > C

o Print A

o Else if B > C

o Print B

o Else

o Print C

o End

8. Explain any five operators used in C language with suitable examples.

1. Arithmetic Operators: Perform mathematical operations.

o Example: +, -, *, /, %

o a + b: Addition

2. Relational Operators: Used to compare two values.

o Example: ==, !=, <, >, <=, >=

o a == b: Checks if a is equal to b.

3. Logical Operators: Used for logical operations.

o Example: &&, ||, !

o a && b:

Logical AND
4. Assignment Operator: Used to assign values.

o Example: =

o a = 5: Assigns 5 to a.

5. Increment and Decrement Operators: Used to increase or decrease a variable’s value by 1.

o Example: ++a, --a

o a++: Increments a by 1.

9. Define flowchart. Explain its symbols in detail and draw the flowchart to find the greatest of three numbers.

Flowchart Definition:

A flowchart is a diagram that represents the flow of a process or algorithm, showing the steps and the logical flow of
control. It uses standardized symbols to illustrate the sequence of operations.

Flowchart Symbols:

1. Oval (Terminal Symbol): Used to represent the start and end of a process.

o Example: "Start" and "End"

2. Rectangle (Process Symbol): Represents a process or action, such as calculations or assignments.

o Example: "a = 5 + 7"

3. Parallelogram (Input/Output Symbol): Represents input or output operations.

o Example: "Read x" or "Display result"

4. Diamond (Decision Symbol): Used for decision-making, represents a condition (true or false).

o Example: "Is A > B?"

5. Arrow (Flow Line): Represents the flow of control between steps.

Flowchart to Find the Greatest of Three Numbers:

+------------------+

| Start |

+------------------+

+------------------+

| Read A, B, C |

+------------------+

+-----------------------+

| Is A > B & A > C? |


+-----------------------+

Yes | No

V |

+------------------+ +-------------------+

| Print A | | Is B > C? |

+------------------+ +-------------------+

| |

V V

+------------------+ +-------------------+

| End | | Print B |

+------------------+ +-------------------+

+------------------+

| End |

+------------------+

10. Summarize the purpose of a flowchart and explain its uses. What are the basic symbols used in flowcharts?
Draw a flowchart to find the smallest of three numbers.

Purpose of a Flowchart:

• Clarifies Process: Helps in understanding the sequence of steps and the logic of a program or process.

• Improves Communication: Easy to understand and communicate the logic of a program or algorithm.

• Error Detection: Helps in identifying logical errors and inefficiencies early in the development stage.

• Documentation: Acts as a visual representation of a process for future reference or for others to
understand easily.

Basic Flowchart Symbols:

• Oval (Terminal Symbol): Used to mark the start and end of the process.

• Rectangle (Process Symbol): Represents a step or action, like calculation or assignment.

• Parallelogram (Input/Output Symbol): Used for input and output operations.

• Diamond (Decision Symbol): Represents decisions or conditional logic.

• Arrow (Flow Line): Indicates the direction of the process flow.

Flowchart to Find the Smallest of Three Numbers:

+------------------+
| Start |

+------------------+

+------------------+

| Read A, B, C |

+------------------+

+-----------------------+

| Is A < B & A < C? |

+-----------------------+

Yes | No

V |

+------------------+ +-------------------+

| Print A | | Is B < C? |

+------------------+ +-------------------+

| |

V V

+------------------+ +-------------------+

| End | | Print B |

+------------------+ +-------------------+

+------------------+

| End |

+------------------+

11. Explain in detail the various data types used in C, and write a C program to calculate the sum of five non-
negative numbers.

Data Types in C
In C programming, data types define the type of data that a variable can hold. C provides several built-in data types,
which can be categorized as follows:

1. Basic Data Types:

o int: Used to store integers (whole numbers) without decimal points. Size typically 4 bytes.

o char: Used to store single characters. Size typically 1 byte.

o float: Used to store single precision floating-point numbers (decimal values). Size typically 4
bytes.

o double: Used to store double precision floating-point numbers (decimal values with more
precision than float). Size typically 8 bytes.

2. Derived Data Types:

o Array: Collection of similar data types stored in contiguous memory locations.

o Pointer: Stores the memory address of another variable.

o Structure: A user-defined data type that groups different data types together.

o Union: Similar to structures but uses the same memory space for all members.

3. Void Data Type:

o void: Represents a function that does not return any value or a pointer that doesn't point to any
specific data type.

C Program to Calculate the Sum of Five Non-Negative Numbers

#include <stdio.h>

int main() {

int num1, num2, num3, num4, num5, sum;

// Input five non-negative numbers

printf("Enter five non-negative numbers:\n");

scanf("%d %d %d %d %d", &num1, &num2, &num3, &num4, &num5);

// Calculate the sum

sum = num1 + num2 + num3 + num4 + num5;

// Output the result

printf("The sum of the numbers is: %d\n", sum);

return 0;

}
Explanation:

• This program prompts the user to input five non-negative numbers.

• It then calculates their sum by adding all five numbers.

• Finally, it displays the sum of the five numbers on the screen.

12. Explain the structure of a C program with a suitable example.

Structure of a C Program

A basic C program consists of the following components:

1. Preprocessor Directives:

o These are instructions that are processed by the preprocessor before the actual compilation. For
example, #include <stdio.h> is used to include the standard input-output header file.

2. Global Declarations:

o Variables, constants, and other resources that can be used throughout the program.

3. Main Function:

o The main() function is the starting point of any C program. Every C program must have this
function.

4. Function Definitions:

o Other functions that perform specific tasks, if needed. Functions can be called from main() or
other functions.

5. Statements and Expressions:

o Inside the main function (or other functions), you write the logic, including variable declarations,
conditional statements, loops, and function calls.

Basic Structure of a C Program:

#include <stdio.h> // Preprocessor directive

// Function declarations (if any)

void display(); // Function prototype

int main() {

// Variable declarations

int num1, num2, sum;

// Input operation

printf("Enter two numbers: ");

scanf("%d %d", &num1, &num2);


// Calculate sum

sum = num1 + num2;

// Output operation

printf("The sum of the two numbers is: %d\n", sum);

// Calling a user-defined function

display();

return 0; // Return to the operating system

// Function definition

void display() {

printf("This is a user-defined function\n");

Explanation of the Structure:

1. Preprocessor Directive:

o #include <stdio.h>: Includes the standard input-output library necessary for using printf and scanf.

2. Global Declarations:

o The display() function is declared above the main() function, which is a prototype that tells the
compiler about the function's name and return type.

3. Main Function:

o The program starts execution from the main() function.

o It declares two integer variables num1 and num2, takes input for these variables, computes their
sum, and displays the result.

o A user-defined function display() is also called inside the main() function to show how functions
work.

4. Function Definitions:

o The display() function simply prints a message. This shows how functions are defined and called in
C.

13. Write a C program to input electricity unit consumption and calculate the total electricity bill according to
the given condition:

Electricity Bill Calculation:


• For the first 100 units: Rs. 0.50/unit

• For the next 100 units: Rs. 0.75/unit

• For the next 100 units: Rs. 1.20/unit

• For units above 250: Rs. 1.50/unit

• An additional surcharge of 20% is added to the bill.

C Program:

#include <stdio.h>

int main() {

float units, bill = 0.0;

// Input the units of electricity consumed

printf("Enter electricity units consumed: ");

scanf("%f", &units);

// Calculate the bill based on units consumed

if (units <= 100) {

bill = units * 0.50;

} else if (units <= 200) {

bill = 100 * 0.50 + (units - 100) * 0.75;

} else if (units <= 300) {

bill = 100 * 0.50 + 100 * 0.75 + (units - 200) * 1.20;

} else {

bill = 100 * 0.50 + 100 * 0.75 + 100 * 1.20 + (units - 300) * 1.50;

// Apply 20% surcharge

bill += bill * 0.20;

// Display the total bill

printf("Total electricity bill: Rs. %.2f", bill);

return 0;
}

This program calculates the total electricity bill based on the consumption and applies the appropriate surcharge.

You might also like