Radical Developers welcomes all the visitors to be a member of the team. Come join us. Its all about open-source concept. All Programs are written and tested in MacOS X, Unix, Linux and may not match with the outputs of Turbo C++ in Windows

Sunday, January 9, 2011

If a 4-digit number is input though the keyboard. Write a program to obtain the sum of the first & last digit of this number.


Source Code :

#include<stdio.h>
void main()
{
      int a,sum=0,rem,div,ctr=1;
      clrscr();
      printf("\nEnter a 4-digit Number : ");
      scanf("%d",&a);
      rem=a;
      div=a;
      while(rem!=0)
      {
            rem=rem%10;
            div=div/10;
            if(ctr==1 || ctr==4)
            {
                  sum+=rem;
            }
            rem=div;
            ctr++;
      }
      printf("\nSum of the First and Last Digit : %d",sum);
      getch();
}

Output :

Enter a Number : 1234

Sum of the First and Last Digit : 5

No comments:

Post a Comment