0% found this document useful (0 votes)
61 views2 pages

Assignment 2

This C program allows the user to choose between performing basic calculations, printing the Fibonacci series, or calculating a factorial. It uses a switch statement to direct the user to the appropriate code block based on their input choice. Basic calculations include addition, subtraction, multiplication and division. Printing the Fibonacci series iterates through the terms of the series up to the number entered by the user. Calculating the factorial multiplies all integers from 1 to the given number. The program repeats if the user chooses to perform another calculation.

Uploaded by

Earl S. Alabat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
61 views2 pages

Assignment 2

This C program allows the user to choose between performing basic calculations, printing the Fibonacci series, or calculating a factorial. It uses a switch statement to direct the user to the appropriate code block based on their input choice. Basic calculations include addition, subtraction, multiplication and division. Printing the Fibonacci series iterates through the terms of the series up to the number entered by the user. Calculating the factorial multiplies all integers from 1 to the given number. The program repeats if the user chooses to perform another calculation.

Uploaded by

Earl S. Alabat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

#include <stdio.

h>
#include <stdlib.h>

int main()
{
int choice_num, repick;

printf("Press 1 if you wish to perform basic calculation.\n");


printf("Press 2 if you wish to print Fibonacci series.\n");
printf("Press 3 if you wish to calculate the factorial of a number.\n\n");

do
{
printf("Select from the following options the calculation you wish to
execute.\n");
printf("Please enter your choice:");
scanf("%d",&choice_num);
printf("\n");

switch (choice_num)
{
case 1:
{
double first_num, second_num, sum, difference, product,
quotient;
printf("This is a program that perform basic
calculation.\n");
printf("Please enter the first number:");
scanf("%lf", &first_num);
printf("Please enter the second number:");
scanf("%lf", &second_num);

sum = first_num+second_num;
difference = first_num-second_num;
product = first_num*second_num;
printf("\n");
printf("Results:\n");
printf("Sum = %lf \n", sum);
printf("Difference = %lf \n", difference);
printf("Product = %lf \n", product);
if (second_num == 0)
printf("Quotient is Undefined. Division of Zero is not
allowed.\n\n");
else
{
quotient = first_num/second_num;
printf("Quotient = %lf \n\n", quotient);
}
}
break;
case 2:
{
int i, n, t1 = 0, t2 = 1, next_term;
printf("This is a program that print Fibonacci series.\n");
printf("Enter the number of terms:");
scanf("%d", &n);
printf("Fibonacci Series:");

for (i = 1; i <= n; i++)


{
printf("%d, ", t1);
next_term = t1 + t2;
t1 = t2;
t2 = next_term;
}
printf ("\n\n");
}
break;
case 3:
{
int i,fact = 1,n;
printf("This is a program that calculate the factorial of a
number.\n");
printf("Please enter an integer value for n:");
scanf("%d",&n);
for(i = 1; i <= n ; i++)
{
fact = fact * i;
}
printf("Factorial of %d is: %d\n\n", n, fact);

}
break;
default:
printf("Please re-enter your choice again.\n\n");
break;

}
printf("If you wish to perform again,\n");
printf("Press 1 if Yes\n");
printf("Press 2 if No\n");
printf("Answer:");
scanf("%d",&repick);
printf("\n");
}

while (repick == 1);

printf("End of Program.\n");
printf("Bye!");
return 0;
}

You might also like