Digital Learning

Tuesday, April 14, 2020

'C' Programs Using While Loop & Array:

C-Programs:
1.Write a C program to find maximum and minimum value using while loop.
2.Write a C program to perform mathematical operations using while loop and switch case.
3.Write a C program to enter values in an array and print the same values.
4.Write a C program to enter values in an array and find even numbers.
5.Write a C program to enter values in an array and find out odd numbers.
6.Write a C program to find out maximum and minimum value using for loop.
7.Write a C program to find a value in an array.
8.Write a C program to find the product of ten given numbers.

Write a C program to find maximum and minimum value using while loop.
#include<stdio.h>
#include<conio.h>
void main()
{
int n, max, min;
clrscr();
printf("\n Enter number");
scanf("%d", &n);
min=n;
max=n;
while(n!=99)
{
printf("\n Enter no");
scanf("%d",&n);
if(min>n)
min=n;
if(max<n)
max=n;
}
printf("\n Max=%d", max);
printf("\n Min=%d", min);
getch();
}
Output:
Enter number 10
Enter no 15
Max=15
Min=10
  
Write a C program to perform mathematical operations using while loop and switch case.
#include<stdio.h>
#include<conio.h>
void main()
{
int n1, n2, n3;
char op;
clrscr();
printf("\n Enter n1, n2");
scanf("%d%d", &n1,&n2);
while(op!='?')
{
printf("\n Enter operator");
op=getche();
switch(op)
{
case '+':
n3=n1+n2;
break;
case '-':
n3=n1-n2;
break;
case '/':
n3=n1/n2;
break;
case '*':
n3=n1*n2;
break;
}
printf("\n Result N3=%d",n3);
}
getch();
}
Output:
Enter n1, n2 10
15
Enter operator +
Result N3=25

Write a C program to enter values in an array and print the same values.
#include<stdio.h>
#include<conio.h>
void main()
{
int value[3],;
clrscr();
for(i=0; i<3; i++)
{
printf("\n Enter value");
scanf("%d", &value[i]);
}
for(i=0; i<3; i++)
{
printf("\n Result=%d", value[i]);
}
getch();
}
Output:
Enter value 1
2
3
Result=1
2
3

Write a C program to enter values in an array and find even numbers.
#include<stdio.h>
#include<conio.h>
void main()
{
int value[10]. i;
clrscr();
for(i=0; i<=9; i++)
{
printf("\n Enter value");
scanf("%d", &value[i]);
}
for(i=0; i<=9; i++)
{
if(value[i]%2==0)
printf("\n Even=%d", value[i]);
}
getch();
}
Output:
Enter value 1 2 3 4 5 6 7 8 9 10
Even=2
4
6
8
10

Write a C program to enter values in an array and find out odd numbers.
#include<stdio.h>
#include<conio.h>
void main()
{
int i, c=0, value[10];
clrscr();
for(i=0; i<=9; i++)
{
printf("\n enter values");
scanf("\t %d", &value[i]);
}
for(i=0; i<=9; i++)
{
if(value[i]%2!=0)
c=c+1;
}
printf("Odd number=\t %d", c);
getch();
}
Output:
enter values 1 2 3 4 5 6 7 8 9 10
Odd number= 1 3 5 7 9

Write a C program to find out maximum and minimum value using for loop.
#include<stdio.h>
#include<conio.h>
void main()
{
int i, max, min, value[10];
clrscr();
printf("\n Enter value");
for(i=0; i<=9; i++)
{
scanf("\t %d", &value[i]);
}
for(i=0; i<=9; i++)
{
max=min=value[0];
}
for(i=0; i<=9; i++)
{
if(max<value[i])
max=value[i];
if(min>value[i])
min=value[i];
}
printf("\n Maximum=%d", max);
printf("\n Minimum=%d", min);
getch();
}
Output:
Enter value 1
2
3
4
5
6
7
8
9
10
Maximum=10
Minimum=1
   
Write a C program to find a value in an array.
#include<stdio.h>
#include<conio.h>
void main()
{
int i, n, value[10];
clrscr();
for(i=0; i<=9; i++)
{
printf("\n Enter value");
scanf("%d", &value[i]);
}
printf("\n Enter No. to be search");
scanf("%d", &n);
for(i=0; i<=9; i++)
{
if(value[i]==n)
break;
}
if(i==10)
printf(" not found");
else
printf("found");
getch();
}

Output:
Enter value 5
2
3
6
4
1
7
8
9
10
Enter No. to be search 5
found

Write a C program to find the product of ten given numbers.
#include<stdio.h>
#include<conio.h>
void main()
{
int i, j, value[10], a=1;
clrscr();
for(i=0; i<=9; i++)
{
printf("\n Enter value");
scanf("%d", value[i]);
}
for(i=0; i<=9; i++)
{
a=1;
for(j=1; j<=value[i]; j++)
{
a=a*j;
}
printf("%d\n ", a);
}
getch();
}

No comments:

Post a Comment