Program - 1
Program - 1
o.h> void main() { int a, b, t; clrscr(); printf(Enter two Numbers:); scanf(%d%d, &a, &b); if(a>b) { t = a; a = b; b = t; } printf(The values of a = %d and b = %d, a, b); getch(); } Output:Enter two Numbers: 8 5The values of a = 5 and b = 8
Program 2 /* Leap Year Example for if else */ #include<stdio.h> #include<conio.h> void main() { int year; clrscr(); printf(Enter a Year:); scanf(%d, &year); if(year % 4 == 0) { printf(%d is a Leap Year, year); } Else { printf(%d is not a Leap Year, year);
Program 3 /* Biggest among two numbers Example for if else */ #include<stdio.h> #include<conio.h> void main() { int a,b; clrscr(); printf(Enter two numbers:); scanf(%d%d, &a, &b); if(a>b)printf(%d is Big, a); elseprintf(%d is Big, b); getch(); } Output:Enter two numbers: 20 3030 is Big Program 4 /* Display the types of character Example for Nested if - else */ #include<stdio.h> #include<conio.h> void main() { char ch; clrscr(); printf(Enter a character:); scanf(%c, &ch); if((ch >= a && ch = z) || (ch >= A && ch < = Z)) printf(%c is an Alphabet.\n, ch); else if(ch >= 0 && ch <= 9) printf(%c is a Digit.\n, ch); elseprintf(%c is a Special Character, ch); getch(); }
Output:Enter a character: ** is a Special Character Program 5 /* Arithmetic Operations (Calculator) Example for switch case */ #include<stdio.h> #include<conio.h> void main() { char ch; int a, b, ans; clrscr(); printf(Calculator:\n + : Addition\n - : Subtract \n * :Multiply\n / : Division\n % : Mod Division\n); printf(Enter the code:); scanf(%c, &ch); printf(\nEnter two values: ); scanf(%d%d, &a, &b); switch(ch) { case + :ans = a+b; break; case - :ans = a-b; break; case * :ans = a*b; break; case / :ans = a/b;break; case % :ans = a%b; break; default: printf(\nInvalid Operator); ans = 0; } printf(\nThe Result is %d, ans); getch(); } Output:Calculator:+ : Addition- : Subtract* : Multiply/ : Division% : Mod DivisionEnter the Code: *Enter two values: 8 2The Result is 16. Program 6 /* Prime Number Program Example for goto label */ #include<stdio.h> #include<conio.h>
#include<math.h> void main() { int n, i, r; clrscr(); printf(Enter the positive integer value); scanf(%d, &n);i=2;step1:if(i<=sqrt(n)) { r=n%i;if(r==0) { printf(%d is not a prime, n); goto end; } } Else { i++; goto step1; } printf(%d is prime, n); getch(); end:printf( ); } Output:Enter the positive integer value 33 is prime
Looping: Program 1 /* Sum of numbers upto 10 Example for while loop */ #include<stdio.h> #include<conio.h> void main() { int sum=0, i =1; clrscr(); while(i<=10) { sum = sum + i;
i++; } printf(The sum of numbers upto 10 is %d, sum); getch(); } Output:The sum of numbers upto 10 is 55 Program 3 /* Sum of numbers upto 10Example for dowhile loop */ #include<stdio.h> #include<conio.h> void main() { int sum=0, i =1; clrscr(); do { sum = sum + i;i++; } while(i<=10); printf(The sum of numbers upto 10 is %d, sum); getch(); } Output:The sum of numbers upto 10 is 55
Program 4 /* Sum of numbers upto 10 Example for for loop */ #include<stdio.h> #include<conio.h> void main() { int sum=0, i; clrscr(); for(i=1; i<=10; i++) { sum = sum + i; }
printf(The sum of numbers upto 10 is %d, sum);getch(); } Output:The sum of numbers upto 10 is 55
Program 5 /* Print numbers upto 5 Example for break stat. */ #include<stdio.h> void main() { int i; for(i=1; i<=10; i++) { if (i==6) break; printf(%d \t, i); } } Output:1 2 3 4
/* Printing Number Pyramid Example for nested for loop */ #include<stdio.h> #include<conio.h> void main() { int i, j, n; clrscr(); printf(\nEnter a number : ); scanf(%d, &n); for(i=1; i<=n; i++) { for(j=i; j<=n; j++) printf(%d\t, j);printf(\n); } getch();
} Output:Enter a number : 12345 2345 345 45 5 Program 8 /* Generation of Fibonacci Series Example for while loop */ #include<stdio.h> #include<conio.h> void main() { int f1 = -1, f2 = 1, n, newterm = 0; clrscr(); printf(\nEnter the final term of series : ); scanf(%d, &n); while(newterm <= n) {newterm = f1 + f2; f1 = f2; f2 = newterm; printf(%d\t, newterm); } getch(); } Output:Enter the final term of series: 210 1 1 2 3 5 8 13 21