# 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;

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

User is eligible to vote

## 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
{
}
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