Program to Search an Element using BInary Search */ include /* Function Prototype Declaration */void binary_search(); /* Global Declaration of Variables */int a[50], n, item, loc, beg, mid, end, i; /* We used main() because it’s a C-Compiler.Use void main() or int main() if necessary */ main(){printf(“\nEnter size of an array: “);scanf(“%d”, &n); // Reading …
Start reading C Program to Implement BINARY SEARCHBlog
include main(){int n,i,sum;i=1,sum=0;printf(“Enter Maximum Value(n):”);scanf(“%d”,&n);while(i<=n){sum=sum+i*i;++i;}printf(“Sum of squares of numbers from 1 to n is :%d “,sum);}OUTPUT: Enter Maximum Value(n):5Sum of squares of numbers from 1 to n is :55
Start reading C Program for Sum of Squares of Numbers from 1 to ninclude main(){int num;printf(“Enter a Number to test ZERO or +ve or -ve :”);scanf(“%d”,&num);if(num==0)printf(“Entered Number is ZERO”);elseif(num>0)printf(“%d is a Positive Number”,num);elseprintf(“%d is a Negative Numbr”,num);}OUTPUT: Enter a Number to test ZERO or +ve or -ve :1010 is a Positive Number
Start reading C Program to Find Given Integer is Positive or Negativemain(){ int n, i; printf(“Enter an Integer Number to Find PRIME or Not : “); scanf(“%d”, &n); for(i=2; i<=n/2; ++i) { if(n%i==0) { break; } } if(i>n/2) printf(“%d is PRIME”,n); else printf(“%d is NOT PRIME”, n);} OUTPUT: Enter an Integer Number to Find PRIME or Not : 2323 is PRIME
Start reading C Program to Check Given Number is PRIME or Notinclude main(){int no, digit = 0, temp;printf(“Enter an Integer to check for Palindrome : “);scanf(“%d”,&no);temp = no;while( temp != 0 ){digit = digit * 10;digit = digit + temp%10;temp = temp/10;}if ( no == digit )printf(“\n %d is a Palindrome Number :)\n”, no);elseprintf(“\n %d is Not a Palindrome Number :(“, no);} OUTPUT: Enter an Integer …
Start reading C Program to Check Whether a Number is PALINDROME or NotFinding GCD and LCM include int main(){ int no1, no2, rem=0, n1,n2; printf(“Enter Two Non-Zero Integer Mumbers:”); scanf(“%d%d”,&no1,&no2); n1=no1; n2=no2; rem=no1%no2; while(rem != 0) { no1=no2; no2=rem; rem=no1%no2; } printf(“\n GCD of %d and %d is %d”, n1,n2,no2); printf(“\n LCM of %d and %d is %d”,n1,n2,(n1*n2)/no2); return 0;} OUTPUT: Enter Two Non-Zero Integer Mumbers:15 25GCD of 15 and 25 is 5LCM of 15 and 25 is 75
Start reading Finding GCD and LCM of Given Numbers using Cinclude struct student{char name[20];int rollno;float marks;};struct student s1[3];void main(){int i;printf(“Enter Name, RollNo, and Marks of Three Students:\n”);//Reading Student detailsfor(i=0; i<=2; i++)scanf(“%s %d %f”,&s1[i].name,&s1[i].rollno,&s1[i].marks); } OUTPUT:Enter Name, RollNo, and Marks of Three Students:John 123 88Ravi 256 97Raj 214 94Student Name Student Rollno Student Marks:John 123 88.00Ravi 256 97.00Raj 214 94.00
Start reading C Program to Implement Structure with Array/* Write C programs that use both recursive and non-recursive functionsTo solve Towers of Hanoi problem.*/ include include /* Non-Recursive Function/ void hanoiNonRecursion(int num,char sndl,char indl,char dndl) { char stkn[50],stksndl[50],stkindl[50],stkdndl[50],stkadd[50],temp; int top,add; top=NULL; one: if(num==1) { printf(“\nMove top disk from needle %c to needle %c “,sndl,dndl); goto four; } two: top=top+1; stkn[top]=num; stksndl[top]=sndl; stkindl[top]=indl; stkdndl[top]=dndl; …
Start reading C Program to Solve Tower of Hanoi Problem Using Recursive and Non-Recursive/*Calculate Sum of Series:Sum=1-x2/2! +x4/4!-x6/6!+x8/8!-x10/10! (x2 means : x power 2)*/ include include main(){int counter,f_coun;float sum=0,x,power,fact; }
Start reading C Program to Solve Sum of Series 1-x2/2! +x4/4!-x6/6!+x8/8!-x10/10!include void binary_search(); int a[50], n, item, loc, beg, mid, end, i;void main(){printf(“\nEnter size of an array: “);scanf(“%d”, &n);printf(“\nEnter elements of an array in sorted form:\n”);for(i=0; i<n; i++)scanf(“%d”, &a[i]);printf(“\nEnter ITEM to be searched: “);scanf(“%d”, &item);binary_search();getch();}void binary_search(){beg = 0;end = n-1;mid = (beg + end) / 2;while ((beg<=end) && (a[mid]!=item)){if (item < a[mid])end = mid – …
Start reading C Program to Search an Array Element using BINARY SEARCH