Problem Statement: Accept bank account number from user and identify the last four digits of the account number. Write a program to print the last four digits from the account number separately.
#1-Program:
//First Program is without loop
#include <stdio.h>
int main()
{
long int acc,lt;
printf("Enter the account number ");
scanf("%ld", &acc);
lt = acc % 10000;
printf("Last four digits of account number is: %ld", lt);
return 0;
}
Output:
Test case-1:
Enter the account number
1000678954
Last four digits of account number is: 8954
Test case-2:
Enter the account number 2100058698
Last four digits of account number is: 8698
#2-Program:
#include <stdio.h>
int main()
{
long int acc;
int lt;
int power=1;
printf("Enter the account number ");
scanf("%ld", &acc);
printf("Enter how many last digit you want");
scanf("%d",<);
int x=lt;
while(lt>0)
{
power=power*10;
lt--;
}
printf("Last %d digits of account number is: %ld", x,acc%power);
return 0;
}
Output:
Test case-1:
Enter the account number 10004567832
Enter how many last digit you want 4
Last 4 digits of account number is: 7832
Test case-2:
Enter the account number 2100058698
Enter how many last digit you want 3
Last 3 digits of account number is: 698
Explanation:
Accept Account number(any number) from user and stored into variable.
accept how many last digits you want to print from user and stored into one variable.
For calculating power, while loop is used.
Use of Modulo operator for getting remainder. Modulo Divide the variable acc by power and print its remainder.
#3-Program:
#include <stdio.h>
#include<math.h>
int main()
{
long long int acc;
int lt;
printf("Enter the account number");
scanf("%lld",&acc);
printf("Enter the how many digits user want");
scanf("%d",<);
int x=lt;
int z=pow(10,lt);
printf("Last %d digits of a account number is=%lld",x,acc%z);
return 0;
}
Output:
Test case-1:
Enter the account number 678943215678
Enter the how many digits user want 6
Last 6 digits of a account number is=215678
Comments