# include < stdio.h > // assigns stdio-h header file to your program void main ( ) // Indicates the starting point of the program. { char C, // variable declaration printf (“Type one character:”) ; // message to user C = getchar () ; // get a character from key board and Stores it in variable C. Printf (” The character you typed is = %c”, C) ; // output } // Statement which displays value of C on // Standard screen. |
Program shows the use of getchar function in an interactive environment.
#include < stdio.h > // Inserts stdio.h header file into the Pgm void main ( ) // Beginning of main function. { char in; // character declaration of variable in. printf (” please enter one character”); // message to user in = getchar ( ) ; // assign the keyboard input value to in. putchar (in); // out put ‘in’ value to standard screen. } |
if (condition)
statement1;
statement2;
In the above syntax, “if” is the keyword and condition in parentheses must evaluate to true or false. If the condition is satisfied (true) then compiler will execute statement1 and then statement2. If the condition is not satisfied (false) then compiler will skip statement1 and directly execute statement2. Syntax of the “if----else” statement is as followsif (condition)
statement1;
else
statement2;
In the above syntax, “if” and “else” are the keywords and condition in parentheses must evaluate to true or false. If the condition is satisfied (true) then compiler will execute statement1 and skip statement2. If the condition is not satisfied (false) then compiler will skip statement1 and directly execute statement2. Nested if-----else statements :Nested “if-----else” statements are used when programmer wants to check multiple conditions. Nested “if---else” contains several “if---else” a statement out of which only one statement is executed. Number of “if----else” statements is equal to the number of conditions to be checked. Following is the syntax for nested “if---else” statements for three conditionsif (condition1)
statement1;
else if (condition2)
statement2;
else if (condition3)
statement3;
else
statement4;
In the above syntax, compiler first check condition1, if it trues then it will execute statement1 and skip all the remaining statements. If condition1 is false then compiler directly checks condition2, if it is true then compiler execute statement2 and skip all the remaining statements. If condition2 is also false then compiler directly checks condition3, if it is true then compiler execute statement3 otherwise it will execute statement 4.switch(expression)
{
case constant1:
statements 1;
break;
case constant2:
statements 2;
break;
…………………..
default:
statements n;
break;
}
Q3)Write a program to check whether the number is even or odd.A3)#include <stdio.h> int main() { int number; printf("Enter an integer: "); scanf("%d", &number);
// True if the remainder is 0 if (number%2 == 0) { printf("%d is an even integer.",number); } else { printf("%d is an odd integer.",number); }
return 0; } |
for loop Here is the general form of the traditional for statement: for(initialization; condition; iteration) { // body } |
#include <stdio.h> int main() { int n, count, sum = 0;
printf("Enter the value of n(positive integer): "); scanf("%d",&n);
for(count=1; count <= n; count++) { sum = sum + count; } | ||
printf("Sum of first %d natural numbers is: %d",n, sum);
return 0; } Output:
Enter the value of n(positive integer): 6 | ||
#include <math.h> #include <stdio.h> int main() { double a, b, c, discriminant, root1, root2, realPart, imagPart; printf("Enter coefficients a, b and c: "); | ||||
scanf("%lf %lf %lf", &a, &b, &c);
discriminant = b * b - 4 * a * c;
// condition for real and different roots if (discriminant > 0) { root1 = (-b + sqrt(discriminant)) / (2 * a); root2 = (-b - sqrt(discriminant)) / (2 * a); printf("root1 = %.2lf and root2 = %.2lf", root1, root2); }
// condition for real and equal roots else if (discriminant == 0) { root1 = root2 = -b / (2 * a); printf("root1 = root2 = %.2lf;", root1); }
// if roots are not real else { realPart = -b / (2 * a); imagPart = sqrt(-discriminant) / (2 * a); printf("root1 = %.2lf+%.2lfi and root2 = %.2f-%.2fi", realPart, imagPart, realPart, imagPart); } | ||||
return 0; }
Enter coefficients a, b and c: 2.3 4 5.6 | ||||
root1 = -0.87+1.30i and root2 = -0.87-1.30i | ||||
#include <stdio.h> // Function for binomial coefficient table int bin_table(int val) { for (int i = 0; i <= val; i++) { printf("%2d", i); int num = 1; for (int j = 0; j <= i; j++) { if (i != 0 && j != 0) num = num * (i - j + 1) / j; printf("%4d", num); } printf("\n"); } } int main() { int value = 5; bin_table(value); |
return 0; } |
#include <stdio.h> int main() { int rows, i, j, space; printf("Enter the number of rows: "); scanf("%d", &rows); for (i = rows; i >= 1; --i) { for (space = 0; space < rows - i; ++space) printf(" "); for (j = i; j <= 2 * i - 1; ++j) printf("* "); for (j = 0; j < i - 1; ++j) printf("* "); printf("\n"); } return 0; } |
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
Q9) Write a program to print even numbers less than 10.A9)#include<stdio.h> #include<conio.h> | ||
int main() { int num=1; //initializing the variable do //do-while loop { printf("%d\n",2*num); num++; //incrementing operation }while(num<=10); return 0; } Output:
2 4 6 8 10 12 14 16 18 | ||
20 | ||
#include<stdio.h> int main() { | ||
int n1=0,n2=1,n3,i,number; printf("Enter the number of elements:"); scanf("%d",&number); printf("\n%d %d",n1,n2);//printing 0 and 1 for(i=2;i<number;++i)//loop starts from 2 because 0 and 1 are already printed { n3=n1+n2; printf(" %d",n3); n1=n2; n2=n3; } return 0; } | ||