Skip to content
Snippets Groups Projects
Commit 739a5617 authored by isabel.lomas's avatar isabel.lomas
Browse files

Published code

parents
Branches main
No related tags found
No related merge requests found
# CS 261 Program 1: Fibonacci
This is my submission for Program 1: Fibonacci for CS 261 - C & Assembly Language Programming, instructed by Dr. Paul Bonamy during the Spring 2024 term at WSU Vancouver.
This work received a score of 50/50 points.
I don't understand why I wrote it the way I did.
According to the course syllabus, students are allowed to publish their code after the course has ended, so I have done so here.
fib.c 0 → 100644
#include <stdio.h>
int main() {
int n = -1;
while(n != 0) {
// PROMPT USER FOR DESIRED FIBONACCI NUMBER
printf("Which fibonacci number?: ");
scanf("%d", &n);
// IGNORE & REPROMPT FOR NEGATIVE NUMBERS
if (n < 0) {continue;}
int value;
int last1 = 1;
int last2 = 0;
// Handling special case of 0,1 at beginning of sequence.
if (n > 1) {
// Adding last two digits in sequence until we reach the desired fibonacci number.
for (int c=1; c != n; c++) {
value = last1+last2;
last2 = last1;
last1 = value;
}
} else {
// 0: 0, 1: 1, everything else handled by for loop above.
value = n;
}
// Print result back to user.
printf("Fibonacci number %d is %d\n\n", n, value);
}
return 0;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment