Professional Documents
Culture Documents
simple if syntax: if(condition) { Statement; } EX: #include<stdio.h> #include<conio.h> void main() { int age; clrscr(); printf("enter ur age:"); scanf("%d",&age); if(age>=18) { simple if if else else if switch
printf("Eligible to vote"); } getch(); } If .. else statement Syntax: If(condition) { Statement; } else { Statement; } Ex: Write a pgm to print a given age is eligible to vote or not #include<stdio.h> #include<conio.h> void main() { int age; clrscr(); printf("enter ur age:"); scanf("%d",&age); if(age>=18)
1. write a pgm to greatest of 2 numbers 2. write a pgm to given number is positive or negtive Else if Syntax: If(condition) { Statement; } else if(condition) { Statement; } . . . .
//write a pgm to print given character is vowel or not void main() { char c; clrscr(); printf("enter ur character :"); scanf("%c",&c); if((c=='a')||(c=='A')) { printf("Given character is vowel"); } else if((c=='e')||(c=='E')) { printf("Given character is vowel"); } else if((c=='i')||(c=='I')) { printf("Given character is vowel"); }
else if((c=='o')||(c=='O')) +-+{ printf("Given character is vowel"); } else if((c=='u')||(c=='U')) { printf("Given character is vowel"); } else { printf("Not Vowel:"); } getch(); } Work: Write a pgm to print given number is odd or even Write a pgm to print given number is positive , negative or zero Write a pgm to print greatest of 3 number Write a pgm to given character is vowel or not(if ..else) Write a pgm to get reg. no, five subj. marks. do the foll. calculate total, avg. display pass if all sub.marks > 40 or fail if avg >= 90 Excellent, >=80 Very Good, >=60 Good, >=50 Fair >=40 satisfactory and finally <40 poor (display the appropriate grade)
ii)
Multiple branching construct Switch In this case of selection is made from several altetnatives available a group of instructions pertaining to the selected alternative is only executed.
switch(expression) { case <expr1>: statement ........ break; case <expr2>: statement ......... break; case <exprN> : statement ........ break; default: statement break; } #include<stdio.h> #include<conio.h>
void main() { int i; clrscr(); printf("Enter the no betn 1 to 5:"); scanf("%d",&i); switch(i) { case 1: printf("Ur no is ONE"); break; case 2: printf("Ur no is TWO"); break; case 3: printf("Ur no is THREE"); break; case 4: printf("Ur no is FOUR"); break; case 5 printf("Ur no is FIVE"); break; default : printf("Ur NO is more than 5");
break; } getch(); } Looping statement: This is for repeated execution of a group of instructions till a condition evaluates to true. 1. Initialization 2. Condition 3. Increment/decrement Types 1. While loop 2. Do while loop 3. For loop
Syn. while(condition) { statements } void main() { int n=1; clrscr(); while(n<=10) { printf("\n N=%d",n); n++;
} getch(); } //write a pgm to print the multiplication table for given number void main() { int n=1,a; clrscr(); printf("Enter your values:"); scanf("%d",&a); while(n<=10) { printf("\n%d * %2d = %3d",a,n,n*a); n++; } getch(); } Dowhile Loop Syntax: do { statements } while(condition); void main() {
prg. to reverse the number write a pgm to print odd and even from 1 to 50 Write a pgm to print 1 22 333 Write a pgm to print sum of digits 123=1+2+3=>6 --------------------------------
{ int r=1,c; clrscr(); while(r<=3) { c=1; while(c<=r) { printf("* "); c++; } printf("\n"); r++; } getch(); }
prg. to reverse the number Write a pgm to count the occurrance of a given character in the given string Write a pgm to insert the given character in given string Write a pgm to print 1 *** 22 ** 333 * Write a pgm to print sum of digits 123=1+2+3=>6