Department of Electrical Engineering
Department of Electrical Engineering
Engineering
Faculty of Engineering & Applied Sciences
Riphah International University, Islamabad, Pakistan
In today’s lab learn about the implementation of Control Structure (for, nested for) by
executing C programs. And I learn about the nested loop and for nested loop in C and perform some
tasks with for loop and nested loop.
Lab Task
Task 1
Basic Concept of programing
Part 1:
Conversion of temperature from kelvin to
frenhiet and celcius
Code
#include <stdio.h>
#include <stdlib.h>
int main()
{float k;
float fh;
float c;
c=k+273.15;
printf("temperature in celcius =%.2f c\n",c);
fh=(k*1.8)-459.67;
printf("temperature in fahrenhiet =%.2f f\n",fh);
return 0;
output
Task 2
Nested if else condition
Part 1:
Write a program in which user input any number and find LCM using nested if else
condition.
Code:
#include<stdio.h>
int main() {
int a;
int b;
int max;
int step;
int lcm=0;
printf("enter first number \n");
scanf("%d",&a);
printf("enter second number \n");
scanf("%d",&b);
if(a > b)
max = step = a;
else
max = step = b;
while(1) {
if(max%a == 0 && max%b == 0) {
lcm = max;
break;
}
Output
Task 3: Basic
While loop
Write a program in which user put any value on output window, and program
give the Binary no in to decimal using while loop.
Code:
#include <stdlib.h>
#include <stdio.h>
int main() {
long n;
printf("Enter a binary number: ");
scanf("%ld", &n);
int dec = 0, i = 0, rem;
while (n != 0) {
rem = n % 10;
n /= 10;
dec += rem * pow(2, i);
i++;}
printf("Number in decimal: %d", dec);
return 0;
}
Output
Task 4
Nested for loop
#include <stdio.h>
int main()
{
int row, c;
for (row = 1; row <= 10; row++)
{
for (c = 1; c <= 10-row; c++)
printf(" ");
printf("\n");
}
return 0;
}
Output
Conclusion:
In this lab I have learned about implement for loop and nested for loop in C programming and
perform some tasks. Similarly we have performed this tasks by using these terms to solve daily life
problems. Also we understand and mentioned the advantages of using loops in C. In this lab i have
also use do- while loop and while loop .