You are on page 1of 8

Ques- wap for selection shorting program?

#include <stdio.h>

int main() {

int i , arr[5] , temp , j , n = 5;

printf("Enter array numbers: ");

for (i = 0; i < n; i++) {

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

for (i = 0; i < n; i++) {

for (j = i + 1; j < n; j++) {

if (arr[i] > arr[j]) {

temp = arr[i];

arr[i] = arr[j];

arr[j] = temp;

printf("Sorted array: ");

for (i = 0; i < n; i++) {

printf("%d ", arr[i]);

return 0;

}
Ques- What is a pointer? Explain how the pointer variable declared and initialized.

A pointer is a variable that stores the memory address of another variable. It allows indirect
access to the value stored in memory.

To declare and initialize a pointer in C:


datatype *pointer_name;

datatype: Specifies the type of data that the pointer will point to.

• *: Indicates that the variable being declared is a pointer.


• pointer_name: The name of the pointer variable.

Ques-- Write a c-program using functions to generate the reverse of a number.

#include <stdio.h>

// Function to generate the reverse of a number

int reverseNumber(int num) {

int reversed = 0;

while (num != 0) {

reversed = reversed * 10 + num % 10;

num /= 10;

return reversed;

int main() {

int number, reversedNumber;

printf("Enter a number: ");

scanf("%d", &number);

reversedNumber = reverseNumber(number);

printf("The reverse of %d is %d\n", number, reversedNumber);

return 0;

}
Ques- Explain the different types of loops in C with syntax?

In C, there are three main types of loops: for, while, and do-while. Each type of loop has its
own syntax and use cases.

1-For Loop: The for loop is typically used when you know the number of iterations
beforehand.

Syntax- for (initialization; condition; increment/decrement) {

// Code to be executed repeatedly

2- While Loop: The while loop is used when you want to execute a block of code repeatedly
as long as a condition is true.

Syntax- while (condition) {

// Code to be executed repeatedly

3- Do-While Loop: The do-while loop is similar to the while loop, but the condition is
evaluated after executing the loop body. This means that the loop body is executed at least
once.

Syntax- do {

// Code to be executed repeatedly

} while (condition);
Ques- Explain if, if-else, nested if-else and cascaded if-else with examples and syntax.

Write down the codes for the following program:

An electricity board charges the following rates for the use of electricity: for the first 200
units 80 paise per unit: for the next 100 units 90 paise per unit: beyond 300 units Rs 1 per
unit. All users are charged a minimum of Rs. 100 as meter charge. If the total amount is more
than Rs 400, then an additional surcharge of 15% of total amount is charged. Write a
program to read the name of the user, number of units consumed and print out the charges.

1- if statement: The if statement is used to execute a block of code if a specified


condition is true.

Syntax- if (condition) {

// Code to be executed if the condition is true

2-if-else statement: The if-else statement is used to execute one block of code if the
condition is true and another block of code if the condition is false.

Syntax- if (condition) {

// Code to be executed if the condition is true

} else {

// Code to be executed if the condition is false

3-Nested if-else statement: A nested if-else statement is an if-else statement inside


another if or else block.

Syntax- if (condition1) {

if (condition2) {

// Code to be executed if both condition1 and condition2 are true

} else {

// Code to be executed if condition1 is true and condition2 is false

} else {

// Code to be executed if condition1 is false

4- Cascaded if-else statement:


if (condition1) {

// Code to be executed if condition1 is true

} else if (condition2) {

// Code to be executed if condition1 is false and condition2 is true

} else {

// Code to be executed if both condition1 and condition2 are false

Now, let's write the program to calculate electricity charges:

#include <stdio.h>

int main() {

char name[50];

int units;

float charges, total_amount, surcharge = 0;

// Input user's name and number of units consumed

printf("Enter your name: ");

scanf("%s", name);

printf("Enter number of units consumed: ");

scanf("%d", &units);

// Calculate charges based on the number of units consumed

if (units <= 200) {

charges = units * 0.8;

} else if (units <= 300) {

charges = 200 * 0.8 + (units - 200) * 0.9;

} else {

charges = 200 * 0.8 + 100 * 0.9 + (units - 300) * 1.0;


}

// Add minimum meter charge of Rs. 100

charges += 100;

// Check if total amount exceeds Rs. 400 and calculate surcharge

total_amount = charges;

if (total_amount > 400) {

surcharge = total_amount * 0.15;

total_amount += surcharge;

// Print the charges

printf("\nName: %s\n", name);

printf("Units Consumed: %d\n", units);

printf("Charges (excluding surcharge): Rs. %.2f\n", charges);

printf("Surcharge: Rs. %.2f\n", surcharge);

printf("Total Amount: Rs. %.2f\n", total_amount);

return 0;

You might also like