Professional Documents
Culture Documents
Assignment 6
12345
12345
12345
12345
12345
Code:
FOR LOOP: WHILE LOOP: DO WHILE:
#include<stdio.h> #include<stdio.h> #include<stdio.h>
{ { {
while(i<=5) while(i<=5) do
{ { {
while(j<=5) while(j<=5) do
{ { {
} } i++;
return 0; return 0; }
} while(i<=5);
} return 0;
}
Question 2: WAP to find and print all perfect numbers
between 1 and 500 using a nested loop.
Code:
#include<stdio.h>
int main()
for(int i=1;i<=500;i++)
int sum=0;
for(int j=1;j<i;j++)
if(i%j==0)
sum=sum+j;
if (sum==i)
printf("%d\n",i);
return 0;
}
Question 3: WAP to print all prime numbers between 2 and
500 using nested for, while and do-while loops each
separately.
Code:
FOR LOOP: WHILE LOOP: DO WHILE:
#include<stdio.h> #include<stdio.h> #include<stdio.h>
{ { {
{ while(i<=500) do
int p=0; { {
if(i%j==0) while(j<i) do
p=1; { {
} if(i%j==0) if(i%j==0)
} } }
} i++;
return 0; }while(i<=500);
} return 0;
}
Question 4: 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
Code:
#include<stdio.h>
int main()
for(int i=1;i<=5;i++)
printf("%d\t",i);
for(int j=2;j<=10;j++)
printf("%d ",i*j);
}
printf("\n");
return 0;
printf("\nADDITION:%0.2lf",a+b);
}
void sub(float a,float b)
printf("\nSUBTRACTION=%0.2lf", a-b);
printf("\nMULTIPLICATION:%0.2lf", a*b);
printf("\nDIVISION:%0.2lf",a/b);
int main()
int i;
float a,b;
printf("WELCOME TO THE
CALCULATOR\n1:ADDITION\n2:SUBTRACTION\n3:MULTIPLICATION\
n4:DIVISION\n");
scanf("%d",&i);
switch(i)
case 1: add(a,b);
break;
case 2: sub(a,b);
break;
case 3: multi(a,b);
break;
case 4: divide(a,b);
break;
return 0;
}
Question 6: Write a menu driven program using function to
compute the area and perimeter of different geometrical
shapes as describe below.
Code:
#include<stdio.h>
if(x=='A')
printf("%0.2f",j*j);
else if(x=='P')
printf("%0.2f",4*j);
if(x=='A')
printf("%0.2f",(m*n));
else if (x=='P')
printf("%0.2f",(2*(m+n)));
{
if(x=='A')
printf("%f",3.14*r*r);
else if(x=='P')
printf("%f",2*3.14*r);
void area()
int a;
scanf("%d",&a);
char x='A';
switch(a)
float j;
scanf("%f",&j);
Square(j,x);
break;
float m,n;
scanf("%f%f",&m,&n);
Rectangle(m,n,x);
break;
float r;
scanf("%f",&r);
Circle(r,x);
break;
default : break;
void parameter()
int a;
scanf("%d",&a);
char x='P';
switch(a)
float j;
scanf("%f",&j);
Square(j,x);
break;
float m,n;
scanf("%f%f",&m,&n);
Rectangle(m,n,x);
break;
float r;
scanf("%f",&r);
Circle(r,x);
break;
default : break;
int main()
int i;
scanf("%d",&i);
switch(i)
case 1: area();
break;
case 2: parameter();
break;
return 0;