Problem Solving using C Language

0 of 77 lessons complete (0%)

Conditional Statement

if-else statement

Example: if

// Program to display a number if it is negative
#include <stdio.h>
int main()
{
	int number;
	printf("Enter an integer: ");
	scanf("%d", &number);
	// true if number is less than 0
	if (number < 0)
	{
		printf("You entered %d.\n", number);
	}
	printf("The if statement is easy.");
	return 0;
}

if…else Statement

 

If…else program


// Check whether an integer is odd or even
#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;
}