0% found this document useful (0 votes)
43 views15 pages

Lab Assignment 6

The document contains 6 questions related to C programming assignments on loops and functions. Q1 asks to print a pattern using nested while and do-while loops. Q2 finds all perfect numbers between 1-500 using nested loops. Q3 prints prime numbers between 2-500 using different loops. Q4 prints a multiplication table using nested loops. Q5 creates a basic calculator program using functions. Q6 computes the area and perimeter of shapes using nested functions and switch statements.

Uploaded by

Raveena Gupta
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)
43 views15 pages

Lab Assignment 6

The document contains 6 questions related to C programming assignments on loops and functions. Q1 asks to print a pattern using nested while and do-while loops. Q2 finds all perfect numbers between 1-500 using nested loops. Q3 prints prime numbers between 2-500 using different loops. Q4 prints a multiplication table using nested loops. Q5 creates a basic calculator program using functions. Q6 computes the area and perimeter of shapes using nested functions and switch statements.

Uploaded by

Raveena Gupta
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/ 15

LAB ASSIGNMENT 6

Q1. WAP to print 1 to 5 five times as follows using nested while and do-while loops.
12345
12345
12345
12345
12345
INPUT
A#include<stdio.h>
int main()
{
int i=0,j;
while(i<5)
{
j=1;
while(j<=5)
{
printf("%d\t",j);
j++;
}
printf("\n");
i++;
}
return 0;
}
OUTPUT
INPUT
B
#include<stdio.h>
int main()
{
int i=0,j;
do
{
j=1;
do
{
printf("%d\t",j);
j++;
}while(j<=5);
printf("\n");
i++;
}while(i<5);
return 0;
}
OUTPUT
Q2. WAP to find and print all perfect numbers between 1 and 500 using a nested loop.
[Hint: This program is an extension to Q1 of MST exam. A number is perfect if you can
sum up its divisors and arrive at that number. For example, 6 is a perfect number: 1 + 2
+ 3 = 6. The code starts with a number, and then looks for all the divisors. A number is
a divisor if the modulus division returns 0 (no remainder). Then it keeps a running total
of the divisors. If all divisors add up to the number, it is perfect.]
INPUT
#include<stdio.h>
int main()
{
int i,j,sum=0;
for(i=1;i<500;i++)
{
sum=0;
for(j=1;j<=i;j++)
{
if(i%j==0)
{
sum=sum+j;
}
}
if(sum/2==i)
{
printf("%d\n",i);
}
}
return 0;
}
OUTPUT
Q3. WAP to print all prime numbers between 2 and 500 using nested for, while and do-
while loops each separately.
INPUT
A
#include<stdio.h>
int main()
{
int i,j,k;
for(i=2;i<500;i++)
{
k=0;
for(j=2;j<i;j++)
{
if(i%j==0)
{
k++;
}
}
if(k==0)
{
printf("%d, ",i);
}
}
printf("\n");
return 0;
}
OUTPUT
INPUT
B
#include<stdio.h>
int main()
{
int i,j,k;
while(i<500)
{
k=0;
j=2;
while(j<i)
{
if(i%j==0)
{
k++;
}
j++;
}
if(k==0)
{
printf("%d, ",i);
}
i++;
}
printf("\b\b\n");
return 0;
}
OUTPUT

INPUT
C
#include<stdio.h>
int main()
{
int i,j,k;
do
{
k=0;
j=2;
do
{
if(i%j==0)
{
k++;
}
j++;
}while(j<i);
if(k==0)
{
printf("%d, ",i);
}
i++;
}while(i<500);
printf("\n");
return 0;
}
OUTPUT

Q4. WAP to print multiplication table as shown below for the numbers 1 to 5 using
nested while and do-while loops each separately.
1 2 3 4 5 6 7 8 9 10
2 4 6 8 10 12 14 16 18 20
3 6 9 12 15 18 21 24 27 30
4 8 12 16 20 24 28 32 36 40
5 10 15 20 25 30 35 40 45 50
INPUT
A
#include<stdio.h>
int main()
{
int i=1,j=1,pro=1;
while(i<=5)
{
j=1;
while(j<=10)
{
pro=j*i;
printf("%d\t",pro);
j++;
}
printf("\n");
i++;
}
return 0;
}
OUTPUT

