Tutorial 09
01. Swap two values stored in two different variables.
#include <stdio.h>
int main()
{
int a = 10;
int b = 20;
int c;
c = a;
a = b;
b = c;
printf("%i\n%i", a,b);
return 5;
}
02. Check whether an entered number is negative, positive or zero.
#include<stdio.h>
int main()
{
int value;
printf ("Enter your value: ");
scanf("%i", &value);
if (value == 0)
printf("Your number is zero");
else
if (value > 0)
printf("Your Value is Positive");
else
printf("Your value is Negetive");
return 5;
}
03. Check whether an entered year is leap year or not.
#include<stdio.h>
int main()
{
int year;
int value;
printf ("Enter Year : ");
scanf("%i" , &year);
value = year % 4;
if (value == 0)
printf("Leap Year");
else
printf("Not a leap year");
return 5;
}
04. Write a program that asks the user to type in two integer values at the terminal. Test these two numbers to determine if the first is evenly divisible by the second, and then display an appropriate message at the terminal.
#include <stdio.h>
int main()
{
int value1;
int value2;
int value3;
printf("Enter The Number 1 :");
scanf("%i", &value1);
printf("Enter The Number 2 :");
scanf("%i", &value2);
value3 = value1 % value2;
if (value3 == 0)
printf("Number 1 is evenly divisable by Number 2");
else
printf("Number 1 is not evenly divisably by Number 2");
return 5;
}
05. Write a program that accepts two integer values typed in by the user. Display the result of dividing the first integer by the second, to three-decimal-place accuracy. Remember to have the program check for division by zero.
#include<stdio.h>
int main ()
{
float value1;
float value2;
float value3;
printf("Enter The Number 1 :");
scanf("%f", &value1);
printf("Enter The Number 2 :");
scanf("%f", &value2);
value3 = value1 / value2;
printf("Value is :%.3f" ,value3 );
return 0;
}
06. Write a program that takes an integer keyed in from the terminal and extracts and displays each digit of the integer in English. So, if the user types in 932, the program should display nine three two. Remember to display “zero” if the user types in just a 0.
#include <stdio.h>
int main()
{
int number;
int value;
printf("Enter the Number : ");
scanf("%i",&number);
if (number != 0)
{
value = number / 100;
printf("%i\n" , value);
number = number % 100;
value = number / 10;
printf("%i\n" , value);
number = number % 10;
value = number / 1;
printf("%i\n" , value);
number = number % 1;
}
else
{
printf("Your Value is zero");
}
return 0;
}