Problem: C program for "If a four digit number is input through the keyboard, write a program to obtain the sum of first and last digit of this number."
Logic used :
- Divide the number by 1000 and store the result in 'first' variable
- Take remainder out using modulus operator by 10 and store result in 'last' variable
- Add both variable and store the result in another variable
- Print that variable
Demo :
f=num/1000;
l=num%10;
sum = f+l;
C program :
// Program by Shyam Kanth#include<stdio.h>int main(){int num, f,l,sum;printf("Enter the 4 digit number : ");scanf("%d",&num);f=num/1000;l=num%10;sum = f+l;printf("Sum of %d and %d is %d.",f,l,sum);return 0;}
- #include < stdio.h > : Header file for input output
- int : Integer data type
- num, f,l,sum : Variable used
- printf : Function used for displaying message
- scanf : Function used for taking input
Do comment, if any doubt
Know about me : Shyam Kanth
No comments:
Post a Comment