You are on page 1of 7

Lab 6

1. Write a program to prompt 5 integers from the operator. Find the largest of those 5
integers and display it to the screen.

Sample input-output
Number 1: 8
Number 2: 12
Number 3: 5
Number 4: 22
Number 5: 17
The largest is: 22

Code:
#include <stdio.h>

int main()
{
char number[5];
int i, max;

//taking input and sort it in an array


for (i=0; i<5; i++)
{
printf("number %d:", i+1);
scanf("%d", &number[i]);
}

max=number[0];
for (i=1; i<5; i++)
{
if (number[i]>max)
{
max=number[i];
}
}
printf("The largest is %d", max);
return 0;
}

Result:
2. Write a C program to receive a series of integers from the operator. Calculates the
product of those integers and print the total to the screen when negative value is enter.

Sample input-output
Please enter an integer: 2
Please enter an integer: 5
Please enter an integer: 4
Please enter an integer: -1
Total (product):40

Code:
#include <stdio.h>

int main()
{
int num, product=1;

while(num != -1)
{
printf("Please enter an integer: ");
scanf("%d", &num);
if (num!= -1)
{
product= product*num;
}
else
{
break;
}
}

printf("Total (Product): %d", product);


return 0;
}
Result:
3. Using a switch statement, write a program to prompt user to choose their favourite
programming language. Choose 1 for C, 2 for Java, and 3 for VB.Net. Display the name
of the programming language to the screen when they make the selection.

Sample input-output.

Menu
1C
2 Java
3 VB.Net
Choice: 1
My favourite programming language is: C

Choice: 4
I do not have any favourite programming language.

Code:
#include <stdio.h>

int main()
{
int selection;
printf("Menu\n");
printf("1. C \n2. Java \n3.VB.Net");
printf("\nChoice: ");
scanf("%d", &selection);

switch(selection)
{
case 1:
{
printf("My favorite programming language is: C");
break;
}

case 2:
{
printf("My favorite programming language is: Java");
break;
}

case 3:
{
printf("My favorite programming language is: VB.Net");
break;
}

default:
{
printf("I don't have any favorite programming language.");
break;
}
}
return 0;
}

Results:

You might also like