Mrs. G. Nagalakshmi, M.Sc., M.Phil.,
Ms. N. Malathi, M.Sc.,
Programming in C (18UAMA41)
II B.Sc. Mathematics (SF)
Example of if statement
#include <stdio.h>
#include <conio.h>
void main()
{
int x = 20;
int y = 22;
if (x<y)
printf("Variable x is less than y);
getch();
}
Output:
Variable x is less than y
Example for if … else statement
#include <stdio.h>
#include <conio.h>
void main()
{
int age;
printf("Enter your age:");
scanf("%d",&age);
if(age >=18)
{
printf("You are eligible for voting");
}
else
{
printf("You are not eligible for voting");
}
getch();
}
Output:
Enter your age:14 You are not eligible for voting
Example of nested if..else
#include <stdio.h>
#include <conio.h>
void main()
{
int var1, var2;
printf("Input the value of var1:");
scanf("%d", &var1);
printf("Input the value of var2:");
scanf("%d",&var2);
if (var1 != var2)
{
printf("var1 is not equal to var2n");
if (var1 > var2)
printf("var1 is greater than var2n");
else
printf("var2 is greater than var1n");
}
else
printf("var1 is equal to var2n");
getch();
}
Example of nested if..else
Output:
Input the value of var1:12
Input the value of var2:21
var1 is not equal to var2
var2 is greater than var1
Example for elseif
#include <stdio.h>
#include <conio.h>
void main()
{
int var1, var2;
printf("Input the value of var1:");
scanf("%d", &var1);
printf("Input the value of var2:");
scanf("%d",&var2);
if (var1 !=var2)
printf("var1 is not equal to var2n");
else if (var1 > var2)
printf("var1 is greater than var2n");
else if (var2 > var1)
printf("var2 is greater than var1n");
Else
printf("var1 is equal to var2n");
getch();
}
Example for elseif
Output:
Input the value of var1:12
Input the value of var2:21
var1 is not equal to var2
Example for switch statement
#include <stdio.h>
#include <conio.h>
int main()
{
int num = 8;
switch (num)
{
case 7:
printf("Value is 7");
break;
case 8:
printf("Value is 8");
break;
case 9:
printf("Value is 9");
break;
default:
printf("Out of range");
break;
}
getch();
}
Output:
Programming in c (18 uama41)(1)
Programming in c (18 uama41)(1)

Programming in c (18 uama41)(1)

  • 1.
    Mrs. G. Nagalakshmi,M.Sc., M.Phil., Ms. N. Malathi, M.Sc., Programming in C (18UAMA41) II B.Sc. Mathematics (SF)
  • 7.
    Example of ifstatement #include <stdio.h> #include <conio.h> void main() { int x = 20; int y = 22; if (x<y) printf("Variable x is less than y); getch(); } Output: Variable x is less than y
  • 9.
    Example for if… else statement #include <stdio.h> #include <conio.h> void main() { int age; printf("Enter your age:"); scanf("%d",&age); if(age >=18) { printf("You are eligible for voting"); } else { printf("You are not eligible for voting"); } getch(); } Output: Enter your age:14 You are not eligible for voting
  • 11.
    Example of nestedif..else #include <stdio.h> #include <conio.h> void main() { int var1, var2; printf("Input the value of var1:"); scanf("%d", &var1); printf("Input the value of var2:"); scanf("%d",&var2); if (var1 != var2) { printf("var1 is not equal to var2n"); if (var1 > var2) printf("var1 is greater than var2n"); else printf("var2 is greater than var1n"); } else printf("var1 is equal to var2n"); getch(); }
  • 12.
    Example of nestedif..else Output: Input the value of var1:12 Input the value of var2:21 var1 is not equal to var2 var2 is greater than var1
  • 15.
    Example for elseif #include<stdio.h> #include <conio.h> void main() { int var1, var2; printf("Input the value of var1:"); scanf("%d", &var1); printf("Input the value of var2:"); scanf("%d",&var2); if (var1 !=var2) printf("var1 is not equal to var2n"); else if (var1 > var2) printf("var1 is greater than var2n"); else if (var2 > var1) printf("var2 is greater than var1n"); Else printf("var1 is equal to var2n"); getch(); }
  • 16.
    Example for elseif Output: Inputthe value of var1:12 Input the value of var2:21 var1 is not equal to var2
  • 19.
    Example for switchstatement #include <stdio.h> #include <conio.h> int main() { int num = 8; switch (num) { case 7: printf("Value is 7"); break; case 8: printf("Value is 8"); break; case 9: printf("Value is 9"); break; default: printf("Out of range"); break; } getch(); } Output: