Pages

Monday, 2 September 2013

ELEMENTS OF C PROGRAMMING

Q) Write a C programme to check a odd or even number

A) c program to check odd or even:

We will determine whether a number is odd or even by using different methods all are provided with a code in c language. As you have study in mathematics that in decimal number system even numbers are divisible by 2 while odd are not so we may use modulus operator(%) which returns remainder, For example 4%3 gives 1 ( remainder when four is divided by three). Even numbers are of the form 2*p and odd are of the form (2*p+1) where p is is an integer. C program to check odd or even using modulus operator

#include<stdio.h>
main()
{
int n;
printf("Enter an integer\n");
scanf("%d",&n);
if ( n%2 == 0 )
printf("Even\n");
else
printf("Odd\n");
return 0;
}

We can use bitwise AND (&) operator to check
odd or even, as an example consider binary of 7
(0111) when we perform 7 & 1 the result will be
one and you may observe that the least
significant bit of every odd number is 1, so
( odd_number & 1 ) will be one always and also
( even_number & 1 ) is zero.
C program to check odd or even using bitwise
operator

#include<stdio.h>
main()
{
int n;
printf("Enter an integer\n");
scanf("%d",&n);
if ( n & 1 == 1 )
printf("Odd\n");
else
printf("Even\n");
return 0;
}

Find odd or even using conditional operator

#include<stdio.h>
main()
{
int n;
printf("Input an integer\n");
scanf("%d",&n);
n%2 == 0 ? printf("Even\n") : printf("Odd\n");
return 0;
}

C program to check odd or even without using
bitwise or modulus operator

#include<stdio.h>
main()
{
int n;
printf("Enter an integer\n");
scanf("%d",&n);
if ( (n/2)*2 == n )
printf("Even\n");
else
printf("Odd\n");
return 0;
}

In c programming language when we divide two
integers we get an integer result, For example
the result of 7/3 will be 2.So we can take
advantage of this and may use it to find whether
the number is odd or even. Consider an integer
n we can first divide by 2 and then multiply it by
2 if the result is the original number then the
number is even otherwise the number is odd.
For example 11/2 = 5, 5*2 = 10 ( which is not
equal to eleven), now consider 12/2 = 6 and 6 *2
= 12 ( same as original number). These are some
logic which may help you in finding if a number
is odd or not.

Q) Write a C program to check whether input
alphabet is a vowel or not.

A) This code checks whether an input alphabet
is a vowel or not. Both lower-case and upper-
case are checked.

#include <stdio.h>
int main()
{
char ch;
printf("Enter a character\n");
scanf("%c", &ch);
if (ch == 'a' || ch == 'A' || ch == 'e' || ch == 'E' ||
ch == 'i' || ch == 'I' || ch =='o' || ch=='O' || ch ==
'u' || ch == 'U')
printf("%c is a vowel.\n", ch);
else
printf("%c is not a vowel.\n", ch);
return 0;
}

Check vowel using switch statement

#include <stdio.h>
int main()
{
char ch;
printf("Input a character\n");
scanf("%c", &ch);
switch(ch)
{
case 'a':
case 'A':
case 'e':
case 'E':
case 'i':
case 'I':
case 'o':
case 'O':
case 'u':
case 'U':
printf("%c is a vowel.\n", ch);
break;
default:
printf("%c is not a vowel.\n", ch);
}
return 0;
}
Function to check vowel
int check_vowel(char a)
{
if (a >= 'A' && a <= 'Z')
a = a + 'a' - 'A'; /* Converting to lower case or
use a = a + 32 */
if (a == 'a' || a == 'e' || a == 'i' || a == 'o' || a == 'u')
return 1;
return 0;
}
This function can also be used to check if a
character is a consonant or not, if it's not a
vowel then it will be a consonant, but make sure
that the character is an alphabet not a special
character.
Q) Write C program to perform addition,
subtraction, multiplication and division.
A) C program to perform basic arithmetic
operations which are addition, subtraction,
multiplication and division of two numbers.
Numbers are assumed to be integers and will be
entered by the user.
#include <stdio.h>
int main()
{
int first, second, add, subtract, multiply;
float divide;
printf("Enter two integers\n");
scanf("%d%d", &first, &second);
add = first + second;
subtract = first - second;
multiply = first * second;
divide = first / (float)second; //typecasting
printf("Sum = %d\n",add);
printf("Difference = %d\n",subtract);
printf("Multiplication = %d\n",multiply);
printf("Division = %.2f\n",divide);
return 0;
}
In c language when we divide two integers we
get integer result for example 5/2 evaluates to 2.
As a general rule integer/integer = integer and
float/integer = float or integer/float = float. So
we convert denominator to float in our program,
you may also write float in numerator. This
explicit conversion is known as typecasting.
Q) Write a C programme to check a Leap year.
A) C program to check leap year: c code to
check leap year, year will be entered by the
user.
#include <stdio.h>
int main()
{
int year;
printf("Enter a year to check if it is a leap year
\n");
scanf("%d", &year);
if ( year%400 == 0)
printf("%d is a leap year.\n", year);
else if ( year%100 == 0)
printf("%d is not a leap year.\n", year);
else if ( year%4 == 0 )
printf("%d is a leap year.\n", year);
else
printf("%d is not a leap year.\n", year);
return 0;
}

No comments:

Post a Comment