PPS Unit 2 Laqs
PPS Unit 2 Laqs
PPS Unit 2 Laqs
Ans. Selection statements in C are used to make decisions in your program. They allow
you to execute different blocks of code based on the evaluation of a condition. In C,
there are primarily two types of selection statements: ‘ if’ statements and ‘ switch’
statements.
Syntax:
if (condition) {
Example:
#include <stdio.h>
int main() {
return 0;
if-else Statement: The if-else statement is used to execute one block of code if a
condition is true and another block of code if the condition is false.
Syntax:
if (condition) {
} else {
Example:
#include <stdio.h>
int main() {
int num = 3;
if (num > 5) {
} else {
return 0;
switch Statement: The switch statement is used to select one of several code blocks
to be executed.
Syntax:
switch (expression) {
case constant1:
break;
case constant2:
break;
// ...
default:
#include <stdio.h>
int main() {
switch (grade) {
case 'A':
printf("Excellent!\n");
break;
case 'B':
printf("Good!\n");
break;
case 'C':
printf("Satisfactory.\n");
break;
default:
return 0;
for Loop: The for loop is used when you know in advance how many times you
want to execute a block of code.
Syntax:
for (initialization; condition; update) {
#include <stdio.h>
int main() {
printf("%d\n", i);
return 0;
while Loop: The while loop is used when you want to execute a block of code as
long as a certain condition is true.
Syntax:
while (condition) {
#include <stdio.h>
int main() {
int count = 1;
printf("%d\n", count);
count++;
return 0;
do-while Loop: The do-while loop is similar to the while loop, but it guarantees
that the code block is executed at least once, even if the condition is initially false.
Syntax:
do {
Example:
#include <stdio.h>
int main() {
int count = 1;
do {
printf("%d\n", count);
count++;
return 0;
3. Define switch statement explain with syntax and flowchart. Write a program to test all
arithmetic operators using switch statement.
Ans. A switch statement in C is used to select one of several code blocks to be
executed, based on the value of an expression. It is an alternative to a series of if-
else if statements when you need to choose between multiple options.
switch (expression) {
case constant1:
// Code to execute for constant1
break;
case constant2:
// Code to execute for constant2
break;
// ...
default:
// Code to execute if none of the cases match
}
Flowchart for a switch statement:
The program to test all the arithmetic operations is:
#include <stdio.h>
int main() {
char operator;
double num1, num2, result;
switch (operator) {
case '+':
result = num1 + num2;
break;
case '-':
result = num1 - num2;
break;
case '*':
result = num1 * num2;
break;
case '/':
if (num2 != 0) {
result = num1 / num2;
} else {
printf("Division by zero is not allowed.\n");
return 1; // Exit the program with an error code
}
break;
default:
printf("Invalid operator.\n");
return 1; // Exit the program with an error code
}
return 0;
}
4. Write a program to display minimum and maximum numbers among 3 numbers using
selection statements.
Ans. The code for finding maximum and minimum of three numbers is
#include<stdio.h>
void main()
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
else
else
#include<stdio.h>
void main()
int a[5],i,j,temp;
for(i=0;i<5;i++)
scanf("%d",&a[i]);
printf("%d \t",a[i]);
for(i=0;i<4;i++)
for(j=i+1;j<5;j++)
if(a[i]>a[j])
temp=a[i];
a[i]=a[j];
a[j]=temp;
for(i=0;i<5;i++)
printf("%d \t",a[i]);
#include<stdio.h>
void main()
{
int a[20],i,x,n;
printf("How many elements? \n");
scanf("%d",&n);
printf("Enter array elements \n");
for(i=0;i<n;++i)
scanf("%d",&a[i]);
printf("\n Enter element to search \n");
scanf("%d",&x);
for(i=0;i<n;++i)
if(a[i]==x)
break;
if(i<n)
printf("Element found at index %d \n",i);
else
printf("Element not found");
return 0;
}
10. Explain all string handling functions along with syntax and examples?
Ans. In C, string handling is typically done with character arrays and a set of standard library
functions for manipulating and working with strings. Below are some of the commonly used
string handling functions in C, along with their syntax and examples.
#include <stdio.h>
#include <string.h>
int main()
length = strlen(str);
return 0;
#include <stdio.h>
#include <string.h>
int main()
{
char source[] = "Hello, World!";
char destination[20];
strcpy(destination, source);
#include <stdio.h>
#include <string.h>
int main()
strcat(destination, source);
#include <stdio.h>
#include <string.h>
int main()
if (result < 0) {
printf("str1 is less than str2\n");
} else {
#include <stdio.h>
#include <string.h>
int main()
strrev(str);
#include <stdio.h>
#include <string.h>
int main()
strlwr(str);
printf("Lowercase string: %s\n", str);
#include <stdio.h>
#include <string.h>
int main()
strupr(str);