0% found this document useful (0 votes)
4 views5 pages

C Programs for Basic Algorithms

The document contains multiple C programs: one to find the nth term of the Fibonacci series, another to find the largest element in an array of 10 integers, and a third to determine the middle number among three integers. Additionally, it includes a project on GPA grading using a structure to store student information and calculate GPA based on marks. Each program is well-structured with input prompts and output statements.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views5 pages

C Programs for Basic Algorithms

The document contains multiple C programs: one to find the nth term of the Fibonacci series, another to find the largest element in an array of 10 integers, and a third to determine the middle number among three integers. Additionally, it includes a project on GPA grading using a structure to store student information and calculate GPA based on marks. Each program is well-structured with input prompts and output statements.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Q1. Write a C program to find nth term Fibonacci Series?

#include<stdio.h>
int fibonacciseries(int n)
{
int a=0,b=1,temp;
if(n==0) return a;
if(n==1) return b;
for(int i=2;i<=n;i++)
{
temp=a+b;
a=b;
b=temp;
}
return b;
}
int main (){
int n;
printf("Enter the position of fiboncchi term :");
scanf("%d",&n);
printf("The %d fibonacci term is %d\n",n,fibonacciseries(n));
return 0;
}

Q2. Write a C program to find the largest element in an array with 10


integers?
#include<stdio.h>
int main ()
{
int arr[10],i,max;
printf("Enter 10 integers: \n");
for(i=0;i<10;i++){
scanf("%d",&arr[i]);
}
max=arr[0];
for(i=1;i<10;i++)
{
if(arr[i]>max){
max=arr[i];
}
}
printf("The largest number of this arrey is: %d\n", max);
return 0;
}

Q3. Find the middle number of any three number?


