PRINCIPLE OF PROGRAMMING USING C
BPOPS103
MODULE1
QUESTION AND ANSWER’S
1. Discuss the Structure of C program with an example.
Ans :
Structure of a C Program :
Definition: A C program consists of various sections, including preprocessor
directives, main function, and user-defined functions. The structure ensures
readability and maintainability.
#include <stdio.h>
int main()
{
int a = 10;
printf("Value of a: %d", a);
return 0;
}
Explanation:
#include <stdio.h>: Includes standard input/output library.
main(): Entry point of execution.
printf(): Prints output to console.
return 0;: Indicates successful execution.
2. What are Variables? Write the rules to declare a variable.
Ans :
Definition: A variable in C is a named memory location used to store data that
can be modified during program execution.
Rules for Declaring Variables:
1. Must begin with a letter or an underscore (_).
2. Can contain letters, digits, and underscores.
3. Cannot be a reserved keyword.
4. Case-sensitive.
5. Should not start with a digit.
Example:
int age = 25;
float price = 12.5;
char grade = 'A';
3. What are Escape sequences? Mention the escape sequences of C
languages with their meaning.
Ans :
Escape Sequences
Definition: Escape sequences are special character combinations in C that represent
certain characters.
Common Escape Sequences:
Escape Sequence Meaning
\n Newline
\t Tab
\" Double quote
\' Single quote
\\ Backslash
Example:
#include <stdio.h>
int main() {
printf("Hello\nWorld\tTabbed\n");
printf("She said, \"Hello!\"\n");
printf("Printing a backslash: \\\n");
return 0;
}
Explanation:
\n moves text to a new line.
\t inserts a tab space.
\" allows printing double quotes.
\\ prints a backslash.
4. Explain any three input and output devices in detail.
Ans :
Input and Output Devices
Keyboard (Input Device): Used to enter data into a computer.
Mouse (Input Device): text and commands.
Mouse - A pointing device used for navigation.
Monitor (Output Device): Displays information to the user.Scanner - Converts
printed documents into digital form.
Joystick - Used for gaming and simulations.
Microphone - Captures audio input.
Output Devices:
Monitor - Displays visual output.
Printer - Produces hard copies of documents.
Speaker - Outputs sound from the computer.
Projector - Displays images on a large screen.
Headphones - Provides audio output privately.
5. Explain scanf() and printf() functions in C language with syntax
and example.
Ans :
Input-Output Statements in C
Definition: C uses input-output functions like printf() and scanf() for data
interaction
printf() Function: Definition: Used to display output on the screen.
Syntax :
printf("format string", argument_list);
Example:
#include <stdio.h>
int main() {
printf("Hello, World!\n");
return 0;
}
scanf() Function: Definition: Used to take user input.
Syntax:
scanf("format string", &variable);
Example:
#include <stdio.h>
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
printf("You entered: %d", num);
return 0;
}
6. Define Computer. Explain the various types of computer.
Ans :
Definition of Computer and Types
Definition: A computer is an electronic device that processes data and
performs calculations to provide meaningful output.
Types of Computers:
Supercomputers - Used for complex scientific calculations, weather
forecasting, and nuclear research.
Mainframes - Used in banks and enterprises for bulk data processing,
handling thousands of transactions per second.
Microcomputers (PCs) - Used in homes, schools, and offices for general
computing tasks.
Minicomputers - Smaller than mainframes but powerful, used in business
applications.
Embedded Systems - Specialized systems found in devices like ATMs,
cars, and medical equipment.
7. Illustrate the flowchart and write a C program which takes as
input p, t, v Compute the simple interest and display result.
Ans :
Flowchart :
Program :
#include <stdio.h>
int main() {
float P, T, R, SI;
printf("Enter Principal amount: ");
scanf("%f", &P);
printf("Enter Time (in years): ");
scanf("%f", &T);
printf("Enter Rate of Interest: ");
scanf("%f", &R);
SI = (P * T * R) / 100;
printf("Simple Interest: %.2f\n", SI);
return 0;
}
8. Define a Computer. Explain the Characteristics of a digital
Computer.
Ans :
Definition: A computer is an electronic device that processes data and
performs calculations to provide meaningful output.
Characteristics of Digital Computers
1. Speed - Performs millions of calculations per second.
2. Accuracy - Delivers precise results with minimal errors.
3. Automation - Executes tasks without human intervention.
4. Versatility - Handles different types of tasks simultaneously.
5. Memory - Stores and retrieves large amounts of data quickly.
9. With a neat diagram explain the steps in the execution of C
program.
Ans :
Example
Let’s consider a simple C program:
#include <stdio.h>
int main() {
printf("Hello, World!\\n");
return 0;
}
How this program goes through the compilation process?
1. Preprocessing (cpp)
o Expands #include <stdio.h> and removes comments.
o Produces an expanded .i file.
2. Compilation (cc1)
o Converts the preprocessed code into assembly (.s file).
3. Assembly (as)
o Translates the assembly code into object code (.o file).
4. Linking (ld)
o Links the object file with standard libraries (like stdio.h).
o Produces the final executable file (a.out or .exe).
10. Designan algorithm and program to find area of a circle.
Explain the various rules for forming identifier names. Give an
example for valid and Invalid identifier.
Ans :
Algorithm:
1. Start
2. Take input principal (P), time (T), and rate of interest (R)
3. Calculate simple interest using formula SI = (P * T * R) / 100
4. Display simple interest
5. Stop
C Program:
#include <stdio.h>
int main() {
float P, T, R, SI;
printf("Enter Principal amount: ");
scanf("%f", &P);
printf("Enter Time (in years): ");
scanf("%f", &T);
printf("Enter Rate of Interest: ");
scanf("%f", &R);
SI = (P * T * R) / 100;
printf("Simple Interest: %.2f\n", SI);
return 0;
}
11. Compareb/w primary memory and secondary memory along
with examples.
Ans :
Feature Primary Memory (RAM, Secondary Memory (HDD,
Cache, Registers) SSD, USB, CD/DVD)
Speed Very fast (nanoseconds) Slower (milliseconds)
Volatility Volatile (data lost when Non-volatile (data is
power is off) retained)
Storage Type Temporary storage for Permanent storage for files
active programs and programs
Capacity Limited (typically GBs) Large (GBs to TBs)
Access Direct access by CPU Needs loading into RAM
first
Example RAM, Cache, Registers HDD, SSD, USB, CD/DVD
12. Explain the following programming paradigms.
i)Procedural programming ii)Structural programming
iii)Object oriented programming
Ans :
Procedural Programming
Follows step-by-step instructions (procedures/functions).
Uses functions to structure code.
Example in C
void add(int a, int b) {
printf("Sum: %d", a + b);
}
Structured Programming
Follows a logical structure using loops, conditionals, and functions.
Avoids goto statements for better readability.
Example in C
if (x > 0) {
printf("Positive");
} else {
printf("Negative");
}
Object-Oriented Programming (OOP)
Organizes code into objects (data + functions).
Uses concepts like encapsulation, inheritance, polymorphism.
Example in C++ (since C lacks OOP):
class Car {
public:
void drive() { cout << "Driving"; }
};
13. Explain the organization of Basic computer model with neat
diagram.
Ans :
Basic Computer Organization in C (Shown in Diagram)
1. Input Unit: Takes user input (e.g., keyboard, mouse).
scanf("%d", &num); // Input from the user
2. CPU (Central Processing Unit): Processes data using:
Control Unit (CU): Directs program execution.
ALU (Arithmetic Logic Unit): Performs calculations
int sum = a + b; // ALU performs addition
3. Memory: Stores data temporarily (RAM) or permanently (HDD/SSD).
int arr[10]; // Stored in primary memory (RAM)
4. Output Unit: Displays results (e.g., monitor, printer).
printf("Result: %d", sum); // Output to screen
14. What are the basic data types available in C.
Ans :
Definition: Data types in C define the type and size of data stored in variables.
Types of Data in C:
1. int - Integer type, used for whole numbers (e.g., int a = 5;).
2. float - Floating-point type, used for decimal numbers (e.g., float pi = 3.14;).
3. char - Character type, used for storing single characters (e.g., char grade =
'A';).
4. double - Double precision floating-point type (e.g., double price = 99.99;).
These data types help define variables and allocate memory efficiently.
SACHIN(1ST SEM)
DATA SCIENCE
ACET