Programming in C


Programs:-


Age(years) to Age(second) Converter.

/*this program convert your age into seconds*/
#include<stdio.h>
int main()
{
float years,secs;
printf("Input you age in yaers=");
scanf("%f",&years);
secs=years*365*24*60*60;
printf("You have lived for about %f seconds\n ",secs);

}
Character to ASCII

#include<stdio.h>
#include<conio.h>
#include<math.h>
main()
{
char a;

printf("Enter your character\n");
scanf("%c",&a);
printf("Entered character ASCII value is=%d",a);
getch();
}


Data type Input Output

#include<stdio.h>
#include<conio.h>
main()
{
int i;
float f;
long double d;
char t, c;

printf("\nEnter the integer value=\n");
scanf("%d",&i);
printf("\nInteger=%d\n",i);
printf("\nEnter the float value=\n");
scanf("%f",&f);
printf("\nFloat=\n%f",f);
printf("\nEnter the double value=\n");
scanf("%f",&d);
printf("\nDouble Value=\n%lf",d);
printf("\nEnter the character=\n");
c=getche();
printf("\nCharatcter=\n%c",c);
getch();

}


Even Or Odd

#include<conio.h>
#include<stdio.h>
main()
{
int n;
printf("Enter the number to know whether even or odd ");
scanf("%d",&n);
if(n%2==0)
printf("Entered number is even ");
else
printf("Entered number is odd");
}


Factorial Of a Number

#include<conio.h>
#include<stdio.h>
main()
{
int n,fact=1,i;
printf("Enter the number=\n");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
fact=fact*i;
}
printf("\nFactorial of the given number is=%d",fact);
}
Grade And Marks Of  Students

#include<conio.h>
#include<stdio.h>
main()
{
float a,b,c,d,t,avg;
printf("Enter 4 subjects marks\n");
scanf("%f%f%f%f",&a,&b,&c,&d);
t=(a+b+c+d);
printf("Total Marks=%f\n",t);
avg=t/4;
printf("Average Marks=%f\n",avg);

if(avg>=75)
{
printf("Grade=A");
}
else if (avg>=60 && avg<75)
{
printf("Grade=B");
}
else if (avg<60 && avg>=40)
{
printf("Grade=C");
}
else
printf("Grade=E");
}


Herons Formula

#include<stdio.h>
#include<conio.h>
#include<math.h>

main()
{
int a,b,c;
float s,area;

printf("Enter the sides of triangle\n");
scanf("%d%d%d",&a,&b,&c);
s=((a+b+c)/2);
area=(sqrt(s*(s-a)*(s-b)*(s-c)));
printf("\nArea=%f",area);
getch();
}




Largest Of Two

#include<conio.h>
#include<stdio.h>
main()
{
float a,b;
printf("Enter the two numbers to find the largest among them=");
scanf("%f%f",&a,&b);
if(a>b)
{
printf("largest number is=%f",a);
}
else
printf("largest number is=%f",b);
}
Leap Or Not

#include<conio.h>
#include<stdio.h>
main()
{
int y;
printf("Enter the year you want to check=");
scanf("%d",&y);
if(y%4==0)
{
printf("It is a leap year");
}
else
printf("Not a leap year");
}
Prime or not

#include<conio.h>
#include<stdio.h>
int main()
{
int n,i,temp=0;
printf("Enter the number(+ve) =");
scanf("%d",&n);
{
for(i=2;i<=n/2;++i)
{
if(n%i==0)
{
temp=1;
break;

}
}
if (temp==0)
printf("It is a prime number");
else
printf("It is not a prime number");

}
}
Right Triangle Or Not

#include<stdio.h>
#include<conio.h>
#include<math.h>
int main()
{
float x,y,z;
printf("Enter the sides of triangle =\n");
scanf("%f%f%f",&x,&y,z);
if((x=sqrt(z^2+y^2))||(y=sqrt(x^2+y^2)||(x=sqrt(x^2+y^2)));
printf("\nThe swapped number is=\n%d\n%d",x,y);
getch();

}
Swapping No Variable

#include<stdio.h>
#include<conio.h>
int main()
{
int x,y;
printf("Enter the two number to be swapped=\n");
scanf("%d%d",&x,&y);
x=x+y;
y=x-y;
x=x-y;
printf("\nThe swapped number is=\n%d\n%d",x,y);
getch();

}
Temp Fahrenheit To Celsius

/*This program convert temperature in degree celcius to Fahrenheit*/
#include<conio.h>
#include<stdio.h>
int main()
{
float f,t;
printf("Enter the temp in degree Fahrenheit=");
scanf("%f",&f);
t=(0.55*(f-32));
{
printf("\nThe Temperature in degree celcius=%f",t);
}

getch();

}


To Convert case

#include<conio.h>
#include<stdio.h>
#include<ctype.h>
main()
{
char t,c;
printf("Enter the character =");
scanf("%c",&c);
t=toupper(c);
printf("The Uppercase of the entered character is=%c",t);

}


To print triangle

#include<conio.h>
#include<stdio.h>
main()
{
int n,i,j;
printf("Enter the rows want in triangular form=");
scanf("%d",&n);

for(i=1;i<=n;i++)
{
printf("\n");

for(j=1;j<=i;j++)
{
printf("%d",j);
}
}

}





Alphabetical Sort
/*
Summary: Alphabetical sort is a program whereby strings of characters are placed in order based on the position of the characters in the conventional ordering of an alphabet.
*/
#include <stdio.h>
#include <string.h>
#include <conio.h>
void main()
{
char name[10][8], tname[10][8], temp[8];
int i, j, n;
printf("Enter the value of n \n");
scanf("%d", &n);
printf("Enter %d names ", n);
for (i = 0; i < n; i++)
{
scanf("%s", name[i]);
strcpy(tname[i], name[i]);
}
for (i = 0; i < n - 1 ; i++)
{
for (j = i + 1; j < n; j++)
{
if (strcmp(name[i], name[j]) > 0)
{
strcpy(temp, name[i]);
strcpy(name[i], name[j]);
strcpy(name[j], temp);
}
}
}
printf("\n----------------------------------------\n");
printf("Input Names\tSorted names\n");
printf("------------------------------------------\n");
for (i = 0; i < n; i++)
{
printf("%s\t\t%s\n", tname[i], name[i]);
}
printf("------------------------------------------\n");
getch();
}
/*
Input: Enter the value of n
7
Enter 7 names
heap
stack
queue
object
class
program
project
*/


Armstrong Number
/*
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;
}
/*
Input: Enter a number to check if it is an armstrong number
371
*/


Binary search
/*
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>
void 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.");
}
getch();
}
/*
Input: Enter the value of num
5
Enter the elements one by one
23
90
56
15
58
Sorted array is...
15
23
56
58
90
Enter the element to be searched
58
*/


Bubble Sort
/*
Summary: Bubble sort is a simple sorting algorithm that works by repeatedly stepping through the list to be sorted, comparing each pair of adjacent items and swapping them if they are in the wrong order.
Complexity - O(n^2)
*/
#include <stdio.h>
#define MAXSIZE 10
void main()
{
int array[MAXSIZE];
int i, j, num, temp;
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]);
}
getch();
}
/*
Input: Enter the value of num
6
Enter the elements one by one
23
45
67
89
12
34
*/


Calculation of Roots of Quadratic Equation
/*
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;
}
/*
Input: Enter coefficients a, b and c: 4
1
0
*/


Cock Tail Sort
/*
Summary: Cocktail sort is a variation of bubble sort that is both a stable sorting algorithm and a comparison sort. The algorithm differs from a bubble sort in that it sorts in both directions on each pass through the list. This sorting algorithm is only marginally more difficult to implement than a bubble sort, and solves the problem of turtles in bubble sorts.
The various names of cocktail sort are bidirectional bubble sort, cocktail shaker sort, shaker sort, ripple sort, shuffle sort, shuttle sort or happy hour sort.
Complexity - O(n^2)
*/
#include <stdio.h>
#define MAX 8
int main()
{
int data[MAX];
int i, j, n, c;
printf("\nEnter the data");
for (i = 0; i < MAX; i++)
{
scanf("%d", &data[i]);
}
n = MAX;
do
{
/*Rightward pass will shift the largest element to its correct place at the end*/
for (i = 0; i < n - 1; i++)
{
if (data[i] > data[i + 1])
{
data[i] = data[i] + data[i + 1];
data[i + 1] = data[i] - data[i + 1];
data[i] = data[i] - data[i + 1];
}
}
n = n - 1;
/* Leftward pass will shift the smallest element to its correct place at the beginning*/
for (i= MAX - 1, c = 0; i >= c; i--)
{
if(data[i] < data[i - 1])
{
data[i] = data[i] + data[i - 1];
data[i - 1] = data[i] - data[i - 1];
data[i] = data[i] - data[i - 1];
}
}
c = c + 1;
} while (n != 0 && c != 0);
printf("The sorted elements are:");
for (i = 0; i < MAX; i++)
{
printf("%d\t", data[i]);
}
}
/*
Input: Enter the data
9 6 2 12 11 9 3 7
*/


Compare String
/*
Summary: Compares two string for equality
*/
#include <stdio.h>
#include <string.h>
int main()
{
char a[100], b[100];
printf("Enter the first string\n");
gets(a);
printf("Enter the second string\n");
gets(b);
if( strcmp(a,b) == 0 )
printf("Entered strings are equal.\n");
else
printf("Entered strings are not equal.\n");
return 0;
}
/*
Input: simple
sample
*/


Compound Interest Calculation
/*
Summary: Calculates compund interest
*/
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float p,t,r;
float i,compound,a;
clrscr();
printf("enter principal:\t");
scanf("%d",&p);
printf("enter rate of interest:\t");
scanf("%d",&r);
printf("enter time in years:\t");
scanf("%d",&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);
}
getch();
}
/*
Input: enter principal: 100
enter rate of interest: 10
enter time in years: 1
*/


Decimal to Binary Conversion
/*
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,remainder,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;
}
/*
Input: Enter any decimal number: 50
*/


Decimal to Hexadecimal Conversion
/*

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>
void main()
{
long int num;
clrscr();
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]);
}
}
getch();
}
/*
Input: Enter the decimal number : 87274
*/


Decimal to Octal Conversion
/*
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,remainder,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;
}
/*
Input: Enter any decimal number: 50
*/


Depreciation Calculation
/*
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>
void main()
{
float sv,pv,dep;
int yos;
clrscr();
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);
getch();
}
/*
Input: Enter the value of purchase value :- 800
Enter the year of service :- 200
Enter the value of depreation :- 2
*/


Even Odd Number
/*
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;
}
/*
Input: Enter an integer you want to check: 25
*/





Factorial of a Number
/*
Summary: Factorial is represented using '!', so five factorial will be written as (5!),n factorial as (n!).
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 main()
{
int c, n, fact = 1;
printf("Enter a number to calculate it's factorial\n");
scanf("%d", &n);
for (c = 1; c <= n; c++)
fact = fact * c;
printf("Factorial of %d = %d\n", n, fact);
return 0;
}
/*
Input: Enter a number to calculate it's factorial
6
*/


Factorial of a Number using Recursion
/*
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));
}
/*
Input: Enter a number: 5
*/


Fahrenheit to Celsius Conversion
/*
Summary: Converts temperature from fahrenheit to degree celcius
*/
#include <stdio.h>
int main (void)
{
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;
}
/*
Input: Enter the temperature in degrees fahrenheit
100
*/


Fibonacci Number Series
/*
Summary: Fibonacci Sequence is the series of numbers:
0, 1, 1, 2, 3, 5, 8, 13, 21, 34,...
*/
#include <stdio.h>
int main() {
int count, n, t1=0, t2=1, display=0;
printf("Enter number of terms: ");
scanf("%d",&n);
/* Displaying first two terms */
printf("Fibonacci Series: %d+%d+", t1, t2);
count=2;
/* count=2 because first two terms are already displayed. */
while (count<n) {
//add first two
display=t1+t2;
//set first no. value to second
t1=t2;
//set second no. value to result of addition
t2=display;
//increment count
++count;
printf("%d+",display);
}
return 0;
}
/*
Input: Enter number of terms: 10
*/


Floyd triangle pattern
/*
Summary: Number of rows of Floyd's triangle to print is entered by the user. First four rows of Floyd's triangle are as follows :-
1
2 3
4 5 6
7 8 9 10
It's clear that in Floyd's triangle nth row contains n numbers.
*/
#include <stdio.h>
int main()
{
int n, i, c, a = 1;
printf("Enter the number of rows of Floyd's triangle to print\n");
scanf("%d", &n);
printf("Floyd's triangle\n");
for (i = 1; i <= n; i++)
{
for (c = 1; c <= i; c++)
{
printf("%d ",a);
a++;
}
printf("\n");
}
return 0;
}
/*
Input: Enter the number of rows of Floyd's triangle to print
4
*/


Gnome Sort
/*
Summary: Gnome sort(stupid sort) is a sorting algorithm which is similar to insertion sort, except that moving an element to its proper place is accomplished by a series of swaps, as in bubble sort. It is conceptually simple, requiring no nested loops.
Complexity - O(n^2)
*/
#include <stdio.h>
void main()
{
int i, temp, ar[10], n;
printf("\nenter the elemts number u would like to enter:");
scanf("%d", &n);
printf("\nenter the elements to be sorted through gnome sort:\n");
for (i = 0; i < n; i++)
scanf("%d", &ar[i]);
i = 0;
while (i < n)
{
if (i == 0 || ar[i - 1] <= ar[i])
i++;
else
{
temp = ar[i-1];
ar[i - 1] = ar[i];
ar[i] = temp;
i = i - 1;
}
}
for (i = 0;i < n;i++)
printf("%d\t", ar[i]);
}
/*
Input: enter the elemts number u would like to enter:7
enter the elements to be sorted through gnome sort:
6
0
9
5
2
4
3
*/


Harmonic Number Series
/*
Summary: Print sum of series
1 + 1/2 + 1/3 + 1/4 + ... + 1/N.
*/
#include<stdio.h>
#include<conio.h>
void main()
{
double n,sum=0,i;
clrscr();
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/%d) ",i);
else
printf(" (1/%d) + ",i);
}
printf("\n\n THE SUM OF THIS SERIES IS %.2lf",sum);
getch();
}
/*
Input: Please Give The Value of N: 5
*/


Heap Sort
/*
Summary: The heapsort algorithm can be divided into two parts.
-Step 1: a heap is built out of the data.
-Step 2: a sorted array is created by repeatedly removing the largest element from the heap, and inserting it into the array.
The heap is reconstructed after each removal. Once all objects have been removed from the heap, we have a sorted array. The direction of the sorted elements can be varied by choosing a min-heap or max-heap in step one.
Complexity: O(n•log n)
*/
#include <stdio.h>
void main()
{
int heap[10], no, i, j, c, root, temp;
clrscr();
printf("\n Enter no of elements :");
scanf("%d", &no);
printf("\n Enter the nos : ");
for (i = 0; i < no; i++)
scanf("%d", &heap[i]);
for (i = 1; i < no; i++)
{
c = i;
do
{
root = (c - 1) / 2;
if (heap[root] < heap[c]) /* to create MAX heap array */
{
temp = heap[root];
heap[root] = heap[c];
heap[c] = temp;
}
c = root;
} while (c != 0);
}
printf("Heap array : ");
for (i = 0; i < no; i++)
printf("%d\t ", heap[i]);
for (j = no - 1; j >= 0; j--)
{
temp = heap[0];
heap[0] = heap[j]; /* swap max element with rightmost leaf element */
heap[j] = temp;
root = 0;
do
{
c = 2 * root + 1; /* left node of root element */
if ((heap[c] < heap[c + 1]) && c < j-1)
c++;
if (heap[root]<heap[c] && c<j) /* again rearrange to max heap array */
{
temp = heap[root];
heap[root] = heap[c];
heap[c] = temp;
}
root = c;
} while (c < j);
}
printf("\n The sorted array is : ");
for (i = 0; i < no; i++)
printf("\t %d", heap[i]);
printf("\n Complexity : \n Best case = Avg case = Worst case = O(n logn) \n");
getch();
}
/*
Input: Enter no of elements :7
Enter the nos : 6
5
3
1
8
7
2
*/
Hello World
#include <stdio.h>
void main()
{
printf("Hello world!");
}


