Posts

Showing posts from May, 2022

Nested loop.

 nested loop loop within a loop. use of nested loop #include<stdio.h> int main() { int n; printf("enter the number of rows :"); scanf("%d", &n); for(int i=1; i<=n; i++){ for(int j=1; j<=n; j++){ printf("*"); } printf("\n"); } return 0; } output : enter the number of rows :9 ********* ********* ********* ********* ********* ********* ********* ********* ********* -------------------------------- Process exited after 4.613 seconds with return value 0 Press any key to continue . .

Loop Control Statements

A Loop is used to consecutively execute a line of code or a block of code again and again until it reaches the desired value. Types of Loop 1. for loop. syntax : for(initialization; condition; increment){ // do something } Initialization: initialize the value. Condition: check the loop to determine whether its value has reached the number of repetitions desired. increment: updating the value of the loop each time, the body of the loop executes. Example : let us write a program to print "well come" 10 time #include<stdio.h> #include<math.h> int main() { for(int i = 1; i<=10; i = i+1){ printf("well come \n"); } return 0; } Output :  well come will be printed 10 times Short hand : a. increment operator: ++i and i++ ++1 (pre increment operator)   means  increase and then use. use of ++i # include<stdio.h> #include<math.h> int main() { int i = 1; printf("%d\n", ++i); //  increase printf("%d\n", i); // a

write a program to calculate the sum of all numbers from 1 and 100.

 //program to calculate the sum of all numbers from 1 and 100. #include<stdio.h> int main() { int sum=0; for(int i=1; i<=100; i++){ sum = sum + i; } printf("sum of numbers from 1 to 100 =%d\n", sum); return 0; } output : sum of numbers from 1 to 100 =5050 -------------------------------- Process exited after 0.006596 seconds with return value 0 Press any key to continue . . .

write a program to print reverse of the table for a number n.

 //program to print reverse of the table for a number n. #include<stdio.h> int main() { int n; printf("enter any number="); scanf("%d", &n); for(int i=10; i>=1; i--){ printf("%d\n", n*i); } return 0; } output: enter any number=2 20 18 16 14 12 10 8 6 4 2 -------------------------------- Process exited after 7.439 seconds with return value 0 Press any key to continue . .

write a program to print the factorial of a number n.

//program to print the factorial of a number n. // 1!=1 // 2!=1*2=2 // 3!=1*2*3=6 // 4!=1*2*3*4=24 // 5!=1*2*3*4*5=120 #include<stdio.h> int main() { int n, fact = 1; printf("enter any number="); scanf("%d", &n); for(int i=1; i<=n; i++){ fact = fact *i; } printf("factorial = %d", fact); return 0; } output : enter any number=10 factorial = 3628800 -------------------------------- Process exited after 5.793 seconds with return value 0 Press any key to continue . . .

write a program to print all the odd numbers from 1 to 100.

//program to print all the odd numbers from 1 to 100. #include<stdio.h> int main() { for(int i=1; i<=100; i++){ if(i % 2 == 0){ // even number will be skiped continue; } printf("%d\n", i); } return 0; } output :  1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49 51 53 55 57 59 61 63 65 67 69 71 73 75 77 79 81 83 85 87 89 91 93 95 97 99 -------------------------------- Process exited after 0.0161 seconds with return value 0 Press any key to continue . . .

program to (keep taking number as input from user until user enters a number which is multiple of 5.)

 //keep taking number as input  from user until user enters a number which is multiple of 5. #include<stdio.h> int main() { int n; do{ printf("enter any number="); scanf("%d", &n); printf("%d\n", n); if(n % 5 == 0){ break; } } while(1); printf("you have entered multiple of 5 "); return 0; } output : enter any number=1 1 enter any number=2 2 enter any number=4 4 enter any number=5 5 you have entered multiple of 5 -------------------------------- Process exited after 5.066 seconds with return value 0 Press any key to continue . .

write a program (keep taking numbers as input from user until user enters an Odd number)

 //keep taking numbers as input from user until user enters an Odd number #include<stdio.h> int main() { int n; do{ printf("enter any number="); scanf("%d", &n); printf("%d\n", n); if(n % 2 !=0){ break; } } while(1); printf("you have entered an odd number "); return 0; } output : enter any number=0 0 enter any number=8 8 enter any number=6 6 enter any number=4 4 enter any number=2 2 enter any number=3 3 you have entered an odd number -------------------------------- Process exited after 11.81 seconds with return value 0 Press any key to continue . . .

Write a program to print the table of number input by the user.

//program to print the table of number input by the user. #include<stdio.h> int main() { int n; printf("enter any number\n"); scanf("%d", &n); for(int i=1; i<=10; i++){ printf("%d\n", n*i); } return 0; } Output : enter any number 2 2 4 6 8 10 12 14 16 18 20 -------------------------------- Process exited after 4.045 seconds with return value 0 Press any key to continue . .

write a program to print the sum of first n natural numbers and also, print them in reverse.

 //program to print the sum of first n natural numbers and also, print them in reverse. #include<stdio.h> int main() { int sum, n; printf("enter any number\n"); scanf("%d", &n); for(int i=1, j=n; i<=n && j>=1; i++, j--){ sum = sum+i; printf("%d\n", j); } printf("sum of n numbers = %d\n", sum); return 0; } Output : enter any number 11 11 10 9 8 7 6 5 4 3 2 1 sum of n numbers = 66 -------------------------------- Process exited after 4.68 seconds with return value 0 Press any key to continue . . . 2nd method #include<stdio.h> int main() { int sum, n; printf("enter any number\n"); scanf("%d", &n); for(int j=n; j>=1; j--){ sum = sum+j; printf("%d\n", j); } printf("sum of n numbers = %d\n", sum); return 0; } Output : enter any number 11 11 10 9 8 7 6 5 4 3 2 1 sum of n numbers = 66 ---------------

write a program to Print the sum of first n natural numbers.

// program to Print the sum of first n natural numbers. #include<stdio.h> int main() { int sum=0, n; printf("enter any number\n"); scanf("%d", &n); for(int i=1; i<=n; i++){ sum = sum+i; //it will work like 1+2+3+4+5+6=21 } printf("sum of first n natural numbers = %d\n", sum); return 0; } Output : enter any number 6 sum of first n natural numbers = 21 -------------------------------- Process exited after 3.741 seconds with return value 0 Press any key to continue . . .

write a program to print the numbers from 0 to n, if n is given by user.

 //program to print the numbers from 0 to n, if n is given by user. #include<stdio.h> int main() { int i=0; int n; printf("enter any number :\n"); scanf("%d", &n); while(i <= n){ printf("%d\n", i); i++; } return 0; } Output : enter  any number : 15 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 -------------------------------- Process exited after 5.739 seconds with return value 0 Press any key to continue . . .

Write a program to print values from 0 to 100 and vice versa.

 //program to print values from 0 to 100. #include<stdio.h> int main() { for(int i = 0; i<=100; i = i+1){ printf("%d\n", i); } return 0; } Output : 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 -------------------------------- Process exited after 0.07529 seconds with return value 0 Press any key to continue . . .  // program to print values from 100 to 0. #include<stdio.h> int main() { for(int i = 100; i>=0; i = i-1){ printf("%d\n", i); } return 0; } Output :  100 99 98 97 96 95 94 93 92 91 90 89 88 87 86 85 84 83 82 81 80 79 78 77 76 75 74 73 72 71 70 69 68 67 66 65 64 63 62 61 60 59 58 57 56 55 54 53 52 51 50 49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32 31 30 29 28

Write a program to find if a character entered by user is upper case or lower case.

 // program to find if a character entered by user is upper case or lower case. #include<Stdio.h> int main() { char ch; printf("enter character\n"); scanf("%c", &ch); if(ch >= 'A' && ch <= 'Z'){  // In, ASCII value every character have its integer value // ASCII value of A = 65 and Z = 90 printf("upper case");     }          else if(ch >= 'a' && ch <= 'z')     {     printf("lower case"); } else{ printf("invalid character");     } return 0; } Output : enter character G upper case -------------------------------- Process exited after 7.554 seconds with return value 0 Press any key to continue . . enter character g lower case -------------------------------- Process exited after 7.554 seconds with return value 0 Press any key to continue . . enter character # invalid character -------------------------------- Process exited after 7.554 se

What will be the output of this code

 //1. What will be the output of this code  #include<Stdio.h> int main() { int x = 2; if(x = 1){ printf("x is equal to 1");     }          else     {     printf("x is not equal to 1"); } return 0; } a. error b. x is equal to 1 c. x is not equal to 1 Ans. b. x is equal to 1, because when we write x = 1, that means we assign x =1 not asking (x==1) whether x is equal to 1 or not. so in if condition value of x becomes 1. // 2. What will be the output of this code  #include<Stdio.h> int main() { int x = 2; if(x = 0){ printf("x is equal to 1");     }          else     {     printf("x is not equal to 1"); } return 0; } a. error b. x is equal to 1 c. x is not equal to 1 Ans : x is not equal to 1 because x = 2.

Write a program to print the grades to a student.

 //program to print the grades to a student. //marks < 35 is C //35 <= marks < 70 is B //70 <= marks < 90 is A //90 <= marks <=100 is A+ #include<stdio.h> #include<math.h> int main() { int marks; printf("enter marks(0-100)\n"); scanf("%d", &marks); if(marks <35 ){ printf("grade C\n"); } else if(marks >= 35 && marks < 70){ printf("grade B\n"); } else if(marks >=70 && marks < 90){ printf("grade A\n"); } else if(marks >=90 && marks <= 100){ printf("grade A+\n"); } else{ printf("invalid marks"); } return 0; } Output : enter marks(0-100) 27 grade C -------------------------------- Process exited after 9.762 seconds with return value 0 Press any key to continue . . . enter marks(0-100) 68 grade B -------------------------------- Process exited after 5.493 seconds with return value 0

Write a program to check if a student passed or failed.

 //marks >=35 is pass other wise fail. #include<stdio.h> #include<math.h> int main() { int marks; printf("enter marks(0-100)\n"); scanf("%d", &marks); if(marks>=35 && marks<=100){ printf("pass"); } else if(marks<=0 || marks<35){ printf("fail"); } else{ printf("invalid marks"); } return 0; } Output : enter marks 45 student is pass -------------------------------- Process exited after 8.051 seconds with return value 0 Press any key to continue . . .

Conditional Statements.

Types of Conditional Statements. 1. if-else if statement performs the operation when the condition is 1( true ). else statement performs the operation when the condition is 0( false ). syntax :   if (condition) { //performs the operation when the condition is 1(true) } else { //else statement performs the operation when the condition is 0( false ) } NOTE: else is an optional block. Example a. else-if else-if statement performs the operation when the if condition is 0( false ). Syntax : if (condition) { //performs the operation when the condition is 1(true) } else if { //else if statement performs the operation when the if condition is 0( false ) } Example b. Nested if use of if inside if. Example : #include<stdio.h> #include<math.h> int main() { int number; printf("enter number\n"); scanf("%d", &number);     if(number>=0){         printf("number is +ive\n"); } if(number%2==0){     printf("number is even\n&quo