Thursday, June 13, 2013
C/C++ programing to search an element from array using binary search.
#include<stdio.h>
#include<conio.h>
int main()
{
int n,DATA[13]={ 11 , 22 , 30 , 33 ,40 , 44 , 55 , 60 , 66 , 77 , 80 , 88 , 99 };
int LB=0,UB=12,ITEM,LOC;
int BEG,END,MID;
BEG=LB;
END=UB;
MID=(BEG+END)/2;
printf(“The Given Array is: \n”);
for(n=0;n<12;n++)
{
printf(“%d\n”,DATA[n]);
}
printf(“Enter The Item to be Searched: “);
scanf(“%d”,&ITEM);
while((BEG<=END)&&(DATA[MID]!=ITEM))
{
if(ITEM<DATA[MID])
{
END=MID-1;
}
else
{
BEG=MID+1;
}
MID=(BEG+END)/2;
}
if(DATA[MID]==ITEM)
{
printf(“LOC= %d”,MID);
}
else
{
printf(“LOC=%d”,-1);
}
getch();
return 0;
}
C/C++ program which take input two no’s unless both no’s less than 250 and odd composite(which are not prime) no’s.
Explanation of Question: When you will enter the 1st then the
compiler take the input of 1st no till you do not enter the no less than
250 and then compiler decide the given no is composite or no if the no
is composite the compiler take the input till u do not enter odd
composite no and then same case with 2nd no and then the program will
return the average of no’s and the table of both no’s.
Don’t use goto statement.
Don’t use goto statement.
I had done this program at dev C++ in turbo this program will give
error.if u want to run this program in turbo please use iostream.h and
remove (using namespace std;) and use one more header file
#include<conio.h> and remove system(“pause”) and use getch(); and
return 0; .
#include<iostream>
using namespace std;
int main()
{
int a,b;
cout<<”Enter The 1st No: “<<endl;
cin>>a;
while( a>250 || a%2==0 )
{
cout<<”Please Enter the odd no less than 250: “<<endl;
cin>>a;
}
int i=0, j=2 ;
while ( j <= a/2 )
{
if( a % j == 0 )
{
i++;
}
j++;
}
while( i==0 && a!=1 || a%2==0)
{
cout<<”Please Enter The Odd Composite No: “<<endl;
cin>>a;
i=0,j=2;
if(a<250)
{
while ( j <= a/2 )
{
if( a % j == 0 )
{
i++;
}
j++;
}
}
else
cout<<”Plese Enter The 1st No Less Than 250 And “;
}
cout<<”Enter The Second No: “<<endl;
cin>>b;
while(b%2==0 || b>250)
{
cout<<”Please Enter The Odd no Less than 250: “<<endl;
cin>>b;
}
j=2;
i=0;
while ( j <= b/2 )
{
if( b % j == 0 )
{
i++;
}
j++;
}
while( i==0 && b!=1 || b%2==0 )
{
cout<<”Please Enter The Odd Composite No: “<<endl;
cin>>b;
if(b<250)
{
i=0,j=2;
while ( j <= b/2 )
{
if( b % j == 0 )
{
i++;
}
j++;
}
}
else
cout<<”Plese Enter The 2nd No Less Than 250 And “;
}
cout<<”1st Odd composite no is: “<<a<<endl;
cout<<”2nd Odd composite no is: “<<b<<endl;
cout<<”The average of two odd composite no’s is: “<<(a+b)/2<<endl;
cout<<”The Table of 1st Odd composite no “<<a<<” is :”<<endl;
int n=1,t;
while(n<=10)
{
cout<<a<<’\t’<<n<<’\t’<<a*n<<endl;
n++;
}
cout<<”The Table of 2nd Odd composite no “<<b<<” is :”<<endl;
n=1;
while(n<=10)
{
cout<<b<<’\t’<<n<<’\t’<<b*n<<endl;
n++;
}
system(“pause”);
}
C/C++ program to print the divisors of an integer/number in descending order.
#include<iostream>
using namespace std;
int main()
{
int num , divisor;
cout<<”Enter the number: “<<endl;
cin>>num;
divisor=num;
if(num<0)
{
cout<<”Please enter the positive integer: “<<endl;
}
cout<<”The divisors of “<<num<<” are:”<<endl;
while(divisor>=1)
{
if(num%divisor==0)
{
cout<<divisor<<endl;
}
divisor–;
}
system(“pause”);
}
C/C++ programing to print counting 1 to 20.
#include<stdio.h>
#include<conio.h>
int main() {
int counter;
counter=0;
while(counter<20)
{
counter++;
printf(“%d\n”,counter);
}
getch();
return 0;
}
#include<conio.h>
int main() {
int counter;
counter=0;
while(counter<20)
{
counter++;
printf(“%d\n”,counter);
}
getch();
return 0;
}
Tuesday, June 11, 2013
Saturday, June 1, 2013
Subscribe to:
Posts (Atom)




