0% found this document useful (0 votes)
63 views8 pages

Lab 8: For and While: CS-114 Fundamentals of Programming Loop

The document is a lab report for a programming fundamentals course. It includes 3 tasks that involve using for and while loops in C code. Task 1 demonstrates printing even numbers between 2 numbers and calculating their sum using a for loop. Task 2 calculates the sum of a Fibonacci series by adding the previous terms to generate the next term, using a for loop. Task 3 uses a while loop to determine if a user-input number has 3 digits, and prints whether the number is positive or not using if/else statements. The conclusion summarizes that the lab involved using while and for loops, and sometimes if/else statements, to complete tasks like printing even numbers, calculating Fibonacci series, and analyzing

Uploaded by

Awais jatoi
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)
63 views8 pages

Lab 8: For and While: CS-114 Fundamentals of Programming Loop

The document is a lab report for a programming fundamentals course. It includes 3 tasks that involve using for and while loops in C code. Task 1 demonstrates printing even numbers between 2 numbers and calculating their sum using a for loop. Task 2 calculates the sum of a Fibonacci series by adding the previous terms to generate the next term, using a for loop. Task 3 uses a while loop to determine if a user-input number has 3 digits, and prints whether the number is positive or not using if/else statements. The conclusion summarizes that the lab involved using while and for loops, and sometimes if/else statements, to complete tasks like printing even numbers, calculating Fibonacci series, and analyzing

Uploaded by

Awais jatoi
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
You are on page 1/ 8

Department of Electrical Engineering

Faculty Member: ___taha ali______________ Dated: __29/1/21__________________

Course/Section: BEE 13-D Semester: Fall 2021

CS-114 Fundamentals of Programming

Lab 8: For and While Loop

Name Reg. No Viva / Analysis


Quiz / Lab of data
Performa in Lab
nce Report

7 Marks 8 Marks

AWAIS AHMED 384685


Lab8: For and While Loop

Objectives
The main aim of performing this lab is to understand the concept of (for/while) loops in C.

Lab Instructions
 The students should perform and demonstrate each lab task separately for step-wise evaluation
 Each group shall submit one lab report on LMS within 6 days after lab is conducted. Lab report
submitted via email will not be graded.
 Students are however encouraged to practice on their own in spare time for enhancing their
skills.
 Complete as many problems as you can within the allotted time.
 Talk to your classmates for help
Lab Report Instructions
All questions should be answered precisely to get maximum credit. Lab report must ensure following
items:
 Lab objectives
 C codes with proper indentation
 Above each line of Code, use two slashes // to write a comment to yourself explaining
what the line does.
 Results (screen shots) duly commented and discussed
 Conclusion
Task 1
Code:
#define _CRT_SECURE_NO_WARNINGS //macro definition
#include<stdio.h> //standard I/O library

int main() //main function


{ //curly braces of main function
int i, n = 10, sum = 0, a; //declaring the variable
printf("Enter the number of terms:"); //prompt for entering the number of terms
scanf("%d", &a); //taking the number of terms as input from user
printf("The even numbers are:"); //prompt for the even numbers

for (i = 2; i <= n; i += 2) //starting of for loop


{
printf(" %d ", i); //printing the even numbers
sum += i; //calculating the sum of even numbers
}
printf("\nThe sum of even numbers upto 5 terms:%d",sum); //printing sum of even numbers
printf("\n"); //space sequence
return 0; //returning an integer
} //curly braces closed
Comments:
In this lab task we learn how to print the even numbers between two numbers and also printed
their sum upto 10 number of terms.

Task 2
Code:
#define _CRT_SECURE_NO_WARNINGS //macro definition
#include <stdio.h> //standard I/O library

int main() //main function


{ //curly braces
int i, n, sum = 1, first = 0, second = 1, third; //declaring and initializing some variables
printf("Enter the Fibonacci number of terms:"); //prompt for the terms of fibonacci series
scanf("%d",&n); //taking the number of terms as input from user

for (i = 2; i < n; i++) //starting of for loop


{
third = first + second; //calculating the sum of third term
sum = sum+third; //adding the sum in the third term
first = second; //here the first term will be assigned the value of second term
second = third; //here second term will be assigned the value of third term
}

printf("The sum of fibonacci series upto %d terms is:%d", n, sum);//print sum of fibonacci series
printf("\n"); //space sequence
return 0; //returning an integer
} //curly braces closed
Comments:
In this lab task we simply enter the number of terms and found the sum of Fibonacci series.
Adding the previous number of terms in the coming number of terms.

Task 3
Code:
#define _CRT_SECURE_NO_WARNINGS //macro definition
#include<stdio.h> //standard I/O library

int main() //main function


{ //curly braces
int n, i = 0; //declaring the variables
printf("Enter the number between 1-999:"); //prompt for entering the numbers between 1-999
scanf("%d", &n); //taking the number as input from user

if (n > 0) //if number is greater than zero


{
while (n != 0) //this while condition shows that number should be not zero
{
n = n / 10; //if while condition is true than number will be divided by ten repeatedly
i++; //I will be incremented again and again
}
if (i == 3) //if condition shows that if number has 3 digits
{
printf("The number has 3 digits.\n"); //prompt that number has 3 digits
printf("The number is positive."); //prompt that number is positive
}
else //if’s condition fails then this else condition will be executed
printf("The number has not 3 digits"); //prompt that number has not 3 digits
}

else //this else condition will be executed if first if condition fails


{
printf("The number is not positive"); //prompt that number is not positive
}
printf("\n"); //giving the space sequence
return 0; //returning an integer
} //curly braces closed
Comments:
In this lab task we simply asked the user to enter the number and we displayed on screen that
how many numbers the digits, if the number entered by the user is positive or not. If user
enters any number (negative or except 3) then the program will terminate.

Conclusion:
In the whole lab tasks we used the while and for loop and in some cases we needed the if-else
statement as well. We also learnt how to find the even number between two number and find
their sum and learnt to print the Fibonacci series as well.

You might also like