#include<stdio.h>
int main ()
{
int num1,num2,num3,middlenum;
printf("Enter three integer number :\n");
scanf("%d%d%d",&num1,&num2,&num3);
if((num1>=num2&&num1<=num3)||(num1<=num2&&num1>=num3)){
middlenum=num1;}
else if ((num2>=num1&&num2<=num3)||(num2<=num1&&num2>=num3)){
middlenum=num2;}
else{
middlenum=num3;}
printf("The middle number is : %d\n",middlenum);
return 0;

Name: Sakibul Islam


Roll: B230305014
Sub: SPL

Project: Solving Structure Program using C


Title: GPA Grading

#include <stdio.h>
struct student {
char name[10];
int roll;
float marks[10];
};
int main() {
struct student s[10];
int n, k;
printf("\nEnter the students number: ");
scanf("%d", &n);
printf("\nEnter students subject number: ");
scanf("%d", &k);
for (int i = 0; i < n; i++) {
float sum = 0;
printf("\nEnter student name: ");
scanf("%s", s[i].name);
printf("\nEnter student roll: ");
scanf("%d", &s[i].roll);
int failed = 0;
for (int j = 0; j < k; j++) {
printf("\nEnter marks for subject %d: ", j + 1);
scanf("%f", &s[i].marks[j]);
int division = s[i].marks[j];
if (division < 40) {
failed = 1;
} else if (division < 45) { sum += 2; }
else if (division < 50) { sum += 2.25; }
else if (division < 55) { sum += 2.50; }
else if (division < 60) { sum += 2.75; }
else if (division < 65) { sum += 3.00; }
else if (division < 70) { sum += 3.25; }
else if (division < 75) { sum += 3.50; }
else if (division < 80) { sum += 3.75; }
else { sum += 4.00; }
}
if (failed) {
printf("Result: Fail\n");
} else {
printf("GPA: %.2f\n", sum / k);
}
}
return 0;
}

Common questions

Powered by AI

Challenges in determining the middle value using conditional statements include ensuring accurate comparisons regardless of number ordering and handling equal values. These are resolved by constructing logical conditions that cover all possible orderings and identifying the middle number through transitive comparisons. Implementing a rigorous condition like `(num1 >= num2 && num1 <= num3) || (num1 <= num2 && num1 >= num3)` helps prevent misidentification of the middle number in skewed ordering or when numbers are equal. Strategically structuring these conditions ensures consistency and correctness in diverse scenarios .

Conditional logic in C is applied using `if-else` statements to manage different grading scenarios based on specific ranges of student marks. For each subject's mark, the program assigns grade points by checking conditions sequentially. If a mark is less than 40, the student fails; otherwise, points correspond to given ranges (e.g., 40-44 earns 2 points, 45-49 earns 2.25, etc.). This approach ensures each mark is accurately assessed and aggregated towards the student's overall GPA, demonstrating an organized use of conditional logic to handle multiple criteria .

The logic to find the middle value among three numbers in C involves using conditional statements to check the range in which a number falls relative to the other two numbers. For each number, the program checks whether it is greater than or equal to one number and less than or equal to the other. If this condition is true, that number is the middle value. Specifically, for `num1`, the condition is `(num1 >= num2 && num1 <= num3) || (num1 <= num2 && num1 >= num3)`. Similarly, conditions are checked for `num2` and `num3`. This method effectively isolates the middle number based on logical comparison .

The GPA computation program robustly handles inputs for multiple students and subjects by structuring its logic to loop through student entries and dynamically calculate GPA for varying numbers of subjects. It prompts for student count, subject count, and iteratively requests marks for each subject. Each mark is processed using conditional grading logic, and failures are detected early. The program outputs precise results for each student with either a GPA or a failure message. This robustness stems from systematic input handling and checks that ensure the program scales well with more data variability .

In C, the nth Fibonacci number can be calculated using an iterative approach. The function `fibonacciseries` starts with two initial terms of the Fibonacci series, 0 and 1. For each subsequent term, it iterates from 2 to n, updating the terms by setting `temp` as the sum of the last two terms. It then updates the variables `a` and `b` to the last two terms respectively. This loop continues until the nth term is reached, and the value is returned as the nth Fibonacci number. This logic leverages the property of the Fibonacci sequence where each term is the sum of the two preceding terms .

Primary and aggregate assignments in loops facilitate iterative calculations and condition checks efficiently. Primary assignments initialize variables such as the starting Fibonacci numbers or initial array elements like `max`. Aggregate assignments, like `temp = a + b` in the Fibonacci series or `sum += points` in GPA calculation, update these variables cumulatively, ensuring that partial results are systematically built upon with each iteration cycle. This combined approach simplifies complex data processing tasks by breaking them down into manageable iterative steps, ensuring programs remain concise and logical .

To find the largest element in an array of 10 integers, a C program typically initializes a variable `max` with the first element of the array. It then iterates through the array, comparing each element with `max`. If a given element is greater than `max`, the program updates `max` with that element. This process ensures that by the end of the iteration, `max` holds the largest value in the array .

The C program calculates the GPA by summing grade points based on marks in each subject and averaging them across all subjects. Each mark is categorized into a grade with specific points using a conditional structure—e.g., 40-44 gets 2 points, 45-49 gets 2.25 points, and so on up to more than 80, which gets 4 points. If any subject mark is below 40, the student is marked as 'Fail' and no GPA is calculated. This condition checks if a student fails any subject before calculating the average GPA. The sum of all points is divided by the number of subjects to get the final GPA for the student .

Iteration constructs like `for` loops are employed in the C programs to efficiently accumulate data by repeatedly executing a block of code. In calculating the Fibonacci series, a `for` loop iteratively updates Fibonacci terms until the required nth term is computed. For finding the largest array element or calculating GPA, `for` loops iterate over integer arrays, comparing values to update a maximum variable or summing values to compute averages. This repetitive structure allows efficient processing of data collections by performing consistent operations on each element .

The programming structure used is a `struct` declaration which defines a `student` data type containing fields for storing each student's name, roll number, and an array of marks. The `struct` is then used to declare an array of students. This array allows traversing each student's data for input and subsequent manipulation, such as calculating GPA or checking for failures in subjects. This method provides a way to group related data in C, allowing operations to be performed on structured data more conveniently .

You might also like