Sie sind auf Seite 1von 3

In Lab

Task No. 01

Make a new C project (console application) using CodeBlocks and type the following code in the
main.c file. Build and run the code and fix the indicated errors.

Program No. 01
#include <stdio.h>
#include <stdlib.h>

int main()
{
int N = 0; // Take a number N.
printf("Enter a number for which you want to find the factorial: \n");
Scanf("%d", &N); // Get input from the user
printf("\nYou entered: %d\n\n", n); // Display what the user entered.

int R = 0; // Take a variable R to hold result


int x = 0; // And another x to count
x = N-1; // Let x equal to N-1
R = N // let R = N
do
{

R = R * x; // multiply R with x and store result in R.

X = X-1; // subtract 1 from x

}while(x>=1); // repeat above steps from multiplication till x is


greater than or equal to 1

Printf(‘The factorial of %d is %d\n\n’ N, R); // Output R to console


return 0;
}
𝑛2 − 5𝑛, 𝑛 ≤ −10
𝑛 + 19, −10 < 𝑛 < −2
3
𝑓[𝑛] = 𝑛 − 50𝑛, Equation.01
𝑛 ≥ −2

{ 𝑛 𝑖𝑠 𝑎𝑛 𝑖𝑛𝑡𝑒𝑔𝑒𝑟

Task No. 02

Fill the table using Equation 01 for values of t from -20 to 20. You will be using this table to compare
the output of the program in the next task and fixing some logical errors.

n f[n] n f[n]
Task No. 03

Type and build the following code in a new CodeBlocks project. Compare the output of the program
with the table in task 1. Find any logical error and write the correct program.
#include <stdio.h>
#include <stdlib.h>

int main(void)
{
int range_min = -20;
int range_max = 20;

int n;
int output;

printf("For the range %d to %d, ", range_min, range_max);


printf("The output of the function is: \n");

for(n=range_min; n<range_max; n++)


{
if(n < -10)
{
output = (n^2) - (5*n);
}
else if((n<-10)&&(n<=-2))
{
output = n + 19;
}
else
{
output = (n^3) - (50*n);
}
printf("%d ", output);

}
printf("\n\n");
return 0;
}

Post Lab:

Modify the above C program for the following piecewise function and report the problems you face.
Use integer variables for your program.

−𝑛 − 4, 𝑛<3
𝑛2 − 7, 3 ≤ 𝑛 ≤ 10
120
𝑓[𝑛] = 𝑛
+ 𝑛, 𝑛 > 10

{𝑤ℎ𝑒𝑟𝑒 𝑛 𝑖𝑠 𝑎𝑛 𝑖𝑛𝑡𝑒𝑔𝑒𝑟 𝑣𝑎𝑟𝑖𝑎𝑏𝑙𝑒

Das könnte Ihnen auch gefallen