Skillrack IL 1

Solutions for Skillrack CSE1002 Inlab 1

Total time : 20 mins

Challenges : 1

Question (Nature of Digit in Position)

Given a number ‘n’ and a position ‘p’, write an algorithm and the subsequent ‘C’ program to check if the ‘p-th’ digit from the  leftmost position  of ‘n’ is odd or even. For example, if ‘n’ is 3145782 and p is 4 then you have to check if 5 is odd or even. Since it is odd print ‘Odd’. Make your code accept numbers of larger size.

Input Format:

The first line contains the number, n

The second line contains the position, p

Output Format:

Print either “Odd” or “Even”

Solution

#include< stdio.h >
void main()
{
	int p,i=0,a[10];
	long int n;
	scanf("%d%d",&n,&p);
	while(n > 0)
	{
		a[i++]=n%10;
		n/=10;
	}
	if(a[i-p]%2==0)
	printf("Even");
	else
	printf("Odd");
}

Input

INPUT:
3145782
4

Output

OUTPUT:
Odd
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s