Insertion Sort
/*
Summary: Insertion sort iterates, consuming one input element each repetition, and growing a sorted output list. Each iteration, insertion sort removes one element from the input data, finds the location it belongs within the sorted list, and inserts it there. It repeats until no input elements remain.
Complexity - O(n^2)
*/
#include <stdio.h>
#define MAX 10
void insertion_sort(int *);
void main()
{
int a[MAX], i;
clrscr();
printf("enter 10 elements to be sorted:");
for (i = 0;i < MAX;i++)
{
scanf("%d", &a[i]);
}
insertion_sort(a);
printf("sorted elements:\n");
for (i = 0;i < MAX; i++)
{
printf(" %d", a[i]);
}
getch();
}
/* sorts the input */
void insertion_sort(int * x)
{
int temp, i, j;
for (i = 1;i < MAX;i++)
{
temp = x[i];
j = i - 1;
while (temp < x[j] && j >= 0)
{
x[j + 1] = x[j];
j = j - 1;
}
x[j + 1] = temp;
}
}
/*
Input: enter elements to be sorted:8 2 4 9 3 6 1
*/


Largest Number
/*
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;
}
/*
Input: Enter the number of elements in array
5
Enter 5 integers
4
5
6
8
2
*/


Letter Count of a String
/*
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;
}
/*
Input: ABCAPPPRC
*/


Linear Search
/*
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>
void 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");
getch();
}
/*
Input: Enter the value of num
5
Enter the elements one by one
456
78
90
40
100
Enter the element to be searched
40
*/


Linked List Data Structure
/*
Summary: Linked list is an ordered set of data elements, each containing a link to its successor. This program is to create a linked list. After creating the linked list ,the length of linked list is calculated.
*/
#include <stdio.h>
#include <conio.h>
#define newnode (struct node*) malloc(sizeof(struct node))
struct node
{
int data;
struct node *next;
};
struct node *create_list();
void main()
{
struct node *f;
int len;
f = NULL;
clrscr();
f = create_list();
len = find_len(f);
printf("\n length = %d",len);
} // main
struct node *create_list()
{
struct node *f,*c,*p;
int tdata;
f = NULL;
printf("\n Enter data ( use 0 to exit ) : ");
scanf("%d",&tdata);
while( tdata != 0 )
{
c = newnode;
if( c == NULL)
{
printf("\n Insuf. mem. ");
exit(0);
}
c->data = tdata;
c->next = NULL;
if( f== NULL)
f = c;
else
p->next = c;
p = c;
printf("\n Enter data ( use 0 to exit ) : ");
scanf("%d",&tdata);
} //while
return(f);
} // create list
int find_len(struct node *f)
{
int len=0;
struct node *t;
if( f == NULL)
return(0);
t = f;
while( t != NULL )
{
len++;
t = t->next;
}
return(len);
}
/*
Input: Enter data ( use 0 to exit ) : 5
Enter data ( use 0 to exit ) : 15
Enter data ( use 0 to exit ) : 65
Enter data ( use 0 to exit ) : 2
Enter data ( use 0 to exit ) : 657
Enter data ( use 0 to exit ) : 3
Enter data ( use 0 to exit ) : 0
*/


Merge Sort
/*
Summary: Merge sort is a divide and conquer comparison-based sorting algorithm. It works as follows:-
1. Divide the unsorted list into n sublists, each containing 1 element (a list of 1 element is considered sorted).
2. Repeatedly merge sublists to produce new sublists until there is only 1 sublist remaining. This will be the sorted list.
Complexity - O(n•log n)
*/
#include <stdio.h>
void mergeSort(int [], int, int, int);
void partition(int [],int, int);
int main()
{
int list[50];
int i, size;
clrscr();
printf("Enter total number of elements:");
scanf("%d", &size);
printf("Enter the elements:\n");
for(i = 0; i < size; i++)
{
scanf("%d", &list[i]);
}
partition(list, 0, size - 1);
printf("After merge sort:\n");
for(i = 0;i < size; i++)
{
printf("%d ",list[i]);
}
getch();
return 0;
}
void partition(int list[],int low,int high)
{
int mid;
if(low < high)
{
mid = (low + high) / 2;
partition(list, low, mid);
partition(list, mid + 1, high);
mergeSort(list, low, mid, high);
}
}
void mergeSort(int list[],int low,int mid,int high)
{
int i, mi, k, lo, temp[50];
lo = low;
i = low;
mi = mid + 1;
while ((lo <= mid) && (mi <= high))
{
if (list[lo] <= list[mi])
{
temp[i] = list[lo];
lo++;
}
else
{
temp[i] = list[mi];
mi++;
}
i++;
}
if (lo > mid)
{
for (k = mi; k <= high; k++)
{
temp[i] = list[k];
i++;
}
}
else
{
for (k = lo; k <= mid; k++)
{
temp[i] = list[k];
i++;
}
}
for (k = low; k <= high; k++)
{
list[k] = temp[k];
}
}
/*
Input: Enter total number of elements:5
Enter the elements:
12
36
22
76
54
*/


Palindrome String
/*
Summary: Palindromes are words or phrases that read the same in both directions.
*/
#include<stdio.h>
#include<string.h>
#include<conio.h>
char *strrev(char *str)
{
char *p1, *p2;
if (! str || ! *str)
return str;
for (p1 = str, p2 = str + strlen(str) - 1; p2 > p1; ++p1, --p2)
{
*p1 ^= *p2;
*p2 ^= *p1;
*p1 ^= *p2;
}
return str;
}
int main()
{
char a[100], b[100];
//Get the number from user.
printf("Enter the string to check if it is a palindrome\n");
gets(a);
//copy input to another array b
strcpy(b,a);
//Reverse it.
strrev(b);
/*Compare it with the number entered by the user.If both are same then print palindrome number*/
if( strcmp(a,b) == 0 )
printf("\nEntered string is a palindrome.\n");
else
printf("\nEntered string is not a palindrome.\n");
getch();
return 0;
}
/*
Input: radar
*/


Pascal number triangle pattern
/*
Summary: Pascal triangle is associated with Binomial Theorem in Mathematics.
*/
#include <stdio.h>
long factorial(int n)
{
int c;
long result = 1;
for( c = 1 ; c <= n ; c++ )
result = result*c;
return ( result );
}
int main()
{
int i, n, c;
printf("Enter the number of rows you wish to see in pascal triangle\n");
scanf("%d",&n);
for ( i = 0 ; i < n ; i++ )
{
for ( c = 0 ; c <= ( n - i - 2 ) ; c++ )
printf(" ");
for( c = 0 ; c <= i ; c++ )
printf("%ld ",factorial(i)/(factorial(c)*factorial(i-c)));
printf("\n");
}
return 0;
}
/*
Input: Enter the no. of lines: 8
*/


Prime Number
/*
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;
}
/*
Input: Enter a positive integer: 29
*/


Print half pyramid pattern using asterix
/*
Summary: Program prints half a pyramid using * character
*/
#include<stdio.h>
main()
{
int n, c, k;
printf("Enter number of rows\n");
scanf("%d",&n);
for ( c = 1 ; c <= n ; c++ )
{
for( k = 1 ; k <= c ; k++ )
printf("* ");

printf("\n");
}
return 0;
}
/*
Input: Enter number of rows
4
*/


Print triangle pattern using asterix
/*
Summary: Prints triangle using *
*/
#include<stdio.h>
main()
{
int n, c, k = 2, j;
printf("Enter number of rows\n");
scanf("%d",&n);
for ( j = 1 ; j <= n ; j++ ){
for ( c = 1 ; c <= 2*n-k ; c++)
printf(" ");
k = k + 2;
for ( c = 1 ; c <= j ; c++)
printf("* ");
printf("\n");
}
getch();
return 0;
}
/*
Input: Enter number of rows
4
*/


Queue Data Structure
/*
Summary: Queue is a data structure which works as FIFO principle. FIFO means “First in First out”, i.e the element which we have inserted first will be deleted first and the element that we have inserted last will be deleted last.Two variables are used to implement queue, i.e “rear” and “front”. Insertion will be done at rear side and deletion will be performed at front side.
*/
#include <stdio.h>
#define MAX 50
int queue_array[MAX];
int rear = - 1;
int front = - 1;
main()
{
int choice;
while (1)
{
printf("1.Insert element to queue \n");
printf("2.Delete element from queue \n");
printf("3.Display all elements of queue \n");
printf("4.Quit \n");
printf("Enter your choice : ");
scanf("%d", &choice);
switch (choice)
{
case 1:
insert();
break;
case 2:
delete();
break;
case 3:
display();
break;
case 4:
exit(1);
default:
printf("Wrong choice \n");
}
}
}
insert()
{
int add_item;
if (rear == MAX - 1)
printf("Queue Overflow \n");
else
{
if (front == - 1)
/*If queue is initially empty */
front = 0;
printf("Inset the element in queue : ");
scanf("%d", &add_item);
rear = rear + 1;
queue_array[rear] = add_item;
}
}
delete()
{
if (front == - 1 || front > rear)
{
printf("Queue Underflow \n");
return ;
}
else
{
printf("Element deleted from queue is : %d\n", queue_array[front]);
front = front + 1;
}
}
display()
{
int i;
if (front == - 1)
printf("Queue is empty \n");
else
{
printf("Queue is : \n");
for (i = front; i <= rear; i++)
printf("%d ", queue_array[i]);
printf("\n");
}
}
/*
Input: //Enter choices and data
*/


