PPS Q-Bank Unit : 02

15. List out conditional Statements (Decision Making Statements) and explain with proper example. (Any conditional statement could be asked)

IF Else and Nested IF Else

In C programming, the if statement is used to execute a block of code if a certain condition is true. The syntax of the if statement is as follows :

if (condition) {
// code to be executed if condition is true
}

The condition is a Boolean expression that evaluates to either true or false. If the condition is true, the code inside the block is executed. If the condition is false, the code inside the block is skipped.

#include<stdio.h>
int main() {
int x = 5;
if (x > 3) {
printf(“x is greater than 3n”);
}
return 0;
}

the if statement checks if x is greater than 3. Since x is 5, the condition is true and the code inside the block is executed, resulting in the output “x is greater than 3”.

A nested if statement is simply an if statement inside another if statement. The syntax is as follows :

if (condition1) {
  // code to be executed if condition1 is true
if (condition2) {
  // code to be executed if condition2 is true
}
}

Here is a Example :

#include<stdio.h>
int main() {
int x = 5;
int y = 10;
if (x > 3) {
printf(“x is greater than 3n”);
if (y > 5) {
printf(“y is greater than 5n”);
}
}
return 0;
}
Explain Else…IF

In C programming language, the Else…If ladder is used to test multiple conditions and execute different code blocks based on those conditions. The syntax of the Else…If ladder is as follows :

if (condition1) {
// code to execute if condition1 is true
}
else if (condition2) {
// code to execute if condition2 is true
}
else if (condition3) {
// code to execute if condition3 is true
}

else {
// code to execute if none of the conditions are true
}

FlowChart :

Switch case and goto statement are also part of decision making explained blow.

16. Write a short note on Switch Case.

In C programming, a switch statement is used to evaluate an expression against a set of predefined cases. The syntax of the switch statement is as follows:

switch (expression) {
   case constant1:
      // code to be executed if expression is equal to constant1
      break;
   case constant2:
      // code to be executed if expression is equal to constant2
      break;
   .
   .
   .
   default:
      // code to be executed if expression doesn't match any of the constants
}

Here, the expression is evaluated against a set of constants defined by the cases. If the expression matches a particular constant, the code inside that case is executed. If no match is found, the code inside the default case is executed.

The switch statement is often used as an alternative to multiple if-else statements. It can make code more concise and easier to read in situations where there are multiple possible values for a variable.

A flowchart of the switch statement might look something like this :

17. What is Loop? Explain different kinds of loops available in C language.(Any Loop could be asked)

Ans: A loop is a programming construct that allows a section of code to be executed repeatedly based on a certain condition. In C language, there are four types of loops: for, while, do-while, and goto. 

For Loop: 

The for loop is used to execute a block of code repeatedly for a fixed number of times.  

The syntax of a for loop is as follows: 

The initialization step is executed only once at the beginning of the loop. The condition is checked before each iteration of the loop, and if it is true, the code inside the loop is executed. After each iteration, the increment/decrement step is executed. The loop stops when the condition becomes false. 

Example: 

While Loop: 

The while loop is used to execute a block of code repeatedly as long as a certain condition is true.  

The syntax of a while loop is as follows: 

The condition is checked at the beginning of each iteration of the loop, and if it is true, the code inside the loop is executed. The loop continues until the condition becomes false. 

Example: 

Do-While Loop: 

The do-while loop is similar to the while loop, but it guarantees that the code inside the loop is executed at least once.  

The syntax of a do-while loop is as follows: 

The code inside the loop is executed first, and then the condition is checked. If the condition is true, the loop continues, and if it is false, the loop stops. 

Example: 

18. Demonstrate GOTO statement with example.

The GOTO statement in C allows the program to transfer control to another part of the program, either forward or backward in the code.

A forward jump means that the program jumps to a statement that appears later in the code. For example:

int x = 1;
if (x == 1) {
   goto jump_here;
}
printf("This line is skippedn");
jump_here:
printf("Jumped here!n");

In this example, if x is equal to 1, the program will jump to the jump_here label and print “Jumped here!”. The line that prints “This line is skipped” is not executed.

A backward jump means that the program jumps to a statement that appears earlier in the code. For example:

int x = 10;
start:
if (x > 0) {
   printf("%dn", x);
   x--;
   goto start;
}

In this example, the program prints the value of x and then decrements it, until x is no longer greater than 0. The program then jumps back to the start label and repeats the process. This creates an infinite loop that prints the numbers from 10 down to 1.

It’s important to note that the use of GOTO statements can make code more difficult to read and understand, and can make it harder to debug. In general, it’s recommended to use structured programming techniques like loops and functions instead of GOTO statements.

19. Write Difference between: 1) break and continue 2) while and do..while

Differentiate between entry and exit controlled loop.

