Introduction to C Programming - Operators, If else , nested if else, switch case , for loop

Check if a number is an even number

 
      #include<stdio.h>

int main()
{
	int a = 0;
	printf("Enter a number");
	scanf("%d", &a);
	if (a % 2 == 0)
	{
		printf("Its an even number");
	}
	else
	{
		printf("Its not an even number");
	}
	
	system("pause");
	return 0;
}
    
Output

Find the greatest of two numbers

 
      #include<stdio.h>

int main()
{
	int a = 0, b = 0;
	printf("Enter two numbers");
	scanf("%d%d", &a, &b);
	if (a > b)
	{
		printf("a is greater");
	}
	else
	{
		printf("b is greater");
	}
	system("pause");
	return 0;
}
    
Output

Write a program to check if a student has passed an exam by getting the input mark

 
      #include<stdio.h>

	int main()
	{
		int mark=0;
		printf("Enter the mark");
		scanf("%d", &mark);
		if (mark >= 40)
		{
			printf("you have passed");
		}
		else
		{
			printf("you have failed");
		}
		system("pause");
		return 0;
	}
    
Output

Demonstrate the use of nested if else with an example

 
      #include<stdio.h>

	int main()
	{
		int input = 0;
		printf("Enter a number");
		scanf("%d", &input);
		if (input == 1)
		{
			printf("one");
		}
		else if (input == 2)
		{
			printf("two");
		}
		else if (input == 3)
		{
			printf("three");
		}
		else
		{
			printf("Invalid Input");
		}
		system("pause");
		return 0;
	}
    
Output

Switch case statement example

 
      #include<stdio.h>

	int main()
	{
		int input = 0;
		printf("Enter a number");
		scanf("%d", &input);
		switch (input)
		{
		case 1:
			printf("one");
			break;
		case 2:
			printf("two");
			break;
		default:
			printf("Invalid Input");
			break;
		}
		system("pause");
		return 0;
	}
    
Output

Switch case statement with character input

 
      #include<stdio.h>

	int main()
	{
		char input;
		printf("Enter a choice");
		scanf("%c", &input);
		switch (input)
		{
		case 'm':
			printf("addition\n subtraction\ndivision");
			break;
		case 'l':
			printf("You have been logged out");
			break;
		default:
			printf("Invalid Input");
			break;
		}
		system("pause");
		return 0;
	}
    
Output

Check if an input year is a leap year

 
      #include<stdio.h>

	int main()
	{
		int year = 0;
		printf("Enter the year");
		scanf("%d", &year);
		if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0))
		{
			printf("The year is a leap year");
		}
		else
		{
			printf("The year is not a leap year");
		}
		system("pause");
		return 0;
	}
    
Output

Display all the odd numbers between 1 and 100

 
      #include<stdio.h>
	int main()
	{
		int i=0;
		for (i = 1; i < 100;i++)
		{
			if (i % 2 != 0)
			{
				printf("%d", i);
			}
		}
		system("pause");
		return 0;
	}
    
Output