Quick Sort
/*
Summary: Quicksort is a very efficient sorting algorithm.It has two phases:
-the partition phase and
-the sort phase.
Most of the work is done in the partition phase - it works out where to divide the work. The sort phase simply sorts the two smaller problems that are generated in the partition phase.
Complexity: O(n•log n)
*/
#include <stdio.h>
void quicksort (int [], int, int);
int main()
{
int list[50];
int size, i;
clrscr();
printf("Enter the number of elements: ");
scanf("%d", &size);
printf("Enter the elements to be sorted:\n");
for (i = 0; i < size; i++)
{
scanf("%d", &list[i]);
}
quicksort(list, 0, size - 1);
printf("After applying quick sort\n");
for (i = 0; i < size; i++)
{
printf("%d ", list[i]);
}
printf("\n");
getch();
return 0;
}
void quicksort(int list[], int low, int high)
{
int pivot, i, j, temp;
if (low < high)
{
pivot = low;
i = low;
j = high;
while (i < j)
{
while (list[i] <= list[pivot] && i <= high)
{
i++;
}
while (list[j] > list[pivot] && j >= low)
{
j--;
}
if (i < j)
{
temp = list[i];
list[i] = list[j];
list[j] = temp;
}
}
temp = list[j];
list[j] = list[pivot];
list[pivot] = temp;
quicksort(list, low, j - 1);
quicksort(list, j + 1, high);
}
}
/*
Input: Enter the number of elements: 6
Enter the elements to be sorted:
67
45
24
98
12
38
*/


Radix Sort
/*
Summary: Radix sort is a non-comparative integer sorting algorithm that sorts data with integer keys by grouping keys by the individual digits which share the same significant position and value.
Two classifications of radix sorts are least significant digit (LSD) radix sorts and most significant digit (MSD) radix sorts. LSD radix sorts process the integer representations starting from the least digit and move towards the most significant digit. MSD radix sorts work the other way around.
Complexity - O(d•n)
*/
#include <stdio.h>
int min = 0, count = 0, array[100] = {0}, array1[100] = {0};
void main()
{
int k, i, j, temp, t, n;
clrscr();
printf("Enter size of array :");
scanf("%d", &count);
printf("Enter elements into array :");
for (i = 0; i < count; i++)
{
scanf("%d", &array[i]);
array1[i] = array[i];
}
for (k = 0; k < 3; k++)
{
for (i = 0; i < count; i++)
{
min = array[i] % 10; /* To find minimum lsd */
t = i;
for (j = i + 1; j < count; j++)
{
if (min > (array[j] % 10))
{
min = array[j] % 10;
t = j;
}
}
temp = array1[t];
array1[t] = array1[i];
array1[i] = temp;
temp = array[t];
array[t] = array[i];
array[i] = temp;
}
for (j = 0; j < count; j++) /*to find MSB */
array[j] = array[j] / 10;
}
printf("Sorted Array (lSdradix sort) : ");
for (i = 0; i < count; i++)
printf("%d ", array1[i]);
getch();
}
/*
Input: Enter size of array :7
Enter elements into array :170
45
90
75
802
24
2
*/


Reverse String
/*
Summary: Reverses a given string
*/
#include <stdio.h>
#include <string.h>
char *strrev(char *str)
{
char *p1, *p2;
if (! str || ! *str)
return str;
for (p1 = str, p2 = str + strlen(str) - 1; p2 > p1; ++p1, --p2)
{
*p1 ^= *p2;
*p2 ^= *p1;
*p1 ^= *p2;
}
return str;
}
int main()
{
char arr[100];
printf("Enter a string to reverse\n");
gets(arr);
strrev(arr);
printf("Reverse of entered string is \n%s\n",arr);
return 0;
}
/*
Input: John
*/


Selection Sort
/*
Summary: Selection sort divides the input list into two parts: the sublist of items already sorted and the sublist of items remaining to be sorted. It proceeds by finding the smallest (or largest) element in the unsorted sublist, exchanging it with the leftmost unsorted element and moving the sublist boundaries one element to the right.
Complexity - O(n^2)
*/
#include<conio.h>
#include<stdio.h>
#define max 50
void selsort(int, int[]);
void main()
{
int i, size, data[max];
clrscr();
printf("\nEnter no of Elements:");
scanf("%d",&size);
printf("\nEnter Elements:");
for(i=1;i<=size;i++)
scanf("%d",&data[i]);
printf("\nUnsorted data:\n");
for(i=1;i<=size;i++)
printf("%d\t",data[i]);
selsort(size, data);
getch();
}
void selsort(int n, int data[])
{
int i, j, min, temp;
printf("\nSorted List is:\n");
for(i=1;i<=n-1;i++)
{
min = i;
for(j=i+1;j<=n;j++)
{
if(data[j]<data[min])
min = j;
}
temp=data[i];
data[i]=data[min];
data[min]=temp;
}
for(i=1;i<=n;i++)
printf("%d\t",data[i]);
}
/*
Input: Enter no of Elements:5
Enter Elements: 50
40
30
20
10
*/


Shell Sort
/*
Summary: Shell Sort generalizes an exchanging sort, such as insertion or bubble sort, by starting the comparison and exchange of elements with elements that are far apart before finishing with neighboring elements. Starting with far apart elements can move some out-of-place elements into position faster than a simple nearest neighbor exchange.The idea is to arrange the list of elements so that, starting anywhere, considering every hth element gives a sorted list.
*/
#include <stdio.h>
#define size 10
/* Function Prototype */
int shell_sort(int []);
void main()
{
int arr[size], i;
clrscr();
printf("Enter 10 elements to be sorted:");
for (i = 0;i < size;i++)
{
scanf("%d", &arr[i]);
}
shell_sort(arr);
printf("The array after sorting is:");
for (i = 0;i < size;i++)
{
printf("\n%d", arr[i]);
}
getch();
}
/* Code to sort array using shell sort */
int shell_sort(int array[])
{
int i = 0, j = 0, k = 0, mid = 0;
for (k = size / 2;k > 0;k /= 2)
{
for (j = k;j < size;j++)
{
for (i = j - k;i >= 0;i -= k)
{
if (array[i + k] >= array[i])
{
break;
}
else
{
mid = array[i];
array[i] = array[i + k];
array[i + k] = mid;
}
}
}
}
return 0;
}
/*
Input: Enter the elements to be sorted:57
67
48
93
42
84
95
*/


Simple Calculator
/*
Summary: Does addition subtraction multiplication division
*/
# include <stdio.h>
int main() {
char operator;
float num1,num2;
printf("Enter operator either + or - or * or divide : ");
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;
}
/*
Input: Enter operator either + or - or * or divide: /
Enter two operands: 13.456
4.56
*/


Stack Data Structure
/*
Summary: Stack is a particular kind of abstract data type or collection. Theonly operations on a stack are the addition of an entity, known as push and removal of an entity, known as pop.The relation between the push and pop operations is such that the stack is a Last-In-First-Out (LIFO) data structure.
*/
#include <conio.h>
#include <stdio.h>
#define max 50
void push();
void pop();
void display();
int menu();
int stack[max], top=0;
void main()
{
int ch;
clrscr();
do{
ch=menu();
switch(ch)
{
case 1: push();
break;
case 2: pop();
break;
case 3: display();
break;
case 4: exit(1);
default: printf("\nEnter a valid choice!!");
}
}while(1);
}
int menu()
{
int ch;
printf("\nStack");
printf("\n1.Push\n2.Pop\n3.Display\n4.Exit");
printf("\nEnter your Choice:");
scanf("%d",&ch);
return ch;
}
void push()
{
if(top==max)
printf("\nOverflow");
else
{
int element;
printf("\nEnter Element:");
scanf("%d",&element);
printf("\nElement(%d) has been pushed at %d", element, top);
stack[top++]=element;
}
}
void pop()
{
if(top==-1)
printf("\nUnderflow");
else
{
top--;
printf("\nELement has been popped out!");
}
}
void display()
{
if(top==0)
printf("\nStack is Empty!!");
else
{
int i;
for(i=0;i<max;i++)
printf("%d",stack[i]);
}
}
/*
Input: Stack
1.Push
2.Pop
3.Display
4.Exit
Enter your Choice:1
Enter Element:32
Stack
1.Push
2.Pop
3.Display
4.Exit
Enter your Choice:2
Stack
1.Push
2.Pop
3.Display
4.Exit
Enter your Choice:3
*/