In computer programming, loops are used to execute a block of code repeatedly until a certain condition is met. There are two types of loops: entry-controlled and exit-controlled loops.

  1. Entry-controlled loop: An entry-controlled loop checks the loop condition at the beginning of each iteration of the loop. If the condition is true, the loop executes the statements within it, and if the condition is false, the loop terminates. The for loop and while loop in C are examples of entry-controlled loops.

Example:

for (int i = 1; i <= 10; i++) {
  printf("%d ", i);
}

In this example, the for loop runs as long as the value of the variable i is less than or equal to 10. This is an entry-controlled loop because the loop condition is checked at the beginning of each iteration of the loop.

  1. Exit-controlled loop: An exit-controlled loop checks the loop condition at the end of each iteration of the loop. If the condition is true, the loop executes the statements within it and continues to the next iteration, and if the condition is false, the loop terminates. The do-while loop in C is an example of an exit-controlled loop.

Example:

int i = 1;
do {
  printf("%d ", i);
  i++;
} while (i <= 10);

In this example, the do-while loop runs as long as the value of the variable i is less than or equal to 10. This is an exit-controlled loop because the loop condition is checked at the end of each iteration of the loop.

Explain Break and Continue statement with example.

In C programming language, the break and continue statements are used to alter the normal flow of execution in loops.

The break statement is used to exit the loop immediately when a certain condition is met. It can be used with any loop statement (for loop, while loop, do-while loop) or switch statement. Here is an example that uses a for loop:

include <stdio.h>
int main() {
  int i;
  for (i = 0; i < 10; i++) {
    if (i == 5) {
      break; // exit loop when i equals 5
    }
    printf("%dn", i);
  }
  return 0;
}

In the above code, the loop will terminate when the value of i becomes 5 because of the break statement. So, the output will be :

0
1
2
3
4

The continue statement is used to skip a particular iteration of the loop and move to the next iteration. It can also be used with any loop statement (for loop, while loop, do-while loop). Here is an example that uses a while loop:

#include <stdio.h>
int main() {
  int i = 0;
  while (i < 10) {
    i++;
    if (i == 5) {
      continue; // skip the iteration when i equals 5
    }
    printf("%dn", i);
  }
  return 0;
}

In the above code, the loop will skip the iteration when the value of i is 5 because of the continue statement. So, the output will be:

1
2
3
4
6
7
8
9
10

Note that i is incremented before the if statement, so the output starts at 1 and ends at 10. The iteration when i is 5 is skipped because of the continue statement.

20. What is an array? Briefly explain array.

Ans: An array is a collection of elements of the same data type that are stored in a contiguous block of memory. Each element in the array can be accessed using an index that starts from 0. 

For example, to declare an array of integers with 5 elements, the syntax would be:  int arr[5]; 

To access an element in the array, we use the index of that element inside square brackets. The index starts from 0, which means the first element in the array has an index of 0, the second element has an index of 1, and so on. here is example read 10 integers in an array. Find the addition of all elements.

#include <stdio.h> 
#include <conio.h> 
void main() 
{     i
nt arr[10], i, sum = 0;     
clrscr();     
printf("Enter 10 integers:n");     
for (i = 0; i < 10; i++) {        
 scanf("%d", &arr[i]);     
}     
for (i = 0; i < 10; i++) {        
 sum += arr[i];     
}    
printf("Sum of all elements in the array is %d", sum);     
getch(); 
}

21. What is 2D array? How we can create 2D array. Demonstrate 2D array with example.

Ans: A 2D array is a collection of elements of the same data type that are organized in a matrix format with rows and columns. It can be thought of as a table with rows and columns, where each cell holds a value.The syntax for declaring a 2D array is as follows: 

datatype arrayname[rows][columns]; 

For example, to declare a 2D array of integers with 3 rows and 4 columns, the syntax would be: 

int arr[3][4]; 

To access an element in the 2D array, we use two indices inside square brackets, one for the row and one for the column. 

22. Write a program to print the number of days in a given month using a switch statement. The program requires a month number (between 1to 12) as an input and then displays the number of days in that month.

#include<stdio.h>
#include<conio.h>
void main()
{
	int op;
	clrscr();
	printf("Enter Month 1 to 12:");
	scanf("%d",&op);
	switch(op)
	{
            case 1: 
              printf("31 days");
              break;
           case 2: 
              printf("28/29 days");
              break;
            case 3: 
              printf("31 days");
              break;
            case 4: 
              printf("30 days");
              break;
            case 5: 
              printf("31 days");
              break;
            case 6: 
              printf("30 days");
              break;
            case 7: 
              printf("31 days");
              break;
            case 8: 
              printf("31 days");
              break;
            case 9: 
              printf("30 days");
              break;
            case 10: 
              printf("31 days");
              break;
            case 11: 
              printf("30 days");
              break;
            case 12: 
              printf("31 days");
              break;
           default: 
              printf("Invalid input! Please enter month number between 1-12");
		
	}
	getch();
}

