Skip to main content

Posts

switch case in c

switch case in c: What is switch case in c ? It is used for checking the equality One switch statement can have multiple case statement. After each case statement break statement is necessary  break statement is used for terminating the control from switch case in c. In switch statement default statement is used for displaying the invalid input. Basic Syntax: scanf("%d",x); switch(x) { case 1: code which will be executed when x=1 break; case 0: code which will be executed when x=0 break; case 10: code which will be executed when x=10 break; default : code if any input is invalid } Explanation: In the above syntax the code in case 1 will be executed if the value of x = 1, if x=0 then case 0 will be executed. So in general any thing written after case is compare with the input given by the user ad if it is equal then the code in that case is executed. Note:  (:) - should be present after every case statement.  

for loop in C language

Loops in C language: What are loops? Loops are used for performing a task again and again in other words loops are used for checking number of iterations performed. There are 3 main types of loops in c language. for loop while loop do while loop We are going to do for loop in this article with some couple of problems. For executing a loop there are mostly three main steps Initialization Condition Increment or Decrement operator. 1. Initialization: It provides the starting value to the variable to start the loop or the value from where the loop should be started. 2. Condition: It is used for checking the condition if the condition is true than the process is executed until the condition becomes false. 3.Increment or Decrement Operator: It is used for incrementing or decrementing the value variable each time after the loop is executed (i++) -  Increases the value of variable by 1. (i+=1) -  Increases the value of variable by 1. (i=i+1) - 

Nested if Statements In C Programming

NESTED if STATEMENTS: In simple words an if statement inside another if statement is called Nested if statement. Syntax: if(condition 1) { if(condition 2) {code if condition 1 and 2 both are satisfied;} } Example Code: Write a program to find whether the number entered by the user lies in a particular range. Ans: #include<stdio.h> #include<conio.h> void main() { int n; getch(); printf("\n enter the number "); scanf("%d",&n); if(a>0) { if(a<18) { printf("\n the entered number is in the given range"); } else {  printf("\n the entered number is not in the given range"); } } else { printf("\n the number is not in the given range); } getch(); } The above code is written in code blocks Output: enter the number 17 the entered number is in the given range Explanation of the above Code: In the above code we have declared an variable of data type integ

Conditional Statements In C Programming

Conditional Statements: What are conditional Statements: Ans. Conditional statements are those statements which are used to check whether a particular condition is true or t is false and if it is true the task given to it gets executed. Types of conditional statements: There 3 types of conditional statements if statement else if statement else statement They all work together to give the desired output. Syntax: if(condition here) {code to be written here;} Note: The condition which needs to be checked should be written inside the round brackets. Code which needs to be executed if the condition is true is written inside curly braces. Semicolon will come only inside curly braces and not after condition. 1. if statement: It is the main statement and it is a standalone statement i.e it doesn't require any other conditional statement for its existence. Syntax: if(condition) {Code to be written here;} If the condition in the

Basic C Programming Questions

Basic Coding Questions: Exercise 1: Q1. Write a programme to calculate area and perimeter of rectangle by accepting its length and breadth. Ans: #include<stdio.h> #include<conio.h> void main() { float a,p,l,b; clrscr(); printf("\n enter the length and breadth of rectangle"); scanf("%f%f",&l,&b); a=l*b; p=2*(l+b); printf("\n the area of rectangle is %f and the perimeter is %f",a,p); getch(); } The above image code is written in code blocks Output: enter the length and breadth of rectangle 20(press enter) 10 (press enter) the area of rectangle is 200 and the perimeter is 60 Explanation of the above code: Declaring of variables in data type float since length and breadth cannot always be integers. Taking input of length and breadth together by using scanf function. Substituting the values of length and breadth stored in l and b