adsense

Hii, welcome to my site. My name is Om prakash kartik. This blog helps you to learn programming languages concepts.

Program in C and C++ to find sum of first and last digits of a given number

Program in C and C++ to find sum of first and last digits of a given number
C Program
#include<stdio.h>
 int main()
  {
      int n, l = 0, f = 0;
      printf("Enter a number :- ");
      scanf("%d",&n);
      /*check a given number is greater than 10 if given number is greater 
      than 10 then calculate sum of first and last digit otherwise not */
      if(n >= 10){
       //Find last digit
       l = n % 10;
       //Find first digit
        while(n >= 10){
            n /= 10;
       }
       f = n;  
       printf("Sum of first and last digits : %d",(f + l));
      }
      else{
        printf("Not possible.");
      }
  return 0;
  }
C++ Program
#include<iostream>
 int main()
  {
      int n, l = 0, f = 0;
      std::cout<<"Enter a number :- ";
      std::cin>>n;
       /*check a given number is greater than 10 if given number is greater 
      than 10 then calculate sum of first and last digit otherwise not */
      if(n >= 10){
       //Find last digit
       l = n % 10;
       //Find first digit
        while(n >= 10){
            n /= 10;
       }
       f = n;  
       std::cout<<"Sum of first and last digits : "<<f + l;
      }
      else{
          std::cout<<"Not possible.";
      }
    return 0;
  }
Output :-













Post a Comment

0 Comments