23. Write a program to check whether a number is a Krishnamurti number or not. Krishnamurthy number is one whose sum of factorial of digits equals the number. Example: 145 → 1! + 4! + 5!

#include<stdio.h>
#include<conio.h>
void main()
{
	int i,x,n,sum=0,fac,rem;
	clrscr();
	printf("Enter a Number:");
	scanf("%d",&n);
	x=n;
	do{
	rem=n%10;
	fac=1;
	for(i=1;i<=rem;i++)
	{
		fac=fac*i;
	}
		sum=sum+fac;
		n=n/10;
	}
	while(n>0);{
	if(sum==x)
	printf("%d is Krishnamurthy number",x);
	else
	printf("%d is Not Krishnamurthy number",x);
	}
	getch();

}

24. Write a program to check whether the number is Armstrong or not. Example:153—-1^3 + 5^3 +3^ 3 = 1 + 125 + 27 = 153

#include<stdio.h>
#include<conio.h>
void main()
{
	int i,x,n,sum=0,f=1,rem;
	clrscr();
	printf("Enter a Number:");
	scanf("%d",&n);
	x=n;
	do{
	rem=n%10;
	for(i=1;i<=n;i++)
	{
		f=rem*rem*rem;
	}
		sum=sum+f;
		n=n/10;
	}
	while(n>0);{
	if(x==sum)
	printf("%d is Armongstrong number",x);
	else
	printf("%d is Not Armongstrong number",x);
	}
	getch();

}

25. Write a program to list all prime numbers within a given range.

#include<stdio.h>
#include<conio.h>
void main()
{
	int end,start,i,n;
	clrscr();
	printf("Enter Range Number : ");
	scanf("%d%d",&start,&end);
	printf("Prime Number between %d & %d are:",start,end);
	for(n=start;n<=end;n++)
	{
		int prime=1;
		for(i=2;i*i<=n;i++)
		{
			if(n%i==0){
				prime=0;
				break;
			}
		}
		if(prime && n>1){
			printf("n%d",n);
		}
	}
	getch();
}

26. Write a program to print Fibonacci series of given numbers.

#include<stdio.h>    
#include<conio.h>
void main()    
{    
 int n1=0,n2=1,n3,i,num;    
 clrscr();
 printf("Enter the number of elements:");    
 scanf("%d",&num);    
 printf("n%d %d",n1,n2);//printing 0 and 1    
 for(i=2;i<num;++i)//loop starts from 2 because 0 and 1 are already printed    
 {    
  n3=n1+n2;    
  printf(" %d",n3);    
  n1=n2;    
  n2=n3;    
 }  
  getch();  
 } 

27. Pattern Printing Program. Any Pattern could be asked.

Click here… Program : 09 – Unit : 03

28. Write a program to find the factorial of a number. (without recursion)

#include<stdio.h>
#include<conio.h>
void main()
{
	int i,n,fac=1;
	clrscr();
	printf("Enter n:");
	scanf("%d",&n);
	for(i=n;i>=1;i--)
	{
		fac=fac*i;
	}
	printf("%d",fac);
	getch();

}

29. Write a program to read 10 integers in an array. Find the addition of all elements.

#include <stdio.h>
#include<conio.h>

void main()
{
    int arr[10],i,total=0;
    clrscr();
    for (i=0;i<10;i++){
	printf("Enter an integer: ");
	scanf("%d", &arr[i]);
	total=total+arr[i];
    }
    printf("The sum of the array elements is: %dn", total);
    getch();
}

30. Write a Program to print Addition of two matrices.

#include <stdio.h>
#include<conio.h>
void main()
{
    int matrix1[2][2] = {{1, 2}, {3, 4}};
    int matrix2[2][2] = {{5, 6}, {7, 8}},sum_matrix[2][2];
    int i,j;
    clrscr();
    for (i=0;i<2;i++){
	for (j=0;j<2;j++){
	    sum_matrix[i][j] = matrix1[i][j] + matrix2[i][j];
	}
    }
    printf("Matrix 1:n");
    for (i=0;i<2;i++){
	for (j=0;j<2;j++){
	    printf("%d ", matrix1[i][j]);
	}
	printf("n");
    }
    printf("nMatrix 2:n");
    for (i=0;i<2;i++){
	for (j=0;j<2;j++) {
	    printf("%d ", matrix2[i][j]);
	}
	printf("n");
    }
    printf("nSum matrix:n");
    for (i=0;i<2;i++){
	for (j=0;j<2;j++){
	    printf("%d ", sum_matrix[i][j]);
	}
	printf("n");
    }
    getch();
}

Leave a Comment

Your email address will not be published. Required fields are marked *

error: Content is protected !!
Scroll to Top