Pages

Monday, January 02, 2012

Program for odd and even number

This program determines wether a given number is odd or even.

#include<stdio.h>

void main(){
int num;
clrscr();
printf("Enter number : ");
scanf("%d",&num);
if(num%2==0)
printf("%d is an even number");
else
printf("%d is an odd number");
getch();

}

output:

Enter number : 4
4 is an even number

Enter number : 71
71 is an odd number


This program finds out odd or even numbers within a range

#include<stdio.h>

void main(){
int min,max,num;
clrscr();
printf("Enter minimum range : ");
scanf("%d",&min);
printf("Enter maximum range : ");
scanf("%d",&max);

printf("\nEven numbers in given range are : ");
for(num=min;num<=max;num++)
if(num%2==0)
printf("%d ",num);
printf("\n\nOdd numbers in given range are : ");

for(num=min;num<=max;num++)
if(num%2!=0)
printf("%d ",num);
getch();

}

output:

Enter minimum range 2
Enter maximum range 20

Even numbers in given range are : 2 4 6 8 10 12 14 16 18 20

Odd numbers in given range are : 1 3 5 7 9 11 13 15 17 19

Adding 1 to each digit of a number

This program adds 1 to each digit of the number.


If a five digit number is input through the keyboard, write a program to print a new number by adding one to each of its digits.For example if the number that is input is 12391 then the output should be displayed as 23402


Here first we have added one to each digit. If any digit becomes 10 then replacing it with 0

#include<stdio.h>
void main(){

long r,num,temp,sum=0,i,temp_sum=0;
clrscr();

printf("Enter 5 digit number : ");
scanf("%ld",&num);

printf("\nThe number after adding 1 to each digit : ");

while(num!=0){
r=num%10;
temp=r+1;
if(temp==10)
temp=0;
num=num/10;
sum=(sum*10)+temp;

}

while(sum!=0){
i=sum%10;
sum=sum/10;
temp_sum=(temp_sum*10)+i;
}
printf("%ld",temp_sum);
getch();

}


output:

Enter 5 digit number : 12345
The number after adding 1 to each digit : 23456

Enter 5 digit number : 12935
The number after adding 1 to each digit : 23046