You are on page 1of 20

1. Explain Selection statements in C with syntax, flowchart and example.

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.

if Statement: The if statement is used to execute a block of code if a given condition


is true.

Syntax:

if (condition) {

// Code to execute when the condition is true


}
Flowchart:

Example:

#include <stdio.h>

int main() {

int num = 10;


if (num > 5) {

printf("The number is greater than 5.\n");

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) {

// Code to execute when the condition is true

} else {

// Code to execute when the condition is false

Flowchart for if-else:

Example:

#include <stdio.h>
int main() {

int num = 3;

if (num > 5) {

printf("The number is greater than 5.\n");

} else {

printf("The number is not greater than 5.\n");

return 0;

switch Statement: The switch statement is used to select one of several code blocks
to be executed.
Syntax:
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 switch:


Example:

#include <stdio.h>

int main() {

char grade = 'B';

switch (grade) {

case 'A':

printf("Excellent!\n");

break;

case 'B':
printf("Good!\n");

break;

case 'C':

printf("Satisfactory.\n");

break;

default:

printf("Not a valid grade.\n");

return 0;

2. Explain Iterative statements in C with syntax, flowchart and example.


Ans. Iterative statements (also known as loops) in C are used to repeatedly execute a
block of code as long as a certain condition is met. There are three primary types of
iterative statements in C: ‘ for’, ‘while’, and ‘do-while’ loops.

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) {

// Code to execute in each iteration

Flowchart for for loop:


Example:

#include <stdio.h>

int main() {

for (int i = 1; i <= 5; i++) {

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) {

// Code to execute in each iteration

Flowchart for while loop:


Example:

#include <stdio.h>

int main() {

int count = 1;

while (count <= 5) {

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 {

// Code to execute in each iteration


} while (condition);

Flowchart for do-while loop:

Example:

#include <stdio.h>

int main() {

int count = 1;

do {

printf("%d\n", count);

count++;

} while (count <= 5);

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.

Syntax of the switch statement:

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;

printf("Enter an operator (+, -, *, /): ");


scanf(" %c", &operator);

printf("Enter two numbers: ");


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

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
}

printf("Result: %lf\n", result);

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;

printf("Enter 3 numbers \n");

scanf("%d%d%d",&a,&b,&c);

if(a>b && a>c)

printf("Mximum number is a = %d \n",a);

else if(b>a && b>c)

printf("Mximum number is b = %d \n",b);

else

printf("Mximum number is c = %d \n",c);

if(a<b && a<c)

printf("Minimum number is a = %d \n",a);

else if(b<a && b<c)

printf("Minimum number is b = %d \n",b);

else

printf("Minimum number is c = %d \n",c);

5. Write the procedure of linear search& bubble sort


Ans.

6. Write a program to compute the addition, multiplication of two matrices.


Ans. The program to compute the multiplication of two matrices is
The program to compute the addition of two matrices is
7. Write a program check the given string is palindrome or not.
Ans.

8. Write a program to find reverse of given string without strrev().


Ans.
9. Write a program to implement linear search& bubble sort using Arrays.
Ans. The program to implement bubble sort is

#include<stdio.h>

void main()

int a[5],i,j,temp;

printf("Enter array values \n");

for(i=0;i<5;i++)

scanf("%d",&a[i]);

printf("Before sorting array values \n");


for(i=0;i<5;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;

printf("array values after sorting \n");

for(i=0;i<5;i++)

printf("%d \t",a[i]);

The program to implement linear search is

#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.

strlen (String Length):


 Syntax: size_t strlen(const char *str);
 Description: Returns the length of the input string, which is the number of
characters in the string excluding the null-terminating character '\0'.
Example:

#include <stdio.h>

#include <string.h>

int main()

char str[] = "Hello, World!";

length = strlen(str);

printf("Length of the string is: %d \n", length);

return 0;

strcpy (String Copy):


 Syntax: char *strcpy(char *dest, const char *src);
 Description: Copies the contents of the source string into the destination
string, including the null-terminating character.
Example:

#include <stdio.h>

#include <string.h>

int main()

{
char source[] = "Hello, World!";

char destination[20];

strcpy(destination, source);

printf("Copied string: %s \n", destination)

strcat (String Concatenate):


 Syntax: char *strcat(char *dest, const char *src);
 Description: Concatenates the source string to the end of the destination
string.
Example:

#include <stdio.h>

#include <string.h>

int main()

char destination[20] = "Hello, ";

char source[] = "World!";

strcat(destination, source);

printf("Concatenated string: %s \n", destination);

strcmp (String Compare):


 Syntax: int strcmp(const char *str1, const char *str2);
 Description: Compares two strings. It returns a negative value if str1 is
lexicographically less than str2, a positive value if str1 is greater, and 0 if
they are equal.
Example:

#include <stdio.h>

#include <string.h>

int main()

char str1[] = "apple";

char str2[] = "banana";

int result = strcmp(str1, str2);

if (result < 0) {
printf("str1 is less than str2\n");

} else if (result > 0) {

printf("str1 is greater than str2\n");

} else {

printf("str1 is equal to str2\n");

strrev (String Reverse):


 Syntax: char *strrev(char *str);
 Description: The strrev function is used to reverse the characters in a string.
It modifies the original string in place.
Example:

#include <stdio.h>

#include <string.h>

int main()

char str[] = "Hello, World!";

printf("Original string: %s\n", str);

strrev(str);

printf("Reversed string: %s\n", str);

strlwr (String to Lowercase):


 Syntax: char *strlwr(char *str);
 Description: The strlwr function converts all characters in a string to
lowercase. It modifies the original string in place.
Example:

#include <stdio.h>

#include <string.h>

int main()

char str[] = "Hello, World!";

printf("Original string: %s \n", str);

strlwr(str);
printf("Lowercase string: %s\n", str);

strupr (String to Uppercase):


 Syntax: char *strupr(char *str);
 Description: The strupr function converts all characters in a string to
uppercase. It modifies the original string in place.
Example:

#include <stdio.h>

#include <string.h>

int main()

char str[] = "Hello, World!";

printf("Original string: %s\n", str);

strupr(str);

printf("Uppercase string: %s\n", str);

You might also like