C Basics

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