0% found this document useful (0 votes)
6 views

Assignment #3 C Program

The document provides an overview of iteration in C language, explaining its purpose and types, including for loops, while loops, and do-while loops. It details the structure and functionality of a for loop with an example program that prints numbers from 1 to 5. Additionally, it presents a C program for calculating the average trade value based on user input, demonstrating the use of loops and arrays.

Uploaded by

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

Assignment #3 C Program

The document provides an overview of iteration in C language, explaining its purpose and types, including for loops, while loops, and do-while loops. It details the structure and functionality of a for loop with an example program that prints numbers from 1 to 5. Additionally, it presents a C program for calculating the average trade value based on user input, demonstrating the use of loops and arrays.

Uploaded by

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

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.

You might also like