INPUT
B
#include<stdio.h>
int main()
{
int i=1,j=1,pro=1;
do
{
j=1;
do
{
pro=j*i;
printf("%d\t",pro);
j++;
}while(j<=10);
printf("\n");
i++;
}while(i<=5);
return 0;
}
OUTPUT

Q5. Write a menu driven program using function to design a simple calculator as
follows:
1. Addition
2. Subtraction
3. Multiplication
4. Division
INPUT
#include<stdio.h>
float sum(float,float);
float diff(float,float);
float pro(float,float);
float div(float,float);
int main()
{
int n;
float x,num1,num2;
printf("Enter your choice:\n");
printf("1-Addition\n2-subtraction\n3-Product\n4-Division\n");
scanf("%d",&n);
printf("Enter Two numbers\n");
scanf("%f%f",&num1,&num2);
switch(n)
{
case 1:
x=sum(num1,num2);
printf("Sum=%0.2f",x);
break;
case 2:
x=diff(num1,num2);
printf("difference=%0.2f",x);
break;
case 3:
x=pro(num1,num2);
printf("Product=%0.2f",x);
break;
case 4:
x=div(num1,num2);
if(num2==0)
{
}
else
{
printf("Divident=%0.2f",x);
}
break;
default:
printf("Wrong choice has been selected\nExiting....");
break;
}
printf("\n");
return 0;
}
float sum(float num1,float num2)
{
float y;
y=num1+num2;
return y;
}
float diff(float num1,float num2)
{
float y;
y=num1-num2;
return y;
}
float pro(float num1,float num2)
{
float y;
y=num1*num2;
return y;
}
float div(float num1,float num2)
{
float y;
if(num2==0)
{
printf("undefined\n");
}
else
{
y=num1+num2;
return y;
}
}
OUTPUT
Q6. Write a menu driven program using function to compute the area and perimeter of
different geometrical shapes as describe below.
[The main() function called two sub-functions named as 1. void area() and 2. void
perimeter() depending on user choice (use switch statement). Then for each sub-
function, compute the required things for different geometrical shapes such as 1.
Square, 2. Rectangle, 3. Circle depending on the user choice (use switch statement).]
INPUT
#include<stdio.h>
#include<math.h>
void area();
void perimeter();
int main()
{
int n;
printf("Enter a valid choice:\n");
printf("1-Area\n2-perimeter\n");
scanf("%d",&n);
switch(n)
{
case 1:
area();
break;
case 2:
perimeter();
break;
default:
printf("Wrong choice\nExiting....");
break;
}
printf("\n");
return 0;
}
void area()
{
int m;
float x,y;
printf("Select a figure:\n1-Square\n2-Rectangle\n3-Circle\n");
scanf("%d",&m);
switch(m)
{
case 1:
printf("Enter the side of square\n");
scanf("%d",&x);
printf("Area=%f",x*x);
break;
case 2:
printf("Enter the length and breadth of rectangle\n");
scanf("%f%f",&x,&y);
printf("Area=%f",x*y);
break;
case 3:
printf("Enter the radius of the circle\n");
scanf("%f",&x);
printf("Area=%f",3.14*pow(x,2));
break;
default:
printf("Wrong choice\nExiting....");
break;
}
}
void perimeter()
{
int m;
float x,y;
printf("Select a figure:\n1-Square\n2-Rectangle\n3-Circle\n");
scanf("%d",&m);
switch(m)
{
case 1:
printf("Enter the side of square\n");
scanf("%f",&x);
printf("Perimeter=%f",4*x);
break;
case 2:
printf("Enter the length and breadth of rectangle\n");
scanf("%f%f",&x,&y);
printf("Perimeter=%f",2*(x+y));
break;
case 3:
printf("Enter the radius of the circle\n");
scanf("%f",&x);
printf("Perimeter=%f",3.14*x*2);
break;
default:
printf("Wrong choice\nExiting....");
break;
}
}
OUTPUT

You might also like