C All Exercises & Assignments

Write a C program to check whether a number is even or odd

Description:

Write a C program to check whether a number is even or odd.

Note: Even number is divided by 2 and give the remainder 0 but odd number is not divisible by 2 for eg. 4 is divisible by 2 and 9 is not divisible by 2.

Conditions:

  • Create a variable with name of number.
  • Take value from user for number variable.
#include <stdio.h>
int main()
{
  int number;
  printf("Enter the number=");
  scanf("%d", &number);

  if (number % 2 == 0)
  {
    printf("Number is Even.");
  }
  else
  {
    printf("Number is Odd.");
  }
  return 0;
}
Tutorials Class - Output Window

Enter the Number=9
Number is Odd.

Write a C program to swap value of two variables using the third variable.

Description:

You need to create a C program to swap values of two variables using the third variable.

Hint:

You can use a temp variable as a blank variable to swap the value of x and y.

Conditions:

  • Take three variables for eg. x, y and temp.
  • Swap the value of x and y variable.
#include <stdio.h>
int main()
{
  int x = 10;
  int y = 20;
  int temp;

  temp = x;
  x = y;
  y = temp;

  printf("X=%d	Y=%d", x, y);
  return 0;
}
Tutorials Class - Output Window

X=20 Y=10

Write a C program to check whether a user is eligible to vote or not.

Description:

You need to create a C program to check whether a user is eligible to vote or not.

Conditions:

  • Minimum age required for voting is 18.
  • You can use decision making statement.
#include <stdio.h>

int main()
{
  unsigned int age;

  printf("Enter your age=");
  scanf("%d", &age);

  if (age >= 18)
  {
    printf("User is eligible to vote");
  }
  else
  {
    printf("User is not eligible to vote");
  }
  return 0;
}
Tutorials Class - Output Window

Enter your age=28
User is eligible to vote

Write a C program to check whether an alphabet is Vowel or Consonant

Description:

You need to create a C program to check whether an alphabet is Vowel or Consonant.

Conditions:

  • Create a character type variable with name of alphabet and take the value from the user.
  • You can use conditional statements.
#include <stdio.h>

int main()
{
	char alphabet;
	printf("Enter an alphabet: ");
	scanf("%c", &alphabet);

	if (alphabet == 'a' || alphabet == 'A')
	{
		printf("%c is a vowel.", alphabet);
	}
	else if (alphabet == 'e' || alphabet == 'E')
	{
		printf("%c is a vowel.", alphabet);
	}
	else if (alphabet == 'i' || alphabet == 'I')
	{
		printf("%c is a vowel.", alphabet);
	}
	else if (alphabet == 'o' || alphabet == 'O')
	{
		printf("%c is a vowel.", alphabet);
	}
	else if (alphabet == 'u' || alphabet == 'U')
	{
		printf("%c is a vowel.", alphabet);
	}
	else
	{
		printf("%c is a consonant.", alphabet);
	}
	return 0;
}
Tutorials Class - Output Window

Enter an alphabet: O
O is a vowel.

Write a C program to find the maximum number between three numbers

Description:

You need to write a C program to find the maximum number between three numbers.

Conditions:

  • Create three variables in c with name of number1, number2 and number3
  • Find out the maximum number using the nested if-else statement
#include <stdio.h>
int main()
{
    int number1, number2, number3;

    printf("Enter three numbers: ");
    scanf("%d%d%d", &number1, &number2, &number3);

    if(number1 > number2)
    {
        if(number1 > number3)
        {
            printf("Number1 is max with value of %d",number1);
        }
        else
        {
            printf("Number3 is max with value of %d",number3);
        }
    }
    else
    {
        if(number2 > number3)
        {
            printf("Number2 is max with value of %d",number2);
        }
        else
        {
            printf("Number3 is max with value of %d",number3);
        }
    }
    return 0;
}
Tutorials Class - Output Window

Enter three numbers: 10
20
30
Number3 is max with value of 30

Write a C program to check whether number is positive, negative or zero

Description:

You need to write a C program to check whether number is positive, negative or zero

Conditions:

  • Create variable with name of number and the value will taken by user or console
  • Create this c program code using else if ladder statement
#include <stdio.h>
int main()
{
    int number;
 
    printf("Enter a number : ");
    scanf("%d", &number);
    if (number >= 0)
        printf("%d is positive \n", number);
    else if(number==0)    
        printf("%d is  zero \n", number);
    else
        printf("%d is negative \n", number);
        
    return 0;    
}
Tutorials Class - Output Window

Enter a number : 10
10 is positive

Write a C program to calculate Electricity bill.

Description:

You need to write a C program to calculate electricity bill using if-else statements.

Conditions:

  • For first 50 units – Rs. 3.50/unit
  • For next 100 units – Rs. 4.00/unit
  • For next 100 units – Rs. 5.20/unit
  • For units above 250 – Rs. 6.50/unit
  • You can use conditional statements.
#include<stdio.h>
int main()
{
	float bill, units;
	
	printf("Enter the units consumed=");
	scanf("%f",&units);
	
	if(units<=50 && units>=0)
	{
		bill=units*3.50;
		printf("Electricity Bill=%f Rupees",bill);
	}
	else if(units<=100 && units>50)
	{
		bill=50*3.50+(units-50)*4;
		printf("Electricity Bill=%f Rupees",bill);

	}
	else if(units<=250 && units>150)
	{
		bill=50*3.50+100*4+(units-150)*5.20;
		printf("Electricity Bill=%f Rupees",bill);

	}
	
	else if(units>250)
	{
		bill=50*3.50+100*4+100*5.20+(units-250)*6.50;
		printf("Electricity Bill=%f Rupees",bill);

	}
	else
	{
		printf("Please enter valid consumed units...");
	}
	return 0;
}
Tutorials Class - Output Window

Enter the units consumed=278.90
Electricity Bill=1282.84 Rupees

Write a C program to print 1 to 10 numbers using the while loop

Description:

You need to create a C program to print 1 to 10 numbers using the while loop

Conditions:

  • Create a variable for the loop iteration
  • Use increment operator in while loop
#include<stdio.h>
int main()
{
	int x=1;
	while(x<=10)
	{
		printf("%d\n",x);
		x++;
	}
	return 0;
}
Tutorials Class - Output Window

1
2
3
4
5
6
7
8
9
10