LOOP STATEMENTS
1. This program generate a series from 1 to 10 using while loop#include<conio.h>
#include<stdio.h>
void main()
{
int i=1;
while(i<=10)
{
printf("\n%d",i);
i++;
}
getch();
}
/*The output of the above program is
**
1
2
3
4
5
6
7
8
9
10
*/
2. This program generate a series from 1 to 10 using do..while loop
#include<conio.h>
#include<stdio.h>
void main()
{
int i=1;
do
{
printf("\n%d",i);
i++;
}while(i<=10);
getch();
}
/*The output of the above program is
**
1
2
3
4
5
6
7
8
9
10
*/
3.This program generate a series from 1 to 10 using for loop
#include<conio.h>
#include<stdio.h>
void main()
{
int i;
for(i=1;i<=10;i++)
{
printf("\n%d",i);
}
getch();
}
/*The output of the above program is
**
1
2
3
4
5
6
7
8
9
10
*/
4.This program generate a table of a given input number
#include<conio.h>
#include<stdio.h>
void main()
{
int num,result,i=1;
printf("\nEnter a number to generate the table : ");
scanf("%d",&num);
printf("\nThe table of %d is given below",num);
while(i<=10)
{
result=num*i;
printf("\n\t %d * %d = %d",num,i,result);
i++;
}
getch();
}
/*The output of the above program is
**
Enter a number to generate the table : 3
The table of 3 is given below
3 * 1 = 3
3 * 2 = 6
3 * 3 = 9
3 * 4 = 12
3 * 5 = 15
3 * 6 = 18
3 * 7 = 21
3 * 8 = 24
3 * 9 = 27
3 * 10 = 30
*/
BINARY SEARCH IN C
#include<stdio.h>
#include<math.h>
#include<conio.h>
#include<iostream.h>
#include<process.h>
void main()
{
clrscr();
int a[15],n,begin,end,mid,k,i;
printf("Enter the numer of elements");
scanf("%d",&n);
printf("Enter the elements in ascending order :\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("Enter the number you wants to search");
scanf("%d",&k);
begin=0;
end=(n-1);
while(begin<=end)
{
mid=(begin+end)/2;
if(k==a[mid])
{
printf("The number found");
getch();
exit(0);
}
else if(k>a[mid])
{
begin=mid+1;
}
else if(k<a[mid])
{
end=mid-1;
}
}
printf("Number not found");
getch();
}
No comments:
Post a Comment