Tuesday 7 August 2012

Loop control structure012


If a four digit number is input through the keyboard, write a program to obtain the sum of the first and last digit of that number.

#include <stdio.h>
#include <conio.h>

void main ( )
{
        int       md, no, co = 1, s = 0;
        clrscr ( );
             
        printf ("\n\t Enter a four digit number ......:"); scanf ("%d",&no);
                 
        while ( no > 0 )
{                   
                       md = no % 10;
                       no = no / 10;
                       if ( co == 1)
                                   s = s + md;
                       co ++;
        }
        s = s + md;
        printf ("\n\t SUM = %d",s);
        getch ( );
}

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...