PPS Ans
PPS Ans
The process of programming involves writing code, compiling it into machine language, and then
executing the code to perform a task:
Define the problem, identify objectives, and understand the needs of the end users.
2. Write code
Use a programming language to write a set of instructions, or program, that tells the computer what
to do.
A compiler converts the code into machine language so the computer can understand it.
The computer scans and executes the code to perform the task.
Q.3. Write a C program to declare and initialize variables of different data types
and display their sizes.
#incIude<stdio.h>
#incIude<conio.h>
void main()
int a=1O;
float b=JO.23;
char c='A';
double d=99.888888;
clrscr();
printf("\nEnter Values:");
printf("\nInteger Value:%d ",a);
printf("\nFIoat Value:%f ",b);
printf("\nCharacter:%c ",c);
printf("\nDoubIe Value:%d ",d);
printf("\nEnter Sizes:");
printf("Size Of Integer-%d Bytes\n",sizeof(int));
printf("Size Of Float-%d Bytes\n",sizeof(float));
printf("Size Of Double-%d Bytes\n",sizeof(double));
printf("Size Of Character-%d Bytes\n",sizeof(char));
getch();
}
Q.4. What is Control Statement in C Programming? Explain if-else in detail
with example.
A control statement in C programming is a construct that allows a computer
to execute certain code based on conditions. The if-else statement is a
fundamental control flow structure that uses conditional logic to make
decisions and direct the flow of a program.
The syntax for an if-else statement is:
• if (condition): The condition to be evaluated
• {: The statements to execute if the condition is true
• } else: The statements to execute if the condition is false
Here's an example of an if-else statement in action:
• if (surgePricingActive): Tests if surge pricing is active
• {: If surge pricing is active, this is what to do
• printf("Surge pricing is active. Fares might be higher now. "); Prints a
message to the user
• } else: If surge pricing is not active, this is what to do
• {: If surge pricing is not active, this is what to do
• printf("No surge pricing. Good time to book a ride! "); Prints a message
to the user
If-else statements are a crucial tool for programmers to create dynamic and
responsive programs. They allow programs to respond correctly to different
situations and inputs.
Q.5. Explain switch case in detail
n C programming, the switch statement provides an efficient way to execute
one out of multiple possible code blocks based on the value of a single
variable or expression. It is a control structure that is particularly useful when
you have many possible values to compare against a variable, as it is often
more readable and efficient than using multiple if-else statements.
Syntax of Switch-Case:
switch (expression) {
case constant1:
// Code block 1
break;
case constant2:
// Code block 2
break;
case constant3:
// Code block 3
break;
default:
// Code block if no case matches
}
Q.6. Write a short note on loops.
In C programming, loops are control structures that allow you to execute a
block of code multiple times, based on certain conditions. They help in
automating repetitive tasks and optimizing code. C provides three main types
of loops:
1. For Loop: Used when the number of iterations is known beforehand.
2. While Loop: Used when the number of iterations is not fixed and depends on a
condition that is checked before each iteration.
3. Do-While Loop: Similar to the while loop, but the condition is checked after the
code executes, ensuring that the loop runs at least once.
Key Points:
• The for loop is ideal for situations where the number of iterations is
known.
• The while and do-while loops are used when the loop condition needs
to be evaluated during execution.
• Loops in C can be controlled using break (to exit the loop) and continue
(to skip the current iteration).
Loops are a fundamental part of C programming, enabling the creation of
efficient and concise code for repetitive tasks.
User-defined function
You can also create functions as per your need. Such functions created by the
user are known as user-defined functions.
How user-defined function works?
#include <stdio.h>
void functionName()
{
... .. ...
... .. ...
}
int main()
{
... .. ...
... .. ...
functionName();
... .. ...
... .. ...
}
The execution of a C program begins from the main() function.
When the compiler encounters functionName();, control of the program
jumps to
void functionName()
And, the compiler starts executing the codes inside functionName().
The control of the program jumps back to the main() function once
code inside the function definition is executed.
Q.8. Explain while loop. Write a C program to reverse a number.
A while loop in C is a control flow statement that repeatedly executes a block
of code as long as a specified condition is true:
• Syntax
The syntax for a while loop is while (condition) { // code block to be executed
}
• Explanation
The while keyword marks the beginning of the loop, the condition is a
Boolean expression that is evaluated before each iteration, and the code
inside the curly braces is the body of the loop
• Purpose
While loops are commonly used when the number of iterations is unknown
to the user. They can save time, reduce errors, and make code more
readable.
• Example
For example, the following code will run the code in the loop over and over
again as long as a variable (i) is less than 5:
• Termination: The while statement can also terminate when
a break, goto, or return within the statement body is
executed. The continue statement can be used to terminate an
iteration without exiting the while loop.
• #include <stdio.h>
• #include <conio.h>
• void main()
• {
• int n, reverse = 0, remainder, original;
•
• printf("Enter an integer: ");
• scanf("%d", &n);
•
• original = n;
•
• while (n != 0) {
• remainder = n % 10;
• reverse = reverse * 10 + remainder;
• n /= 10;
• }
•
• if (original % 10 == 0) {
• printf("Reversed number = %d", reverse);
•
• while (original % 10 == 0) {
• printf("0");
• original /= 10;
• }
• } else {
• printf("Reversed number = %d", reverse);
• }
•
• return 0;
• }
int main() {
int num1, num2, sum, product;
// Perform addition
sum = add(num1, num2);
// Perform multiplication
product = multiply(num1, num2);
return 0;
}