String Concatenation
/*
Summary: Joins two strings
*/
#include <stdio.h>
#include <string.h>
int main()
{
char a[100], b[100];
printf("Enter the first string\n");
gets(a);
printf("Enter the second string\n");
gets(b);
strcat(a,b);
printf("String obtained on concatenation is %s\n",a);
return 0;
}
/*
Input: Enter first string: lol
Enter second string: :)
*/


Sum of numbers between 100 and 200 divisible by 7
/*
Summary: Sum of numbers between 100 and 200 which are divisible by 7
*/
#include<stdio.h>
#include<conio.h>
void main()
{
int i,sum = 0;
clrscr();
for(i = 100;i <= 200 ; i++)
{
if (i % 7 == 0)
sum = sum + i;
}
printf("Sum of all no between 100 and 200 ");
printf("which is divisible by 7 is :: %d",sum);
getch();
}
/*
Input: No input
*/


Swap two numbers without using third
/*
Summary: Program swaps two number without using temporary variable
*/
#include <stdio.h>
void main()
{
int a,b;
printf("Enter a and b");
scanf("%d",a);
scanf("%d",b);
printf("Before Swapping a=%d,b=%d"a,b);
a=a+b;
b=a-b;
a=a-b;
printf("After Swapping a=%d,b=%d"a,b);
}
/*
Input: Enter a and b
10 20
*/


Vowel Count in a String
/*
Summary: Find how many number of vowels are present in a string.
*/
#include<stdio.h>
int count_vowels(char []);
int check_vowel(char);
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;
}
/*
Input: Enter a string
This program is easy 2 understand
*/

COMMENTS

Name

