top of page
Writer's pictureanitadevkar

Assignment No. 06: Program to print the last four digits of a given number.

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",&lt);

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:

  1. Accept Account number(any number) from user and stored into variable.

  2. accept how many last digits you want to print from user and stored into one variable.

  3. For calculating power, while loop is used.

  4. 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",&lt);

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


22 views0 comments

Recent Posts

See All

C Program for pyramid of number

Program: #include <stdio.h> int main() { int i,j; for(i=5;i>0;i--) //for rows { for(j=1;j<=i;j++) //for columns j loop will iterate...

Program to print Triangle

Program: #include <stdio.h> int main() { int i,j; for(i=5;i>0;i--) //for rows { for(j=0;j<i;j++) //for columns j loop will iterate...

Comments


bottom of page