I am trying to write a program to calculate nCr where n and r are input by the user. My approach is following:
I ask to user to enter value of n.
Then I ask the user to enter value of r.
Then I calculate value of n-r.
Then using three while loops I calculate the value of n! r! and (n-r)!
Then I divide by n! by r! and (n-r)!
Then I display it to the user.
Following is my code:
.data prompt1: .asciiz "Enter the value of n:\n" prompt2: .asciiz "Enter the value of r:\n" message: .asciiz "The value of nCr is: " .text #Prompt the user to enter value of n li $v0,4 la $a0,prompt1 syscall #Get the value of n li $v0,5 syscall #Store the value of n in $t0 move $t0,$v0 #Prompt the user to enter value of r li $v0,4 la $a0,prompt1 syscall #Get the value of r li $v0,5 syscall #Store the value of r in $t2 move $t2,$v0 #Getting the value of (n-r) sub $t4,$t0,$t2 #Calculating value of n! addi $t1,$zero,1 while: blt $t0,1,exit mul $t1,$t1,$t0 sub $t0,$t0,1 j while exit: li $v0,10 syscall #Calculating the value of r! addi $t3,$zero,1 while: blt $t2,1,exit mul $t3,$t3,$t2 sub $t2,$t2,1 j while exit: li $v0,10 syscall #Calculating the value of (n-r)! addi $t5,$zero,1 while: blt $t4,1,exit mul $t5,$t5,$t4 sub $t4,$t4,1 j while exit: li $v0,10 syscall #Getting the final value div $s0,$t1,$t3 div $s1,$s0,$t5 #Displaying the message li $v0,4 la $a0,message syscall #Display the answer addi $a0,$s1,0 syscall
The Error which I am getting is this:
- line 46 column 6: label "while" already defined.
- line 46 column 6: label "exit" already defined.
What I am doing wrong here? Is it not the correct way to use three while loops in a mips