Sum the selected digits


If a four digit integer number is input through the keyboard then write a c program to find the sum of first and last digit.

Sample Input: 3254
Output: 7
Hints: 3(1st digit)+4(last digit)=7 

Source Code: 

২টি মন্তব্য:

  1. #include
    int main()
    {
    int num,sum=0,i,lastDigit,temp=0;
    printf("Enter any number: ");
    scanf("%d",&num);
    temp=num%10;
    while(num!=0)
    {
    lastDigit=num;
    num=num/10;
    }
    sum=temp+lastDigit;
    printf("\nSum of first and last digit is:%d\n",sum);
    }

    উত্তরমুছুন
  2. #include
    main()
    {
    int num,sum=0,i,lastDigit,p=0;
    printf("Enter the number: ");
    scanf("%d",&num);
    p=num%10;
    while(num!=0)
    {
    lastDigit=num;
    num=num/10;
    }
    sum=p+lastDigit;
    printf("Sum of first and last digit is:%d\n",sum);
    }

    উত্তরমুছুন