Assignment #3 C program
Assignment #3
Q1: What is Iteration in C Language?
Answer:
Iteration in C language refers to the process of executing a set of statements repeatedly until a
specified condition is met. It helps in reducing code redundancy by using loops. There are three
types of iteration structures in C:
1. For Loop
2. While Loop
3. Do-While Loop
These loops allow efficient control of repetitive tasks in programming.
---
Q2: What is a For Loop in C Language?
Answer:
A for loop in C is a control structure that allows repeated execution of a block of code a fixed
number of times. It consists of three parts:
for(initialization; condition; increment/decrement) {
// Code to execute
}
Example:
#include <stdio.h>
int main() {
int i;
for(i = 1; i <= 5; i++) {
printf("Iteration %d\n", i);
}
return 0;
}
This loop prints numbers from 1 to 5, increasing i by 1 in each iteration.
---
Q3: C Program for Data Analysis in International Relations
Task: Calculate the Average Trade Value from User Input
A simple C Program:
#include <stdio.h>
int main() {
int n, i;
float tradeSum = 0, tradeAvg;
// Asking user for the number of trade entries
printf("Enter the number of trade records: ");
scanf("%d", &n);
float tradeValues[n];
// Using a for loop to take trade values as input
for(i = 0; i < n; i++) {
printf("Enter trade value for record %d (in billion dollars): ", i + 1);
scanf("%f", &tradeValues[i]);
tradeSum += tradeValues[i]; // Adding trade value to sum
}
// Calculating the average trade value
tradeAvg = tradeSum / n;
// Displaying the results
printf("\nTrade Data Analysis Report:\n");
printf("Total Trade Value: %.2f billion dollars\n", tradeSum);
printf("Average Trade Value: %.2f billion dollars\n", tradeAvg);
return 0;
}
Thank you, for reading.