0% found this document useful (0 votes)
35 views

C Programming Assignment 6: Department of Computer Science and Engineering

This document contains the solutions to 6 questions on C programming assignments submitted by a student. The questions cover topics like printing patterns using loops, finding perfect numbers, prime numbers, multiplication tables and a menu driven calculator and geometry shape area and perimeter program using functions. Code snippets implementing the solutions using various loops and functions are provided for each question.

Uploaded by

chiragmanchanda
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)
35 views

C Programming Assignment 6: Department of Computer Science and Engineering

This document contains the solutions to 6 questions on C programming assignments submitted by a student. The questions cover topics like printing patterns using loops, finding perfect numbers, prime numbers, multiplication tables and a menu driven calculator and geometry shape area and perimeter program using functions. Code snippets implementing the solutions using various loops and functions are provided for each question.

Uploaded by

chiragmanchanda
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

C programming

Assignment 6

Department of Computer Science and engineering

Submitted by: Submitted To:


Name:Chirag Manchanda MR. Hemant Petwal
Roll No: 101903257
Group: H6
Question 1: WAP to print 1 to 5 five times as follows using nested
while and do-while loops.

12345

12345

12345

12345

12345

Code:
FOR LOOP: WHILE LOOP: DO WHILE:
#include<stdio.h> #include<stdio.h> #include<stdio.h>

int main() int main() int main()

{ { {

int i; int i=1; int i=1;

while(i<=5) while(i<=5) do

{ { {

int j=1; int j=1; int j=1;

while(j<=5) while(j<=5) do

{ { {

printf("%d ",j); printf("%d ",j); printf("%d ",j);

j++; j++; j++;


} } }

printf("\n"); printf("\n"); while(j<=5);

i++; i++; printf("\n");

} } 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>

int main() int main() int main()

{ { {

for(int i=2;i<=500;i++) int i=2; int i=2;

{ while(i<=500) do

int p=0; { {

for(int j=2; j<i;j++) int p=0; int p=0;

{ int j=2; int j=2;

if(i%j==0) while(j<i) do

p=1; { {

} if(i%j==0) if(i%j==0)

if(p==0) p=1; p=1;


printf("%d\n",i); j++; j++;

} } }

return 0; if(p==0) while(j<i);

} printf("%d ",i); if(p==0)

i++; printf("%d ",i);

} 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;

Question 5: Write a menu driven program using function to


design a simple calculator as follows:
1. Addition
2. Subtraction
3. Multiplication
4. Division
Code:
#include<stdio.h>

void add (float a,float b)

printf("\nADDITION:%0.2lf",a+b);

}
void sub(float a,float b)

printf("\nSUBTRACTION=%0.2lf", a-b);

void multi(float a,float b)

printf("\nMULTIPLICATION:%0.2lf", a*b);

void divide(float a, float 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);

printf("\nENTER TWO NUMBERS: ");


scanf("%f%f",&a,&b);

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;

default: printf("wrong choice");

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>

void Square(float j,char x)

if(x=='A')

printf("%0.2f",j*j);

else if(x=='P')

printf("%0.2f",4*j);

void Rectangle(float m,float n,char x)

if(x=='A')

printf("%0.2f",(m*n));

else if (x=='P')

printf("%0.2f",(2*(m+n)));

void Circle(float r,char x)

{
if(x=='A')

printf("%f",3.14*r*r);

else if(x=='P')

printf("%f",2*3.14*r);

void area()

printf("AREA OF?\n 1:Square\n 2:Rectangle\n 3:Circle\n");

int a;

scanf("%d",&a);

char x='A';

switch(a)

case 1: printf("Enter side of square");

float j;

scanf("%f",&j);

Square(j,x);

break;

case 2: printf("Enter length and breadth of rectangle");

float m,n;

scanf("%f%f",&m,&n);
Rectangle(m,n,x);

break;

case 3: printf("Enter the radius of circle");

float r;

scanf("%f",&r);

Circle(r,x);

break;

default : break;

void parameter()

printf("PARAMETER OF?\n 1:Square\n 2:Rectangle\n 3:Circle\n");

int a;

scanf("%d",&a);

char x='P';

switch(a)

case 1: printf("Enter side of square");

float j;

scanf("%f",&j);
Square(j,x);

break;

case 2: printf("Enter length and breadth of rectangle");

float m,n;

scanf("%f%f",&m,&n);

Rectangle(m,n,x);

break;

case 3: printf("Enter the radius of circle");

float r;

scanf("%f",&r);

Circle(r,x);

break;

default : break;

int main()

int i;

printf("WELCOME\n 1:AREA\n 2:PARAMETER");

scanf("%d",&i);
switch(i)

case 1: area();

break;

case 2: parameter();

break;

default: printf("\n WRONG CHOICE");

return 0;

You might also like