You are on page 1of 8

SUBMITTED TO:

Ma'am Amber Zeb

SUBMITTED BY:
Muhammad Saad

REGISTRATION NO.:
CIIT/FA20-BEE-137/ISB

SEMESTER-SECTION:
2-C

COURSE:
Computer
Programming CSC141

Lab Report: 6

BS Electrical Engineering
Lab#4
For and Nested For Loops
INLAB TASK
1. Analyze the following for loops in the table and state whether (a) they compile (b)
they run (c) They print out a line (d) they print out more than one line Answer each.
Compile Run Print out a line Print out more
Tasks (Yes/No) (Yes/No) (Yes/No) than one line
(Yes/No)
a) for (j = 1; j <=10; Yes Yes No No
j++);
b) for (j = 1; j < 11;++j); Yes Yes No No
c) for (j = 1; j <=10; j++) No No No No
d) for (j = 1; j <= 10;j++) No No No No
printf1 (“Hello\n”);
e) for (j = 1; j <= Yes Yes Yes No
10;j++); printf
(“Hello\n”);
f) for (j = 1, j <= 10, j++) No No No No
printf (“Hello\n”);

2. Write a program that input two integers x and y from the user. It display the table
of x upto y e.g. if the user input as x = 2 and y = 3 then the program displays the
following output
2x1=2
2x2=4
2x3=6

Program:
#include <stdio.h>
#include <stdlib.h>

int main()
{
int x,y,j;
printf("Enter Number: ");
scanf("%d",&x);
printf("Enter Range: ");
scanf("%d",&y);
for ( j=1;j<=y;j++)
{
printf("%d x %d = %d\n",x,j,x*j);
}
}

Output:

3. Write a program that determines the number entered by the user is even or odd.
After displaying the message (“Even” or “Odd”), it ask the user Do you want to
enter another number (y/n). If the user enter y, it do the same process again
otherwise exit. (Note: use do-while loop)

Program:
#include <stdio.h>
#include <stdlib.h>

int main()
{
int a;
char c;
do
{
printf("\nEnter Number: ");
scanf(" %d",&a);
if (a%2==0)
printf("%d is Even",a);
else
printf("%d is Odd",a);
printf("\n\nDo You Want To Enter Another Number[Y/N]:
");
scanf(" %c",&c);
}
while(c == 'y');
}
Output:

4. Write a program that input two integers x and y from the user. It calculates and
then displays the x raise to power y (xy).

Program:
#include <stdio.h>
#include <stdlib.h>

int main()
{
int a,b,j;
long long p=1;
printf("Enter Base: ");
scanf("%d",&a);
printf("Enter Exponent: ");
scanf("%d",&b);
for (j=1;j<=b;j++)
{
p = p*a;
}
printf("%d ^ %d = %d",a,b,p);
}

Output:
5. Write down the output of the following program
for (int a=1;j=1; j<=5;j++)
for (i=1; i<=5;i++)
{
printf(“%d\n”,a);
a++;
}

The above mentioned program is incorrect!

POSTLAB TASKS
1. Write a C program using for loop to find the sum of the integers 73
through 415 inclusive.
PROGRAM:
#include <stdio.h>
#include <stdlib.h>

int main()
{
int i,s=0;
for(i=73;i<=415;i++)
{
s = s+i;
}
printf("\nThe Sum of Integers From 73 to 415 Inclusive
is: %d ",s);
getch();
}
OUTPUT:

2. Write a program that input one integer n from the user. It prints all the even
number from 1 to n.
PROGRAM:
#include <stdio.h>
#include <stdlib.h>

int main()
{
int n,i;
printf("Even Numbers Till: ");
scanf("%d",&n);
for (i=1;i<=n;i++)
{
if (i%2==0)
{
printf("%d\n",i);
}
}
}
OUTPUT:
3. Write a program for a matchstick game being played between the computer and a
user. Your program should ensure that the computer always wins. Rules for the
game are as follows:
− There are 21 matchsticks.
− The computer asks the player to pick 1, 2, 3, or 4 matchsticks.
− Whoever is forced to pick up the last matchstick loses the game
− After the person picks, the computer does its picking.

PROGRAM:
#include <stdio.h>
#include <stdlib.h>

int main()
{
int a = 21, b, c;
while(a>=1)
{
printf("Total Match Sticks: %d\n", a);
printf("Pick Up The Match Sticks From (1 to 4): ");
scanf("%d", &b);

if(b>4)
{
printf("!Error");
break;
}

c = 5 - b;
printf("Computer Picks Up The %d Match Sticks.\n", c);
a = a - b - c;
if(a==1)
{
printf("\nThe Computer Has Won.");
break;
}
}
return(0);
}

OUTPUT:

You might also like