Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

C Programming Tasks

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

Pakistan Institute of Engineering and

Applied Sciences

Computing Fundamentals & Programming


Fall 2024
Lab Exercise-04

Date: March 14, 2024


Instructions:

Any One Found

1. Using Mobile Without Permission Will Be Marked As Absent And 50% Marks Of This
Lab Will Be Deducted.

2. Copying Assignment / Using the internet During Lab tasks and Marking Proxy will Lead
you to an “F” Grade in the Lab. Be very careful.

3. Use an appropriate naming convention for variable name. e.g to calculate the sum of the
number variable name can be sum, sum_of_number. Random variable names are not
allowed.
4. Lab Tasks must be submitted in Microsoft Word format with a screen shot of output.

5. Submission Deadline of Lab Task is on the same day, during Lab, by 11:20 am
Topics Covered
1. For Loop

For Loop

A for loop is a repetition control structure that allows you to efficiently write a loop that needs to
execute a specific number of times.

Syntax:
for(initialization ; condition ; increment)
{
statement(s);
}
The initialization step is executed first, and only once. This step allows you to declare and
initialize any loop control variables. You are not required to put a statement here, as long as a
semicolon appears.
Next, the condition is evaluated. If it is true, the body of the loop is executed. If it is false, the
body of the loop does not execute and flow of control jumps to the next statement just after the
for loop.
After the body of the for loop executes, the flow of control jumps back up to the increment
statement. This statement allows you to update any loop control variables. This statement can be
left blank, as long as a semicolon appears after the condition.
The condition is now evaluated again. If it is true, the loop executes and the process repeats itself
(body of loop, then increment step, and then again condition). After the condition becomes false,
the for loop terminates.

Program-01
#include <stdio.h>
int main()
{
int num;
printf(“All numbers between 1 and 10 are \n”);
for(num = 2 ; num < 10; num++)
{
printf(“%d \n“,num);
}
getchar();
return 0;
}
Output:
When the program is executed the output will be as under.
All numbers between 1 and 10 are
2
3
4
5
6
7
8
9
Program--02
Printing even and odd series together, by using a for loop
#include <stdio.h>
int main()
{
printf(“Odd Even \n”);
for(int a=1,b=2;a<=10;a+=2,b+=2)
{
printf(“%d \t %d \n“,a,b);
}
getchar();
return 0;
}

Output:
Output of the program would be
Odd Even
1 2
3 4
4 6
7 8
9 10

Within-Lab Task 01
Write a C program to print odd numbers upto 10000.

Within-Lab Task 02

Create a C Program, to print a table of any number given by the user. The range will also be given by the
user.

Within-Lab Task 03

Use only Addition-Operator, find the square of the number given by the user.

Within-Lab Task 04

Write a complete and an efficient C program that takes an integer number from the user and calculates its
factorial.

Within-Lab Task 05

C Program to Display Fibonacci Sequence upto n terms.


Hint: The Fibonacci sequence is a sequence where the next term is the sum of the previous two terms.
The first two terms of the Fibonacci sequence are 0 followed by 1.

Sample Output:
Within-Lab Task 06

Write a C Program to Display Characters from A to Z Using for Loop.

Within-Lab Task 7
Write a program that calculates the power of a number without using exponentiation operation, only
using for loops.

Within-Lab Task 8

Create a C Program, in which take marks in 6 subjects, the total marks are 100 in each subject and then
tell the percentage of marks obtained.

Within-Lab Task 9

Write a program in C to display the square of the number up-to a given integer.

Sample Output:
Input number of terms : 5

Number is : 1 and Square of the 1 is :1


Number is : 2 and Square of the 2 is :4
Number is : 3 and Square of the 3 is :9

Number is : 4 and Square of the 4 is :16


Number is : 5 and Square of the 5 is :25

Within-Lab Task 10
Write a program in C to display the cube of the number up to an integer.
Test Data :
Input number of terms : 5
Expected Output :
Number is : 1 and cube of the 1 is :1
Number is : 2 and cube of the 2 is :8
Number is : 3 and cube of the 3 is :27
Number is : 4 and cube of the 4 is :64
Number is : 5 and cube of the 5 is :125

You might also like