5. Algorithm: To check the number is positive.
Step 1: Start
Step 2: Read a number and store in the variable Num.
Step 3: If the number stored in the variable Num is grater
than 0, print “ The number is Positive”.
Step 4: Stop
17/11/2023
5
Kristu Jayanti College
6. Example: C program to check the given number is positive.
#include<stdio.h>
#include<conio.h>
void main()
{
int Num;
clrscr();
printf(“enter a numbern”);
scanf(“%d”,&Num);
// if statement
if(Num>0)
printf(“The number is positive”);
getch();
}
17/11/2023
6
Kristu Jayanti College
9. Algorithm: To check the number is positive
or negative.
Step 1: Start
Step 2: Read a number and store in the variable Num.
Step 3: If the number stored in the variable Num is grater
than 0 go to Step 4, otherwise, go to step 5.
Step 4: print “ The number is Positive”. Goto Step 6.
Step 5: Print “The number is Negative”.
Step 6: Stop
17/11/2023
9
Kristu Jayanti College
10. Example: C program to check the given number is positive or negative.
#include<stdio.h>
#include<conio.h>
void main()
{
int Num;
clrscr();
printf(“enter a numbern”);
scanf(“%d”,&Num);
// if else ladder
if(Num>0)
printf(“The number is positive”);
else
printf(“The number is negative”);
getch();
} 17/11/2023
10
Kristu Jayanti College
11. if-else ladder statement: Syntax
// any if-else ladder starts with an if statement only
if(condition1)
{
// statements
}
else if(condition2)
{
// this else if will be executed when condition in if is false and
// the condition of this else if is true
}
.... // once if-else ladder can have multiple else if
else
{
// at the end we put else
} 17/11/2023
11
Kristu Jayanti College
13. Example: C program to check the given number is positive or negative.
#include<stdio.h>
#include<conio.h>
void main()
{
int Num;
clrscr();
printf(“enter a numbern”);
scanf(“%d”,&Num);
// if else ladder
if(Num>0)
printf(“The number is positive”);
else if(Num<0)
printf(“The number is negative”);
else
printf(“The number is zero”);
getch();
17/11/2023
13
Kristu Jayanti College
16. Example: C program to check the given number is positive or negative.
i) If positive, check whether if is odd or even
ii) if negative, display its square.
#include<stdio.h>
#include<conio.h>
void main() {
int Num;
clrscr();
printf(“enter a numbern”);
scanf(“%d”,&Num); // reading a number
if(Num>0)
{
printf(“The number is positive”);
if(Num%2==0) //nested if
printf(“The number is even”);
else
printf(“The number is odd”);
}
else {
printf(“The number is positive”);
printf(“The square of the number is %d”, Num*Num);
}
getch();
}
17/11/2023
16
Kristu Jayanti College
17. switch statement: Syntax
switch(expression)
{
case value1: statement_1;
break;
case value2: statement_2;
break;
. . .
case value_n: statement_n;
break;
default: default_statement;
} 17/11/2023
17
Kristu Jayanti College
19. Example: C program to display the day on user’s choice using switch statement.
#include<stdio.h>
#include<conio.h>
void main() {
int choice;
clrscr();
printf(“enter a choicen”);
scanf(“%d”,&choice); // reading a number
switch(choice) {
case 1: printf(“It is Monday”); break;
case 2: printf(“It is Tuesday”); break;
case 3: printf(“It is Wednesday”); break;
case 4: printf(“It is Thursday”); break;
case 5: printf(“It is Friday”); break;
case 6: printf(“It is Saturday”); break;
case 7: printf(“It is Sunday”); break;
default: printf(“Invalid choice”);
}
getch();
} 17/11/2023
19
Kristu Jayanti College