11th,2,12th,20,12th Chemistry,5,12th Computer Science,7,12th Physics,1,5th Sem CSE,1,AAI ATC,2,Android,18,Banking,1,Blogger,42,Books,5,BTech,17,CBSE,22,CSE,4,ECE,3,Electronics,1,English,2,ESE,1,Ethical Hacking,61,Exams,5,Games,9,GATE,1,GATE ECE,1,Government Jobs,1,GS,1,How To,27,IBPS PO,1,Information,53,Internet,25,IPU,8,JEE,8,JEE Mains,8,Jobs,1,Linux,65,News,19,Notes,23,Physics,3,Placement,10,PO,1,Poetry,3,RRB,1,SEO,11,Softwares,38,SSC,2,SSC CGL,1,SSC GS,2,Tech News,1,Tips and Tricks,46,UPSC,1,Windows,46,
ltr
static_page
SolutionRider- One Stop Solution for Notes, Exams Prep, Jobs & Technical Blogs.: Programming in C
Programming in C
Programs:- Age(years) to Age(second) Converter. /*this program convert your age into seconds*/ #include int main() { float years,secs; printf("Input you age in yaers="); scanf("%f",&years); secs=years*365*24*60*60; printf("You have lived for about %f seconds\n ",secs); } Character to ASCII #include #include #include main() { char a; printf("Enter your character\n"); scanf("%c",&a); printf("Entered character ASCII value is=%d",a); getch(); } Data type Input Output #include #include main() { int i; float f; long double d; char t, c; printf("\nEnter the integer value=\n"); scanf("%d",&i); printf("\nInteger=%d\n",i); printf("\nEnter the float value=\n"); scanf("%f",&f); printf("\nFloat=\n%f",f); printf("\nEnter the double value=\n"); scanf("%f",&d); printf("\nDouble Value=\n%lf",d); printf("\nEnter the character=\n"); c=getche(); printf("\nCharatcter=\n%c",c); getch(); } Even Or Odd #include #include main() { int n; printf("Enter the number to know whether even or odd "); scanf("%d",&n); if(n%2==0) printf("Entered number is even "); else printf("Entered number is odd"); } Factorial Of a Number #include #include main() { int n,fact=1,i; printf("Enter the number=\n"); scanf("%d",&n); for(i=1;i<=n;i++) { fact=fact*i; } printf("\nFactorial of the given number is=%d",fact); } Grade And Marks Of Students #include #include main() { float a,b,c,d,t,avg; printf("Enter 4 subjects marks\n"); scanf("%f%f%f%f",&a,&b,&c,&d); t=(a+b+c+d); printf("Total Marks=%f\n",t); avg=t/4; printf("Average Marks=%f\n",avg); if(avg>=75) { printf("Grade=A"); } else if (avg>=60 && avg<75) { printf("Grade=B"); } else if (avg<60 && avg>=40) { printf("Grade=C"); } else printf("Grade=E"); } Herons Formula #include #include #include main() { int a,b,c; float s,area; printf("Enter the sides of triangle\n"); scanf("%d%d%d",&a,&b,&c); s=((a+b+c)/2); area=(sqrt(s*(s-a)*(s-b)*(s-c))); printf("\nArea=%f",area); getch(); } Largest Of Two #include #include main() { float a,b; printf("Enter the two numbers to find the largest among them="); scanf("%f%f",&a,&b); if(a>b) { printf("largest number is=%f",a); } else printf("largest number is=%f",b); } Leap Or Not #include #include main() { int y; printf("Enter the year you want to check="); scanf("%d",&y); if(y%4==0) { printf("It is a leap year"); } else printf("Not a leap year"); } Prime or not #include #include int main() { int n,i,temp=0; printf("Enter the number(+ve) ="); scanf("%d",&n); { for(i=2;i<=n/2;++i) { if(n%i==0) { temp=1; break; } } if (temp==0) printf("It is a prime number"); else printf("It is not a prime number"); } } Right Triangle Or Not #include #include #include int main() { float x,y,z; printf("Enter the sides of triangle =\n"); scanf("%f%f%f",&x,&y,z); if((x=sqrt(z^2+y^2))||(y=sqrt(x^2+y^2)||(x=sqrt(x^2+y^2))); printf("\nThe swapped number is=\n%d\n%d",x,y); getch(); } Swapping No Variable #include #include int main() { int x,y; printf("Enter the two number to be swapped=\n"); scanf("%d%d",&x,&y); x=x+y; y=x-y; x=x-y; printf("\nThe swapped number is=\n%d\n%d",x,y); getch(); } Temp Fahrenheit To Celsius /*This program convert temperature in degree celcius to Fahrenheit*/ #include #include int main() { float f,t; printf("Enter the temp in degree Fahrenheit="); scanf("%f",&f); t=(0.55*(f-32)); { printf("\nThe Temperature in degree celcius=%f",t); } getch(); } To Convert case #include #include #include main() { char t,c; printf("Enter the character ="); scanf("%c",&c); t=toupper(c); printf("The Uppercase of the entered character is=%c",t); } To print triangle #include #include main() { int n,i,j; printf("Enter the rows want in triangular form="); scanf("%d",&n); for(i=1;i<=n;i++) { printf("\n"); for(j=1;j<=i;j++) { printf("%d",j); } } } Alphabetical Sort /* Summary: Alphabetical sort is a program whereby strings of characters are placed in order based on the position of the characters in the conventional ordering of an alphabet. */ #include #include #include void main() { char name[10][8], tname[10][8], temp[8]; int i, j, n; printf("Enter the value of n \n"); scanf("%d", &n); printf("Enter %d names ", n); for (i = 0; i < n; i++) { scanf("%s", name[i]); strcpy(tname[i], name[i]); } for (i = 0; i < n - 1 ; i++) { for (j = i + 1; j < n; j++) { if (strcmp(name[i], name[j]) > 0) { strcpy(temp, name[i]); strcpy(name[i], name[j]); strcpy(name[j], temp); } } } printf("\n----------------------------------------\n"); printf("Input Names\tSorted names\n"); printf("------------------------------------------\n"); for (i = 0; i < n; i++) { printf("%s\t\t%s\n", tname[i], name[i]); } printf("------------------------------------------\n"); getch(); } /* Input: Enter the value of n 7 Enter 7 names heap stack queue object class program project */ Armstrong Number /* 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 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; } /* Input: Enter a number to check if it is an armstrong number 371 */ Binary search /* 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 void 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."); } getch(); } /* Input: Enter the value of num 5 Enter the elements one by one 23 90 56 15 58 Sorted array is... 15 23 56 58 90 Enter the element to be searched 58 */ Bubble Sort /* Summary: Bubble sort is a simple sorting algorithm that works by repeatedly stepping through the list to be sorted, comparing each pair of adjacent items and swapping them if they are in the wrong order. Complexity - O(n^2) */ #include #define MAXSIZE 10 void main() { int array[MAXSIZE]; int i, j, num, temp; 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]); } getch(); } /* Input: Enter the value of num 6 Enter the elements one by one 23 45 67 89 12 34 */ Calculation of Roots of Quadratic Equation /* 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 #include 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; } /* Input: Enter coefficients a, b and c: 4 1 0 */ Cock Tail Sort /* Summary: Cocktail sort is a variation of bubble sort that is both a stable sorting algorithm and a comparison sort. The algorithm differs from a bubble sort in that it sorts in both directions on each pass through the list. This sorting algorithm is only marginally more difficult to implement than a bubble sort, and solves the problem of turtles in bubble sorts. The various names of cocktail sort are bidirectional bubble sort, cocktail shaker sort, shaker sort, ripple sort, shuffle sort, shuttle sort or happy hour sort. Complexity - O(n^2) */ #include #define MAX 8 int main() { int data[MAX]; int i, j, n, c; printf("\nEnter the data"); for (i = 0; i < MAX; i++) { scanf("%d", &data[i]); } n = MAX; do { /*Rightward pass will shift the largest element to its correct place at the end*/ for (i = 0; i < n - 1; i++) { if (data[i] > data[i + 1]) { data[i] = data[i] + data[i + 1]; data[i + 1] = data[i] - data[i + 1]; data[i] = data[i] - data[i + 1]; } } n = n - 1; /* Leftward pass will shift the smallest element to its correct place at the beginning*/ for (i= MAX - 1, c = 0; i >= c; i--) { if(data[i] < data[i - 1]) { data[i] = data[i] + data[i - 1]; data[i - 1] = data[i] - data[i - 1]; data[i] = data[i] - data[i - 1]; } } c = c + 1; } while (n != 0 && c != 0); printf("The sorted elements are:"); for (i = 0; i < MAX; i++) { printf("%d\t", data[i]); } } /* Input: Enter the data 9 6 2 12 11 9 3 7 */ Compare String /* Summary: Compares two string for equality */ #include #include int main() { char a[100], b[100]; printf("Enter the first string\n"); gets(a); printf("Enter the second string\n"); gets(b); if( strcmp(a,b) == 0 ) printf("Entered strings are equal.\n"); else printf("Entered strings are not equal.\n"); return 0; } /* Input: simple sample */ Compound Interest Calculation /* Summary: Calculates compund interest */ #include #include #include void main() { float p,t,r; float i,compound,a; clrscr(); printf("enter principal:\t"); scanf("%d",&p); printf("enter rate of interest:\t"); scanf("%d",&r); printf("enter time in years:\t"); scanf("%d",&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); } getch(); } /* Input: enter principal: 100 enter rate of interest: 10 enter time in years: 1 */ Decimal to Binary Conversion /* Summary: Converts decimal number which is base 10 number system 0-9 to binary which is base 2 number system 0 and 1. */ #include int main(){ long int decimalNumber,remainder,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; } /* Input: Enter any decimal number: 50 */ Decimal to Hexadecimal Conversion /* 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 #include #include void main() { long int num; clrscr(); 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]); } } getch(); } /* Input: Enter the decimal number : 87274 */ Decimal to Octal Conversion /* 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 int main(){ long int decimalNumber,remainder,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; } /* Input: Enter any decimal number: 50 */ Depreciation Calculation /* Summary: Compute the yearly depreciation of the value of an item, given by Depreciation = (PURCHASE VALUE - SALVAGE VALUE) / YEAR OF SERVICE */ #include #include void main() { float sv,pv,dep; int yos; clrscr(); 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); getch(); } /* Input: Enter the value of purchase value :- 800 Enter the year of service :- 200 Enter the value of depreation :- 2 */ Even Odd Number /* Summary: Check whether a number entered by user is even or odd. */ #include 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; } /* Input: Enter an integer you want to check: 25 */ Factorial of a Number /* Summary: Factorial is represented using '!', so five factorial will be written as (5!),n factorial as (n!). n! = n*(n-1)*(n-2)*(n-3)...3.2.1 and zero factorial is defined as one i.e. 0! = 1. */ #include int main() { int c, n, fact = 1; printf("Enter a number to calculate it's factorial\n"); scanf("%d", &n); for (c = 1; c <= n; c++) fact = fact * c; printf("Factorial of %d = %d\n", n, fact); return 0; } /* Input: Enter a number to calculate it's factorial 6 */ Factorial of a Number using Recursion /* 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 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)); } /* Input: Enter a number: 5 */ Fahrenheit to Celsius Conversion /* Summary: Converts temperature from fahrenheit to degree celcius */ #include int main (void) { 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; } /* Input: Enter the temperature in degrees fahrenheit 100 */ Fibonacci Number Series /* Summary: Fibonacci Sequence is the series of numbers: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34,... */ #include int main() { int count, n, t1=0, t2=1, display=0; printf("Enter number of terms: "); scanf("%d",&n); /* Displaying first two terms */ printf("Fibonacci Series: %d+%d+", t1, t2); count=2; /* count=2 because first two terms are already displayed. */ while (count int main() { int n, i, c, a = 1; printf("Enter the number of rows of Floyd's triangle to print\n"); scanf("%d", &n); printf("Floyd's triangle\n"); for (i = 1; i <= n; i++) { for (c = 1; c <= i; c++) { printf("%d ",a); a++; } printf("\n"); } return 0; } /* Input: Enter the number of rows of Floyd's triangle to print 4 */ Gnome Sort /* Summary: Gnome sort(stupid sort) is a sorting algorithm which is similar to insertion sort, except that moving an element to its proper place is accomplished by a series of swaps, as in bubble sort. It is conceptually simple, requiring no nested loops. Complexity - O(n^2) */ #include void main() { int i, temp, ar[10], n; printf("\nenter the elemts number u would like to enter:"); scanf("%d", &n); printf("\nenter the elements to be sorted through gnome sort:\n"); for (i = 0; i < n; i++) scanf("%d", &ar[i]); i = 0; while (i < n) { if (i == 0 || ar[i - 1] <= ar[i]) i++; else { temp = ar[i-1]; ar[i - 1] = ar[i]; ar[i] = temp; i = i - 1; } } for (i = 0;i < n;i++) printf("%d\t", ar[i]); } /* Input: enter the elemts number u would like to enter:7 enter the elements to be sorted through gnome sort: 6 0 9 5 2 4 3 */ Harmonic Number Series /* Summary: Print sum of series 1 + 1/2 + 1/3 + 1/4 + ... + 1/N. */ #include #include void main() { double n,sum=0,i; clrscr(); 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/%d) ",i); else printf(" (1/%d) + ",i); } printf("\n\n THE SUM OF THIS SERIES IS %.2lf",sum); getch(); } /* Input: Please Give The Value of N: 5 */ Heap Sort /* Summary: The heapsort algorithm can be divided into two parts. -Step 1: a heap is built out of the data. -Step 2: a sorted array is created by repeatedly removing the largest element from the heap, and inserting it into the array. The heap is reconstructed after each removal. Once all objects have been removed from the heap, we have a sorted array. The direction of the sorted elements can be varied by choosing a min-heap or max-heap in step one. Complexity: O(n•log n) */ #include void main() { int heap[10], no, i, j, c, root, temp; clrscr(); printf("\n Enter no of elements :"); scanf("%d", &no); printf("\n Enter the nos : "); for (i = 0; i < no; i++) scanf("%d", &heap[i]); for (i = 1; i < no; i++) { c = i; do { root = (c - 1) / 2; if (heap[root] < heap[c]) /* to create MAX heap array */ { temp = heap[root]; heap[root] = heap[c]; heap[c] = temp; } c = root; } while (c != 0); } printf("Heap array : "); for (i = 0; i < no; i++) printf("%d\t ", heap[i]); for (j = no - 1; j >= 0; j--) { temp = heap[0]; heap[0] = heap[j]; /* swap max element with rightmost leaf element */ heap[j] = temp; root = 0; do { c = 2 * root + 1; /* left node of root element */ if ((heap[c] < heap[c + 1]) && c < j-1) c++; if (heap[root] void main() { printf("Hello world!"); } Insertion Sort /* Summary: Insertion sort iterates, consuming one input element each repetition, and growing a sorted output list. Each iteration, insertion sort removes one element from the input data, finds the location it belongs within the sorted list, and inserts it there. It repeats until no input elements remain. Complexity - O(n^2) */ #include #define MAX 10 void insertion_sort(int *); void main() { int a[MAX], i; clrscr(); printf("enter 10 elements to be sorted:"); for (i = 0;i < MAX;i++) { scanf("%d", &a[i]); } insertion_sort(a); printf("sorted elements:\n"); for (i = 0;i < MAX; i++) { printf(" %d", a[i]); } getch(); } /* sorts the input */ void insertion_sort(int * x) { int temp, i, j; for (i = 1;i < MAX;i++) { temp = x[i]; j = i - 1; while (temp < x[j] && j >= 0) { x[j + 1] = x[j]; j = j - 1; } x[j + 1] = temp; } } /* Input: enter elements to be sorted:8 2 4 9 3 6 1 */ Largest Number /* Summary: Finds largest number from array and its location */ #include 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; } /* Input: Enter the number of elements in array 5 Enter 5 integers 4 5 6 8 2 */ Letter Count of a String /* Summary: Counts frequency of letters in given string */ #include #include 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; } /* Input: ABCAPPPRC */ Linear Search /* 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 void 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"); getch(); } /* Input: Enter the value of num 5 Enter the elements one by one 456 78 90 40 100 Enter the element to be searched 40 */ Linked List Data Structure /* Summary: Linked list is an ordered set of data elements, each containing a link to its successor. This program is to create a linked list. After creating the linked list ,the length of linked list is calculated. */ #include #include #define newnode (struct node*) malloc(sizeof(struct node)) struct node { int data; struct node *next; }; struct node *create_list(); void main() { struct node *f; int len; f = NULL; clrscr(); f = create_list(); len = find_len(f); printf("\n length = %d",len); } // main struct node *create_list() { struct node *f,*c,*p; int tdata; f = NULL; printf("\n Enter data ( use 0 to exit ) : "); scanf("%d",&tdata); while( tdata != 0 ) { c = newnode; if( c == NULL) { printf("\n Insuf. mem. "); exit(0); } c->data = tdata; c->next = NULL; if( f== NULL) f = c; else p->next = c; p = c; printf("\n Enter data ( use 0 to exit ) : "); scanf("%d",&tdata); } //while return(f); } // create list int find_len(struct node *f) { int len=0; struct node *t; if( f == NULL) return(0); t = f; while( t != NULL ) { len++; t = t->next; } return(len); } /* Input: Enter data ( use 0 to exit ) : 5 Enter data ( use 0 to exit ) : 15 Enter data ( use 0 to exit ) : 65 Enter data ( use 0 to exit ) : 2 Enter data ( use 0 to exit ) : 657 Enter data ( use 0 to exit ) : 3 Enter data ( use 0 to exit ) : 0 */ Merge Sort /* Summary: Merge sort is a divide and conquer comparison-based sorting algorithm. It works as follows:- 1. Divide the unsorted list into n sublists, each containing 1 element (a list of 1 element is considered sorted). 2. Repeatedly merge sublists to produce new sublists until there is only 1 sublist remaining. This will be the sorted list. Complexity - O(n•log n) */ #include void mergeSort(int [], int, int, int); void partition(int [],int, int); int main() { int list[50]; int i, size; clrscr(); printf("Enter total number of elements:"); scanf("%d", &size); printf("Enter the elements:\n"); for(i = 0; i < size; i++) { scanf("%d", &list[i]); } partition(list, 0, size - 1); printf("After merge sort:\n"); for(i = 0;i < size; i++) { printf("%d ",list[i]); } getch(); return 0; } void partition(int list[],int low,int high) { int mid; if(low < high) { mid = (low + high) / 2; partition(list, low, mid); partition(list, mid + 1, high); mergeSort(list, low, mid, high); } } void mergeSort(int list[],int low,int mid,int high) { int i, mi, k, lo, temp[50]; lo = low; i = low; mi = mid + 1; while ((lo <= mid) && (mi <= high)) { if (list[lo] <= list[mi]) { temp[i] = list[lo]; lo++; } else { temp[i] = list[mi]; mi++; } i++; } if (lo > mid) { for (k = mi; k <= high; k++) { temp[i] = list[k]; i++; } } else { for (k = lo; k <= mid; k++) { temp[i] = list[k]; i++; } } for (k = low; k <= high; k++) { list[k] = temp[k]; } } /* Input: Enter total number of elements:5 Enter the elements: 12 36 22 76 54 */ Palindrome String /* Summary: Palindromes are words or phrases that read the same in both directions. */ #include #include #include char *strrev(char *str) { char *p1, *p2; if (! str || ! *str) return str; for (p1 = str, p2 = str + strlen(str) - 1; p2 > p1; ++p1, --p2) { *p1 ^= *p2; *p2 ^= *p1; *p1 ^= *p2; } return str; } int main() { char a[100], b[100]; //Get the number from user. printf("Enter the string to check if it is a palindrome\n"); gets(a); //copy input to another array b strcpy(b,a); //Reverse it. strrev(b); /*Compare it with the number entered by the user.If both are same then print palindrome number*/ if( strcmp(a,b) == 0 ) printf("\nEntered string is a palindrome.\n"); else printf("\nEntered string is not a palindrome.\n"); getch(); return 0; } /* Input: radar */ Pascal number triangle pattern /* Summary: Pascal triangle is associated with Binomial Theorem in Mathematics. */ #include long factorial(int n) { int c; long result = 1; for( c = 1 ; c <= n ; c++ ) result = result*c; return ( result ); } int main() { int i, n, c; printf("Enter the number of rows you wish to see in pascal triangle\n"); scanf("%d",&n); for ( i = 0 ; i < n ; i++ ) { for ( c = 0 ; c <= ( n - i - 2 ) ; c++ ) printf(" "); for( c = 0 ; c <= i ; c++ ) printf("%ld ",factorial(i)/(factorial(c)*factorial(i-c))); printf("\n"); } return 0; } /* Input: Enter the no. of lines: 8 */ Prime Number /* Summary: Check whether a number is prime or not */ #include 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; } /* Input: Enter a positive integer: 29 */ Print half pyramid pattern using asterix /* Summary: Program prints half a pyramid using * character */ #include main() { int n, c, k; printf("Enter number of rows\n"); scanf("%d",&n); for ( c = 1 ; c <= n ; c++ ) { for( k = 1 ; k <= c ; k++ ) printf("* "); printf("\n"); } return 0; } /* Input: Enter number of rows 4 */ Print triangle pattern using asterix /* Summary: Prints triangle using * */ #include main() { int n, c, k = 2, j; printf("Enter number of rows\n"); scanf("%d",&n); for ( j = 1 ; j <= n ; j++ ){ for ( c = 1 ; c <= 2*n-k ; c++) printf(" "); k = k + 2; for ( c = 1 ; c <= j ; c++) printf("* "); printf("\n"); } getch(); return 0; } /* Input: Enter number of rows 4 */ Queue Data Structure /* Summary: Queue is a data structure which works as FIFO principle. FIFO means “First in First out”, i.e the element which we have inserted first will be deleted first and the element that we have inserted last will be deleted last.Two variables are used to implement queue, i.e “rear” and “front”. Insertion will be done at rear side and deletion will be performed at front side. */ #include #define MAX 50 int queue_array[MAX]; int rear = - 1; int front = - 1; main() { int choice; while (1) { printf("1.Insert element to queue \n"); printf("2.Delete element from queue \n"); printf("3.Display all elements of queue \n"); printf("4.Quit \n"); printf("Enter your choice : "); scanf("%d", &choice); switch (choice) { case 1: insert(); break; case 2: delete(); break; case 3: display(); break; case 4: exit(1); default: printf("Wrong choice \n"); } } } insert() { int add_item; if (rear == MAX - 1) printf("Queue Overflow \n"); else { if (front == - 1) /*If queue is initially empty */ front = 0; printf("Inset the element in queue : "); scanf("%d", &add_item); rear = rear + 1; queue_array[rear] = add_item; } } delete() { if (front == - 1 || front > rear) { printf("Queue Underflow \n"); return ; } else { printf("Element deleted from queue is : %d\n", queue_array[front]); front = front + 1; } } display() { int i; if (front == - 1) printf("Queue is empty \n"); else { printf("Queue is : \n"); for (i = front; i <= rear; i++) printf("%d ", queue_array[i]); printf("\n"); } } /* Input: //Enter choices and data */ Quick Sort /* Summary: Quicksort is a very efficient sorting algorithm.It has two phases: -the partition phase and -the sort phase. Most of the work is done in the partition phase - it works out where to divide the work. The sort phase simply sorts the two smaller problems that are generated in the partition phase. Complexity: O(n•log n) */ #include void quicksort (int [], int, int); int main() { int list[50]; int size, i; clrscr(); printf("Enter the number of elements: "); scanf("%d", &size); printf("Enter the elements to be sorted:\n"); for (i = 0; i < size; i++) { scanf("%d", &list[i]); } quicksort(list, 0, size - 1); printf("After applying quick sort\n"); for (i = 0; i < size; i++) { printf("%d ", list[i]); } printf("\n"); getch(); return 0; } void quicksort(int list[], int low, int high) { int pivot, i, j, temp; if (low < high) { pivot = low; i = low; j = high; while (i < j) { while (list[i] <= list[pivot] && i <= high) { i++; } while (list[j] > list[pivot] && j >= low) { j--; } if (i < j) { temp = list[i]; list[i] = list[j]; list[j] = temp; } } temp = list[j]; list[j] = list[pivot]; list[pivot] = temp; quicksort(list, low, j - 1); quicksort(list, j + 1, high); } } /* Input: Enter the number of elements: 6 Enter the elements to be sorted: 67 45 24 98 12 38 */ Radix Sort /* Summary: Radix sort is a non-comparative integer sorting algorithm that sorts data with integer keys by grouping keys by the individual digits which share the same significant position and value. Two classifications of radix sorts are least significant digit (LSD) radix sorts and most significant digit (MSD) radix sorts. LSD radix sorts process the integer representations starting from the least digit and move towards the most significant digit. MSD radix sorts work the other way around. Complexity - O(d•n) */ #include int min = 0, count = 0, array[100] = {0}, array1[100] = {0}; void main() { int k, i, j, temp, t, n; clrscr(); printf("Enter size of array :"); scanf("%d", &count); printf("Enter elements into array :"); for (i = 0; i < count; i++) { scanf("%d", &array[i]); array1[i] = array[i]; } for (k = 0; k < 3; k++) { for (i = 0; i < count; i++) { min = array[i] % 10; /* To find minimum lsd */ t = i; for (j = i + 1; j < count; j++) { if (min > (array[j] % 10)) { min = array[j] % 10; t = j; } } temp = array1[t]; array1[t] = array1[i]; array1[i] = temp; temp = array[t]; array[t] = array[i]; array[i] = temp; } for (j = 0; j < count; j++) /*to find MSB */ array[j] = array[j] / 10; } printf("Sorted Array (lSdradix sort) : "); for (i = 0; i < count; i++) printf("%d ", array1[i]); getch(); } /* Input: Enter size of array :7 Enter elements into array :170 45 90 75 802 24 2 */ Reverse String /* Summary: Reverses a given string */ #include #include char *strrev(char *str) { char *p1, *p2; if (! str || ! *str) return str; for (p1 = str, p2 = str + strlen(str) - 1; p2 > p1; ++p1, --p2) { *p1 ^= *p2; *p2 ^= *p1; *p1 ^= *p2; } return str; } int main() { char arr[100]; printf("Enter a string to reverse\n"); gets(arr); strrev(arr); printf("Reverse of entered string is \n%s\n",arr); return 0; } /* Input: John */ Selection Sort /* Summary: Selection sort divides the input list into two parts: the sublist of items already sorted and the sublist of items remaining to be sorted. It proceeds by finding the smallest (or largest) element in the unsorted sublist, exchanging it with the leftmost unsorted element and moving the sublist boundaries one element to the right. Complexity - O(n^2) */ #include #include #define max 50 void selsort(int, int[]); void main() { int i, size, data[max]; clrscr(); printf("\nEnter no of Elements:"); scanf("%d",&size); printf("\nEnter Elements:"); for(i=1;i<=size;i++) scanf("%d",&data[i]); printf("\nUnsorted data:\n"); for(i=1;i<=size;i++) printf("%d\t",data[i]); selsort(size, data); getch(); } void selsort(int n, int data[]) { int i, j, min, temp; printf("\nSorted List is:\n"); for(i=1;i<=n-1;i++) { min = i; for(j=i+1;j<=n;j++) { if(data[j] #define size 10 /* Function Prototype */ int shell_sort(int []); void main() { int arr[size], i; clrscr(); printf("Enter 10 elements to be sorted:"); for (i = 0;i < size;i++) { scanf("%d", &arr[i]); } shell_sort(arr); printf("The array after sorting is:"); for (i = 0;i < size;i++) { printf("\n%d", arr[i]); } getch(); } /* Code to sort array using shell sort */ int shell_sort(int array[]) { int i = 0, j = 0, k = 0, mid = 0; for (k = size / 2;k > 0;k /= 2) { for (j = k;j < size;j++) { for (i = j - k;i >= 0;i -= k) { if (array[i + k] >= array[i]) { break; } else { mid = array[i]; array[i] = array[i + k]; array[i + k] = mid; } } } } return 0; } /* Input: Enter the elements to be sorted:57 67 48 93 42 84 95 */ Simple Calculator /* Summary: Does addition subtraction multiplication division */ # include int main() { char operator; float num1,num2; printf("Enter operator either + or - or * or divide : "); 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; } /* Input: Enter operator either + or - or * or divide: / Enter two operands: 13.456 4.56 */ Stack Data Structure /* Summary: Stack is a particular kind of abstract data type or collection. Theonly operations on a stack are the addition of an entity, known as push and removal of an entity, known as pop.The relation between the push and pop operations is such that the stack is a Last-In-First-Out (LIFO) data structure. */ #include #include #define max 50 void push(); void pop(); void display(); int menu(); int stack[max], top=0; void main() { int ch; clrscr(); do{ ch=menu(); switch(ch) { case 1: push(); break; case 2: pop(); break; case 3: display(); break; case 4: exit(1); default: printf("\nEnter a valid choice!!"); } }while(1); } int menu() { int ch; printf("\nStack"); printf("\n1.Push\n2.Pop\n3.Display\n4.Exit"); printf("\nEnter your Choice:"); scanf("%d",&ch); return ch; } void push() { if(top==max) printf("\nOverflow"); else { int element; printf("\nEnter Element:"); scanf("%d",&element); printf("\nElement(%d) has been pushed at %d", element, top); stack[top++]=element; } } void pop() { if(top==-1) printf("\nUnderflow"); else { top--; printf("\nELement has been popped out!"); } } void display() { if(top==0) printf("\nStack is Empty!!"); else { int i; for(i=0;i #include int main() { char a[100], b[100]; printf("Enter the first string\n"); gets(a); printf("Enter the second string\n"); gets(b); strcat(a,b); printf("String obtained on concatenation is %s\n",a); return 0; } /* Input: Enter first string: lol Enter second string: :) */ Sum of numbers between 100 and 200 divisible by 7 /* Summary: Sum of numbers between 100 and 200 which are divisible by 7 */ #include #include void main() { int i,sum = 0; clrscr(); for(i = 100;i <= 200 ; i++) { if (i % 7 == 0) sum = sum + i; } printf("Sum of all no between 100 and 200 "); printf("which is divisible by 7 is :: %d",sum); getch(); } /* Input: No input */ Swap two numbers without using third /* Summary: Program swaps two number without using temporary variable */ #include void main() { int a,b; printf("Enter a and b"); scanf("%d",a); scanf("%d",b); printf("Before Swapping a=%d,b=%d"a,b); a=a+b; b=a-b; a=a-b; printf("After Swapping a=%d,b=%d"a,b); } /* Input: Enter a and b 10 20 */ Vowel Count in a String /* Summary: Find how many number of vowels are present in a string. */ #include int count_vowels(char []); int check_vowel(char); 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; } /* Input: Enter a string This program is easy 2 understand */
SolutionRider- One Stop Solution for Notes, Exams Prep, Jobs & Technical Blogs.
https://thesolutionrider.blogspot.com/p/programming-in-c.html
https://thesolutionrider.blogspot.com/
https://thesolutionrider.blogspot.com/
https://thesolutionrider.blogspot.com/p/programming-in-c.html
true
6820083649286484786
UTF-8
Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS CONTENT IS PREMIUM Please share to unlock Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy