/*
Summary: Binary search compares the search value with the value of the middle element of the array. If they match, then a matching element has been found and its position is returned. Otherwise, if the search value is less than the middle element's value, then the algorithm repeats its action on the sub-array to the left of the middle element or, if the search value is greater, on the sub-array to the right.
Complexity - O(log n)
*/
#include <stdio.h>
int main()
{
int array[10];
int i, j, num, temp, keynum;
int low, mid, high;
printf("Enter the value of num \n");
scanf("%d", &num);
printf("Enter the elements one by one \n");
for (i = 0; i < num; i++)
{
scanf("%d", &array[i]);
}
/* Bubble sorting begins */
for (i = 0; i < num; i++)
{
for (j = 0; j < (num - i - 1); j++)
{
if (array[j] > array[j + 1])
{
temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
}
}
}
printf("Sorted array is...\n");
for (i = 0; i < num; i++)
{
printf("%d\n", array[i]);
}
printf("Enter the element to be searched \n");
scanf("%d", &keynum);
/* Binary searching begins */
low = 1;
high = num;
do
{
mid = (low + high) / 2;
if (keynum < array[mid])
high = mid - 1;
else if (keynum > array[mid])
low = mid + 1;
} while (keynum != array[mid] && low <= high);
if (keynum == array[mid])
{
printf("SEARCH SUCCESSFUL \n");
printf("Number Located at position:%d ",mid+1);
}
else
{
printf("SEARCH FAILED! \n Number not found.");
}
return 0;
}
/*
Summary: Linear search or sequential search is a method for finding a particular value in a list, that consists of checking every one of its elements, one at a time and in sequence, until the desired one is found.
Complexity - O(n)
*/
#include <stdio.h>
int main()
{
int array[10];
int i, num, keynum, found = 0;
printf("Enter the value of num \n");
scanf("%d", &num);
printf("Enter the elements one by one \n");
for (i = 0; i < num; i++)
{
scanf("%d", &array[i]);
}
printf("Enter the element to be searched \n");
scanf("%d", &keynum);
/* Linear search begins */
for (i = 0; i < num ; i++)
{
if (keynum == array[i] )
{
found = 1;
break;
}
}
if (found == 1)
{
printf("Element is present in the array\n");
printf("Number Located at position:%d ",i+1);
}
else
printf("Element is not present in the array\n");
return 0;
}
/*
Summary: Check whether a number entered by user is even or odd.
*/
#include <stdio.h>
int main()
{
int num;
printf("Enter an integer you want to check: ");
scanf("%d",&num);
/* Checking whether remainder is 0 or not. */
if((num%2)==0)
printf("%d is even.",num);
else
printf("%d is odd.",num);
return 0;
}
/*
Summary: Does addition subtraction multiplication division
*/
# include <stdio.h>
int main()
{
char operator;
float num1,num2;
printf("Enter operator either + or - or * or / : ");
scanf("%c",&operator);
printf("Enter two operands: ");
scanf("%f%f",&num1,&num2);
switch(operator)
{
case '+':
printf("num1+num2=%.2f",num1+num2);
break;
case '-':
printf("num1-num2=%.2f",num1-num2);
break;
case '*':
printf("num1*num2=%.2f",num1*num2);
break;
case '/':
printf("num2/num1 = %.2f",num1/num2);
break;
default:
/* If operator is other than +, -, * or /, error message is shown */
printf("Error! operator is not correct");
break;
}
return 0;
}
/*
Summary: Finds roots of a quadratic equation ax2+bx+c=0 where a, b and c are coefficients. This program will ask the coefficients: a, b and c from user and displays the roots.
*/
#include <stdio.h>
#include <math.h>
int main()
{
float a, b, c, determinant, r1,r2, real, imag;
printf("Enter coefficients a, b and c: ");
scanf("%f%f%f",&a,&b,&c);
determinant=b*b-4*a*c;
if (determinant>0)
{
r1= (-b+sqrt(determinant))/(2*a);
r2= (-b-sqrt(determinant))/(2*a);
printf("Roots are: %.2f and %.2f",r1 , r2);
}
else if (determinant==0)
{
r1 = r2 = -b/(2*a);
printf("Roots are: %.2f and %.2f", r1, r2);
}
else
{
real= -b/(2*a);
imag = sqrt(-determinant)/(2*a);
printf("Roots are: %.2f+%.2fi and %.2f-%.2fi", real, imag, real, imag);
}
return 0;
}
/*
Summary: Converts temperature from fahrenheit to degree celcius
*/
#include <stdio.h>
int main ()
{
int fahrenheit;
double celsius;
printf("Enter the temperature in degrees fahrenheit:");
scanf("%d", &fahrenheit);
celsius = (5.0/9.0) * (fahrenheit-32);
printf ("The converted temperature is %lf\n", celsius);
return 0;
}
/*
Summary: Converts decimal number which is base 10 number system 0-9 to hexadecimal which uses the digits from 0 to 9 and A, B, C, D, E, F..
*/
#include<stdio.h>
#include<conio.h>
#include<math.h>
int main()
{
long int num;
printf("Enter the decimal number : ");
scanf("%ld",&num);
long int rem[50],i=0,length=0;
while(num>0)
{
rem[i]=num%16;
num=num/16;
i++;
length++;
}
printf("Hexadecimal number : ");
for(i=length-1;i>=0;i--)
{
switch(rem[i])
{
case 10:
printf("A");
break;
case 11:
printf("B");
break;
case 12:
printf("C");
break;
case 13:
printf("D");
break;
case 14:
printf("E");
break;
case 15:
printf("F");
break;
default :
printf("%ld",rem[i]);
}
}
return 0;
}
/*
Summary: Converts decimal which is base 10 number system which uses the digits from 0 - 9 to octal which is base 8 number system which uses the digits from 0 to 7.
*/
#include<stdio.h>
int main()
{
long int decimalNumber,quotient;
int octalNumber[100],i=1,j;
printf("Enter any decimal number: ");
scanf("%ld",&decimalNumber);
quotient = decimalNumber;
while(quotient!=0)
{
octalNumber[i++]= quotient % 8;
quotient = quotient / 8;
}
printf("Equivalent octal value of decimal number %d: ",decimalNumber);
for(j = i -1 ;j> 0;j--)
printf("%d",octalNumber[j]);
return 0;
}
/*
Summary: Converts decimal number which is base 10 number system 0-9 to binary which is base 2 number system 0 and 1.
*/
#include<stdio.h>
int main()
{
long int decimalNumber,quotient;
int binaryNumber[100],i=1,j;
printf("Enter any decimal number: ");
scanf("%ld",&decimalNumber);
quotient = decimalNumber;
while(quotient!=0)
{
binaryNumber[i++]= quotient % 2;
quotient = quotient / 2;
}
printf("Equivalent binary value of decimal number %d: ",decimalNumber);
for(j = i -1 ;j> 0;j--)
printf("%d",binaryNumber[j]);
return 0;
}
/*
Summary: Compute the yearly depreciation of the value of an item, given by Depreciation = (PURCHASE VALUE - SALVAGE VALUE) / YEAR OF SERVICE
*/
#include<conio.h>
#include<stdio.h>
int main()
{
float sv,pv,dep;
int yos;
printf("Enter the purchase value :- ");
scanf("%f",&pv);
printf("Enter the year of service :- ");
scanf("%d",&yos);
printf("Enter the value of depreation :- ");
scanf("%f",&dep);
sv = pv - (dep * yos);
printf("\n The salvage value equal to :- %f",sv);
return 0;
}
/*
Summary: Calculates compound interest
*/
#include<stdio.h>
#include<conio.h>
#include<math.h>
int main()
{
float p,t,r;
float compound,a;
printf("enter principal:\t");
scanf("%f",&p);
printf("enter rate of interest:\t");
scanf("%f",&r);
printf("enter time in years:\t");
scanf("%f",&t);
if((p<1)||(t<1)||(r<1))
printf("invalid");
else
{
a=(float)p*(pow(1+r/100.0,t));
compound=a-p;
printf("the compound interest is rs.%.2f",compound);
}
return 0;
}
/*
Summary: Print sum of series
1 + 1/2 + 1/3 + 1/4 + ... + 1/N.
*/
#include<stdio.h>
#include<conio.h>
int main()
{
double n,sum=0,i;
printf("\n Please Give The Value of N: ");
scanf("%lf",&n);
for(i=1;i<=n;i++)
{
sum = sum + (1/i);
if(i==1)
printf("\n 1 +");
else if(i==n)
printf(" (1/%.0lf) ",i);
else
printf(" (1/%.0lf) + ",i);
}
printf("\n\n THE SUM OF THIS SERIES IS %.2lf",sum);
return 0;
}
/*
Summary: Check whether a number is armstrong or not. A number is armstrong if the sum of cubes of individual digits of a number is equal to the number itself. For example 371 is an armstrong number as 27 + 343 + 1 = 371.
*/
#include <stdio.h>
int main()
{
int number, sum = 0, temp, remainder;
printf("Enter an integer\n");
scanf("%d",&number);
temp = number;
while( temp != 0 )
{
remainder = temp%10;
sum = sum + remainder*remainder*remainder;
temp = temp/10;
}
if ( number == sum )
printf("Entered number is an armstrong number.\n");
else
printf("Entered number is not an armstrong number.\n");
return 0;
}
/*
Summary: Check whether a number is prime or not
*/
#include <stdio.h>
int main()
{
int n, i, flag=0;
printf("Enter a positive integer: ");
scanf("%d",&n);
for(i=2;i<=n/2;++i)
{
if(n%i==0)
{
flag=1;
break;
}
}
if (flag==0)
printf("%d is a prime number.",n);
else
printf("%d is not a prime number.",n);
return 0;
}
/*
Summary: Finds largest number from array and its location
*/
#include <stdio.h>
int main()
{
int array[100], maximum, size, c, location = 1;
printf("Enter the number of elements in array\n");
scanf("%d", &size);
printf("Enter %d integers\n", size);
for (c = 0; c < size; c++)
scanf("%d", &array[c]);
maximum = array[0];
for (c = 1; c < size; c++)
{
if (array[c] > maximum)
{
maximum = array[c];
location = c+1;
}
}
printf("Maximum element is present at location %d and it's value is %d.\n", location, maximum);
return 0;
}
/*
Summary: Counts frequency of letters in given string
*/
#include <stdio.h>
#include <string.h>
int main()
{
char string[100];
int c = 0, count[26] = {0};
printf("Enter a string\n");
gets(string);
while ( string[c] != '\0' )
{
/* Considering characters from 'a' to 'z' only */
if ( string[c] >= 'a' && string[c] <= 'z' )
count[string[c]-'a']++;
c++;
}
for ( c = 0 ; c < 26 ; c++ )
{
if( count[c] != 0 )
printf("%c occurs %d times in the entered string.\n",c+'a',count[c]);
}
return 0;
}
/*
Summary: Find how many number of vowels are present in a string.
*/
#include<stdio.h>
int count_vowels(char []);
int check_vowel(char);
int main()
{
char array[100];
int c;
printf("Enter a string\n");
gets(array);
c = count_vowels(array);
printf("Number of vowels: %d\n", c);
return 0;
}
int count_vowels(char a[])
{
int count = 0, c = 0, flag;
char d;
do
{
d = a[c];
flag = check_vowel(d);
if ( flag == 1 )
count++;
c++;
}while( d != '\0' );
return count;
}
int check_vowel(char a)
{
if ( a >= 'A' && a <= 'Z' )
a = a + 'a' - 'A'; /* Converting to lower case */
if ( a == 'a' || a == 'e' || a == 'i' || a == 'o' || a == 'u')
return 1;
return 0;
}
/*
Summary: Factorial is represented using '!', so five factorial will be written as (5!),n factorial as (n!).Also
n! = n*(n-1)*(n-2)*(n-3)...3.2.1 and zero factorial is defined as one i.e. 0! = 1.
*/
#include<stdio.h>
int fact(int);
int main()
{
int num,f;
printf("\nEnter a number: ");
scanf("%d",&num);
f=fact(num);
printf("\nFactorial of %d is: %d",num,f);
return 0;
}
//recursive function
int fact(int n)
{
if(n==1)
return 1;
else
return(n*fact(n-1));
}