Write a C program to find first and last digit of any number by using for loop. in c++
Answers
Answer:
#include <stdio.h>
int main()
{
int n, sum=0, firstDigit, lastDigit;
printf("Enter number = ");
scanf("%d", &n);
// Find last digit of a number.
lastDigit = n % 10;
➥ C program to find first and last digit of any number by using for loop
#include <stdio.h>
int main()
{
int n, sum=0, firstdigit, lastdigit;
printf("Enter number = ");
scanf("%d", &n);
lastdigit = n % 10;
while(n >= 10)
{
n = n / 10;
}
firstdigit = n;
printf("first digit = %d\n", firstdigit);
printf("last digit = %d\n", lastdigit);
return 0;
}
EXAMPLE OUTPUT
Enter number = 123456
first digit = 1
last digit = 6
Note: Program has been run in the above attachment with output attached.
➥ C++ Program to find first and last digit of any number by using for loop
#include <iostream>
using namespace std;
int main(){
int num, last;
cout<<"Enter number = ";
cin>>num;
last = num%10;
cout<<"first digit = "<< last<<endl;
while(num>=10)
{
num = num/10;
}
cout<<"last digit = "<<num;
return 0;
}
EXAMPLE OUTPUT
Enter number = 123456
first digit = 1
last digit = 6
Note: Program has been run in the above attachment with output attached.