--------------------------------------------------------------
1) Print Hello world
--------------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
puts("Hello World!!");
/* prints Hello World!! */
return EXIT_SUCCESS;
}
-------------------------------------------------------------
2) For Loop
-------------------------------------------------------------
#include <stdio.h>
-------------------------------------------------------------
2) For Loop
-------------------------------------------------------------
#include <stdio.h>
int main()
{
int i;
/* The loop goes while i < 10, and i increases by one every loop*/
for ( i = 0; i < 10; i++ )
{
/* Keep in mind that the loop condition checks the conditional statement before it loops again. consequently, when i equals 10 the loop breaks.iis updated before the condition is checked. */
/* Keep in mind that the loop condition checks the conditional statement before it loops again. consequently, when i equals 10 the loop breaks.iis updated before the condition is checked. */
printf( "%d\n", i );
}
getchar();
}
-------------------------------------------------------------
3) If else statement
-------------------------------------------------------------
#include <stdio.h>
}
-------------------------------------------------------------
3) If else statement
-------------------------------------------------------------
#include <stdio.h>
int main()
{
int number;
printf("Please enter a number: ");
scanf("%d", &number);
if (number < 100)
{
printf("Number is less than 100!\n");
}
{
printf("Number is less than 100!\n");
}
else if (number == 100)
{
printf("Number is 100!!\n");
}
else
{
printf("Number is greater than 100!\n");
}
return 0;
}
-------------------------------------------------------------
4) While loop
-------------------------------------------------------------
#include <stdio.h>
}
return 0;
}
-------------------------------------------------------------
4) While loop
-------------------------------------------------------------
#include <stdio.h>
int main()
{
/* Don't forget to declare variables */
/* Don't forget to declare variables */
int i = 0;
/* While x is less than 10 */
while (i < 10)
{
printf("%d\n", i);
/* Update x so the condition can be met eventually */
i++;
}
getchar();
}
-------------------------------------------------------------
5) Print an Integer
-------------------------------------------------------------
#include <stdio.h>
int main()
{
{
int a;
printf("Enter an integer\n");
scanf("%d", &a);
printf("Integer that you have entered is %d\n", a);
return 0;
}
-------------------------------------------------------------
6) Factorial of a number
-------------------------------------------------------------
#include <stdio.h>;
long int factorial(int n);
void main()
{
int n;
printf("Enter the number:\n");
scanf("%d", &n);
printf("Factorial of %d is %d", n, factorial(n));
getch();
}
long int factorial(int n)
{
if (n <= 1)
{
return (1);
}
else
{
n = n * factorial(n - 1);
return (n);
}
}
-------------------------------------------------------------
7) Check if Number is prime or not
-------------------------------------------------------------
#include<stdio.h>
main()
{
int n, c = 2;
printf("Enter a number to check if it is prime\n");
scanf("%d", &n);
scanf("%d", &n);
for (c = 2; c <= n - 1; c++)
{
if (n % c == 0)
printf("%d is not prime.\n", n);
break;
}
}
break;
}
}
if (c == n)
printf("%d is prime.\n", n);
return 0;
}
-------------------------------------------------------------
8) Check if alphabet is vowel
-------------------------------------------------------------
#include <stdio.h>
int main()
{
char ch;
printf("Enter a character\n");
scanf("%c", &ch);
scanf("%c", &ch);
if (ch == 'a' || ch == 'A' || ch == 'e' || ch == 'E' || ch == 'i'|| ch == 'I' || ch == 'o' || ch == 'O' || ch == 'u' || ch == 'U')
printf("%c is a vowel.\n", ch);
elseprintf("%c is not a vowel.\n", ch);
return 0;
}
-------------------------------------------------------------
9) Fibonacci series
-------------------------------------------------------------
#include <stdio.h>;
void main()
{
int a, b, c, i, n;a = 0;b = 1;
printf("Enter a number to define the length of fibonacci series: ");
scanf("%d", &n);
printf("\nThe Series is: \n");
printf("%d\t%d", a, b);
for (i = 0; i < n; i++)
{
c = a + b;
a = b;
b = c;
a = b;
b = c;
printf("\t%d", c);
}
getch();
}
-------------------------------------------------------------
10) Palindrome
-------------------------------------------------------------
#include <stdio.h>
}
-------------------------------------------------------------
10) Palindrome
-------------------------------------------------------------
#include <stdio.h>
int main()
{
int n, reverse = 0, temp;
int n, reverse = 0, temp;
printf("Enter a number to check if it is a palindrome or not\n");
scanf("%d",&n);
scanf("%d",&n);
temp = n;
while( temp != 0 )
{
reverse = reverse * 10;
reverse = reverse + temp%10;
temp = temp/10;
}
if ( n == reverse )
printf("%d is a palindrome number.\n", n);
else
printf("%d is not a palindrome number.\n", n);
return 0;
}
-------------------------------------------------------------
11) Pattern 1
-------------------------------------------------------------
/*
*
**
***
****
******
*/
#include <stdio.h>
}
-------------------------------------------------------------
11) Pattern 1
-------------------------------------------------------------
/*
*
**
***
****
******
*/
#include <stdio.h>
int 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;
}
-------------------------------------------------------------
12)Pattern 2
-------------------------------------------------------------
/*
*
*A*
*A*A*
*A*A*A*
*/
#include<stdio.h>
main()
{
int n, c, k, space, count = 1;
printf("Enter number of rows\n");
scanf("%d", &n);
space = n;
for (c = 1; c <= n; c++)
{
for (k = 1; k < space; k++)
printf(" ");
for (k = 1; k <= c; k++)
{
printf("*");
{
printf("*");
if (c > 1 && count < c)
{
printf("A");
count++;
}
}
printf("\n");
space--;
count = 1;
}
return 0;
}
-------------------------------------------------------------
13) Number pyramid
-------------------------------------------------------------
/*
1
232
34543
4567654
567898765
*/
567898765
*/
#include<stdio.h>
main()
{
int n, c, d, num = 1, space;
printf("Enter the number of rows ");
scanf("%d", &n);
space = n - 1;
for (d = 1; d <= n; d++)
{
num = d;
for (c = 1; c <= space; c++)
printf(" ");space--;
for (c = 1; c <= d; c++)
{
printf("%d", num);
num++;
}
num--;
num--;
for (c = 1; c < d; c++)
{
printf("%d", num);
num--;
}
printf("\n");
}
return 0;
}
-------------------------------------------------------------
-------------------------------------------------------------
14) Pyramid
-------------------------------------------------------------
/*
*
***
*****
*******
*********
*/
#include <stdio.h>
int main()
{
int row, c, n, temp;
printf("Enter the number of rows in pyramid of stars you wish to see ");
scanf("%d", &n);
temp = n;
for (row = 1; row <= n; row++)
{
for (c = 1; c < temp; c++)
printf(" ");
temp--;
for (c = 1; c <= 2 * row - 1; c++)
printf("*");
printf("\n");
}
return 0;
}
-------------------------------------------------------------
15) Diamond
-------------------------------------------------------------
/*
*
***
*****
***
*
*/
#include <stdio.h>
int main()
{
int n, c, k, space = 1;
printf("Enter number of rows\n");
scanf("%d", &n);space = n - 1;
for (k = 1; k <= n; k++)
{
for (c = 1; c <= space; c++)
printf(" ");
space--;
for (c = 1; c <= 2 * k - 1; c++)
printf("*");
printf("\n");
}
space = 1;
for (k = 1; k <= n - 1; k++)
{
for (c = 1; c <= space; c++)
printf(" ");space++;
for (c = 1; c <= 2 * (n - k) - 1; c++)
printf("*");
printf("\n");
}
return 0;
}
-------------------------------------------------------------
16) Pascal triangle
-------------------------------------------------------------
#include <stdio.h>;
long fact(int);
int main()
{
int line, i, j;
printf("Enter the no. of lines: ");
scanf("%d", &line);
for (i = 0; i < line; i++)
{
for (j = 0; j < line - i - 1; j++)
{
printf(" ");
}
for (j = 0; j <= i; j++)
{
printf("%ld ", fact(i) / (fact(j) * fact(i - j)));
}
printf("\n");
}
return 0;
}
long fact(int num) {long f = 1;
int i = 1;
while (i <= num)
{
f = f * i;
i++;
}
return f;
}
-------------------------------------------------------------
17) Floyd's triangle
-------------------------------------------------------------
#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);
for (i = 1; i <= n; i++)
{
for (c = 1; c <= i; c++)
{
printf("%d ", a);
a++;
}
printf("\n");
}
return 0;
}
-------------------------------------------------------------
18) Even or odd
-------------------------------------------------------------
#include<stdio.h>
main()
{
int n;
printf("Enter an integer\n");
scanf("%d", &n);if ((n / 2) * 2 == n)
printf("Even\n");
elseprintf("Odd\n");
return 0;
}
-------------------------------------------------------------
19) Sort array in ascending order
-------------------------------------------------------------
/** C program to accept N numbers and arrange them in an ascending order*/
#include <stdio.h>
void main()
{
int i, j, a, n, number[30];
printf("Enter the value of N \n");
scanf("%d", &n);
printf("Enter the numbers \n");
for (i = 0; i < n; ++i)
{
scanf("%d", &number[i]);
}
for (i = 0; i < n; ++i)
{
for (j = i + 1; j < n; ++j)
{
if (number[i] > number[j])
{
a = number[i];
number[i] = number[j];
number[j] = a;
}
}
}
printf("The numbers arranged in ascending order are given below \n");
for (i = 0; i < n; ++i)
{
printf("%d\n", number[i]);
}
}
-------------------------------------------------------------
20) Sort array in descending order
-------------------------------------------------------------
/** C program to accept a set of numbers and arrange them* in a descending order*/
#include <stdio.h>
void main()
{
int number[30];
int i, j, a, n;
printf("Enter the value of N\n");
scanf("%d", &n);
printf("Enter the numbers \n");
for (i = 0; i < n; ++i)
{
scanf("%d", &number[i]);
}
/* sorting begins ... */
for (i = 0; i < n; ++i)
{
for (j = i + 1; j < n; ++j)
{
if (number[i] < number[j])
{
a = number[i];
number[i] = number[j];
number[j] = a;
}
}
}
printf("The numbers arranged in descending order are given below\n");
for (i = 0; i < n; ++i)
{
printf("%d\n", number[i]);
}
}
-------------------------------------------------------------
21) Sort names alphabetically
-------------------------------------------------------------
/** C program to read N names, store them in the form of an array* and sort them in alphabetical order. Output the given names and* the sorted names in two columns side by side.*/
#include <stdio.h>
#include <string.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", 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 NamestSorted names\n");
printf("---------------------- --------------------\n");
for (i = 0; i < n; i++)
{
printf("%s\t\t%s\n", tname[i], name[i]);
}
printf("---------------------- --------------------\n");
}
-------------------------------------------------------------
22) Add 'n' Numbers
-------------------------------------------------------------
#include <stdio.h>
int main()
{
int n, sum = 0, c, value;
printf("Enter the number of integers you want to add\n");
scanf("%d", &n);
printf("Enter %d integers\n",n);
for (c = 1; c <= n; c++)
{
scanf("%d",&value);
sum = sum + value;
}
printf("Sum of entered integers = %d\n",sum);
return 0;
}
-------------------------------------------------------------
23) Add'n' Numbers using Array
-------------------------------------------------------------
#include <stdio.h>
int main()
{
int n, sum = 0, c, array[100];
printf("Enter the number of integers you want to add\n");
scanf("%d", &n);
printf("Enter %d integers\n",n);
for (c = 0; c < n; c++)
{
scanf("%d", &array[c]);
sum = sum + array[c];
}
printf("Sum = %d\n",sum);
return 0;
}
-------------------------------------------------------------
24) Add Digits of a Number
-------------------------------------------------------------
#include <stdio.h>
int main()
{
int n, sum = 0, remainder;
printf("Enter an integer\n");
scanf("%d", &n);while (n != 0) {remainder = n % 10;
sum = sum + remainder;
n = n / 10;
}
printf("Sum of digits of entered number = %d\n", sum);
return 0;
}
-------------------------------------------------------------
25) Add Digits of a Number using Recursion
-------------------------------------------------------------
#include <stdio.h>
int add_digits(int);
int main()
{
int n, result;
printf("Enter a number\n");
scanf("%d", &n);
result = add_digits(n);
printf("%d\n", result);
return 0;
}
int add_digits(int n)
{
static int sum = 0;
if (n == 0)
{
return 0;
}
sum = n % 10 + add_digits(n / 10);
return sum;
}
-------------------------------------------------------------
26) Simple pointer example
-------------------------------------------------------------
#include <stdio.h>
int main ()
{
int var = 20; /* actual variable declaration */
int *ip; /* pointer variable declaration */
ip = &var; /* store address of var in pointer variable*/
printf("Address of var variable: %x\n", &var ); /* address stored in pointer variable */
printf("Address stored in ip variable: %x\n", ip ); /* access the value using the pointer */
printf("Value of *ip variable: %d\n", *ip );
return 0;
}
-------------------------------------------------------------
27) Add two numbers using Pointers
-------------------------------------------------------------
#include <stdio.h>
int main()
{
int first, second, *p, *q, sum;
printf("Enter two integers to add\n");
scanf("%d%d", &first, &second);
p = &first;
q = &second;
sum = *p + *q;
printf("Sum of entered numbers = %d\n",sum);
return 0;
}
-------------------------------------------------------------
28) Add, Subtract, Multiply & Divide
-------------------------------------------------------------
#include <stdio.h>
int main()
{
int first, second, add, subtract, multiply;
float divide;
printf("Enter two integers\n");
scanf("%d%d", &first, &second);
add = first + second;
subtract = first - second;
multiply = first * second;
divide = first / (float)second; //typecasting
printf("Sum = %d\n",add);
printf("Difference = %d\n",subtract);
printf("Multiplication = %d\n",multiply);
printf("Division = %.2f\n",divide);
return 0;
}
-------------------------------------------------------------
29) Armstrong Number
-------------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
int main()
{
//An Armstrong number is a number such that the sum//! of its digits raised to the third power is equal to the number//! itself.
For example, 371 is an Armstrong number,
since//! 3**3 + 7**3 + 1**3 = 371.int a, n, b = 0, t;
printf("Enter the no: ");
scanf("%d", &n);t = n;
while (n > 0)
{
a = n % 10;
b = b + a * a * a;
n = n / 10;
}
if (b == t)
{
printf("Its an Armstrong number");
}
else
{
printf("Its Not an Armstrong number");
}
getch();
return 0;
}
-------------------------------------------------------------
30) Array of char Pointer
-------------------------------------------------------------
#include <stdio.h>
const int MAX = 4;
int main()
{
char *names[] = { "Google", "Amazon", "Toshiba", "Sony", };
int i = 0;
for (i = 0; i < MAX; i++)
{
printf("Value of names[%d] = %s\n", i, names[i]);
}
return 0;
}
-------------------------------------------------------------
31) Array of Struct
-------------------------------------------------------------
#include<stdio.h>
struct st
{
int a;
char c;
};
int main()
{
struct st st_arr[3]; // Declare an array of 3 structure objects
struct st st_obj0; // first structure object
st_obj0.a = 0;
st_obj0.c = 'a';
struct st st_obj1; //Second structure object
st_obj1.a = 1;
st_obj1.c = 'b';
struct st st_obj2; // Third structure object
st_obj2.a = 2;
st_obj2.c = 'c';
st_arr[0] = st_obj0; // Initializing first element of array with first structure object
st_arr[1] = st_obj1; // Initializing second element of array with second structure object
st_arr[2] = st_obj2; // Initializing third element of array with third structure object
printf("\n First Element of array has values of a = [%d] and c = [%c]\n",
st_arr[0].a, st_arr[0].c);
printf("\n Second Element of array has values of a = [%d] and c = [%c]\n",
st_arr[1].a, st_arr[1].c);
printf("\n Third Element of array has values of a = [%d] and c = [%c]\n",
st_arr[2].a, st_arr[2].c);
return 0;
}
-------------------------------------------------------------
32) Array of int Pointer
-------------------------------------------------------------
#include <stdio.h>
const int MAX = 3;
int main()
{
int var[] = { 10, 100, 200 };
int i, *ptr[MAX];
for (i = 0; i < MAX; i++)
{
ptr[i] = &var[i];
/* assign the address of integer. */
}
for (i = 0; i < MAX; i++)
{
printf("Value of var[%d] = %d\n", i, *ptr[i]);
}
return 0;
}
-------------------------------------------------------------
33) Bubble Sort
-------------------------------------------------------------
#include <stdio.h>
int main()
{
int array[100], n, c, d, swap;
printf("Enter number of elements\n");
scanf("%d", &n);
printf("Enter %d integers\n", n);
for (c = 0; c < n; c++)
scanf("%d", &array[c]);
for (c = 0; c < (n - 1); c++)
{
for (d = 0; d < n - c - 1; d++)
{
if (array[d] > array[d + 1]) /* For decreasing order use < */
{
swap = array[d];
array[d] = array[d + 1];
array[d + 1] = swap;
}
}
}
printf("Sorted list in ascending order:\n");
for (c = 0; c < n; c++)
printf("%d\n", array[c]);
return 0;
}
-------------------------------------------------------------
34) Swap two number
-------------------------------------------------------------
#include <stdio.h>
int main()
{
int x, y, temp;
printf("Enter the value of x and y\n");
scanf("%d%d", &x, &y);
printf("Before Swapping\nx = %d\ny = %d\n", x, y);
temp = x;x = y;y = temp;
printf("After Swapping\nx = %d\ny = %d\n", x, y);
return 0;
}
-------------------------------------------------------------
35) Sum and average of all number
-------------------------------------------------------------
/** C program to read N integers into an array A and*
a) Find the sum of negative numbers*
b) Find the sum of positive numbers*
c) Find the average of all numbers*
Display the results with suitable headings*/
#include <stdio.h>
#define MAXSIZE 10
void main()
{
int array[MAXSIZE];
int i, num, negative_sum = 0, positive_sum = 0;
float total = 0.0, average;
printf ("Enter the value of N \n");
scanf("%d", &num);
printf("Enter %d numbers (-ve, +ve and zero) \n", num);
for (i = 0; i < num; i++)
{
scanf("%d", &array[i]);
}
printf("Input array elements \n");
for (i = 0; i < num; i++)
{
printf("%+3d\n", array[i]);
}
/* Summation starts */
for (i = 0; i < num; i++)
{
if (array[i] < 0)
{
negative_sum = negative_sum + array[i];
}
else if (array[i] > 0)
{
positive_sum = positive_sum + array[i];
}
else if (array[i] == 0)
{
;
}
total = total + array[i] ;
}
average = total / num;
printf("\n Sum of all negative numbers = %d\n", negative_sum);
printf("Sum of all positive numbers = %d\n", positive_sum);
printf("\n Average of all input numbers = %.2f\n", average);
}
-------------------------------------------------------------
36) Maximum number in an array
-------------------------------------------------------------
#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;
}
-------------------------------------------------------------
37) Minimum number in an array
-------------------------------------------------------------
#include <stdio.h>int main()
{
int array[100], minimum, 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]);
minimum = array[0];
for ( c = 1 ; c < size ; c++ )
{
if ( array[c] < minimum )
{
minimum = array[c];
location = c+1;
}
}
printf("Minimum element is present at location %d and it's value is %d.\n", location, minimum);
return 0;
}
-------------------------------------------------------------
38) Reverse of a number
-------------------------------------------------------------
#include <stdio.h>
int main()
{
int n, reverse = 0;
printf("Enter a number to reverse\n");
scanf("%d",&n);
while (n != 0)
{
reverse = reverse * 10;
reverse = reverse + n%10;
n = n/10;
}
printf("Reverse of entered number is = %d\n", reverse);
return 0;
}
-------------------------------------------------------------
39) Reverse of an array
-------------------------------------------------------------
#include <stdio.h>
int main()
{
int n, c, d, a[100], b[100];
printf("Enter the number of elements in array\n");
scanf("%d", &n);
printf("Enter the array elements\n");
for (c = 0; c < n ; c++)
scanf("%d", &a[c]); /* * Copying elements into array b starting from end of array a */
for (c = n - 1, d = 0; c >= 0; c--, d++)
b[d] = a[c]; /* * Copying reversed array into original. *
Here we are modifying original array, this is optional. */
for (c = 0; c < n; c++)
a[c] = b[c];
printf("Reverse array is\n");
for (c = 0; c < n; c++)
printf("%d\n", a[c]);
return 0;
}
-------------------------------------------------------------
40) Print prime numbers using functions
-------------------------------------------------------------
#include<stdio.h>
int check_prime(int);
main()
{
int n, result;
printf("Enter an integer to check whether it is prime or not.\n");
scanf("%d", &n);result = check_prime(n);
if (result == 1)
printf("%d is prime.\n", n);
elseprintf("%d is not prime.\n", n);
return 0;
}
int check_prime(int a)
{
int c;
for (c = 2; c <= a - 1; c++)
{
if (a % c == 0)
return 0;
}
if (c == a)
return 1;
}
-------------------------------------------------------------
41) Print alternate numbers
-------------------------------------------------------------
/** C Program to Print the Alternate Elements in an Array*/
#include <stdio.h>
void main()
{
int array[10];
int i, j, temp;
printf("Enter 10 elements of an array \n");
for (i = 0; i < 10; i++)
{
scanf("%d", &array[i]);}
printf("Alternate elements of a given array \n");
for (i = 0; i < 10; i += 2)
{
printf("%d\n", array[i]);
}
}
-------------------------------------------------------------
42) Insertion sort
-------------------------------------------------------------
#include <stdio.h>
int main()
{
int n, array[1000], c, d, t;
printf("Enter number of elements\n");
scanf("%d", &n);
printf("Enter %d integers\n", n);
for (c = 0; c < n; c++)
{
scanf("%d", &array[c]);
}
for (c = 1; c <= n - 1; c++)
{
d = c;
while (d > 0 && array[d] < array[d - 1])
{
t = array[d];
array[d] = array[d - 1];
array[d - 1] = t;
d--;
}
}
printf("Sorted list in ascending order:\n");
for (c = 0; c <= n - 1; c++)
{
printf("%d\n", array[c]);
}
return 0;
}
-------------------------------------------------------------
43) Selecting sort
-------------------------------------------------------------
#include <stdio.h>
int main()
{
int array[100], n, c, d, position, swap;
printf("Enter number of elements\n");
scanf("%d", &n);
printf("Enter %d integers\n", n);
for (c = 0; c < n; c++)
scanf("%d", &array[c]);
for (c = 0; c < (n - 1); c++)
{
position = c;
for (d = c + 1; d < n; d++)
{
if (array[position] > array[d])position = d;
}
if (position != c)
{
swap = array[c];
array[c] = array[position];
array[position] = swap;
}
}
printf("Sorted list in ascending order:\n");
for (c = 0; c < n; c++)
printf("%d\n", array[c]);
return 0;
}
-------------------------------------------------------------
44) HCF and LCM
-------------------------------------------------------------
#include <stdio.h>
int main()
{
int a, b, x, y, t, gcd, lcm;
printf("Enter two integers\n");
scanf("%d%d", &x, &y);
a = x;
b = y;
while (b != 0)
{
t = b;
b = a % b;
a = t;
}
gcd = a;
lcm = (x * y) / gcd;
printf("Greatest common divisor of %d and %d = %d\n", x, y, gcd);
printf("Least common multiple of %d and %d = %d\n", x, y, lcm);
return 0;
}
-------------------------------------------------------------
45) Perfect number
-------------------------------------------------------------
#include <stdio.h>;
int main()
{
//The first perfect number is 6, because 1, 2, and 3//are its proper positive divisors, and 1 + 2 + 3 = 6.int n, i = 1,
sum = 0;
printf("Enter a number: ");
scanf("%d", &n);
while (i < n)
{
if (n % i == 0)
sum = sum + i;
i++;
}
if (sum == n)
printf("%d is a perfect number", i);
elseprintf("%d is not a perfect number", i);
return 0;
}
-------------------------------------------------------------
46) Leap year
-------------------------------------------------------------
#include <stdio.h>
int main()
{
int year;
printf("Enter a year to check if it is a leap year\n");
scanf("%d", &year);
if ( year%400 == 0)
printf("%d is a leap year.\n", year);
else if ( year%100 == 0)
printf("%d is not a leap year.\n", year);
else if ( year%4 == 0 )
printf("%d is a leap year.\n", year);
else printf("%d is not a leap year.\n", year);
return 0;
}
-------------------------------------------------------------
47) Null pointer
-------------------------------------------------------------
#include <stdio.h>
int main ()
{
int *ptr = NULL;
printf("The value of ptr is : %x\n", ptr );
return 0;
}
-------------------------------------------------------------
48) Incrementing a pointer
-------------------------------------------------------------
#include <stdio.h>
const int MAX = 3;
int main()
{
int var[] = { 10, 100, 200 };
int i, *ptr;
/* let us have array address in pointer */
ptr = var;
for (i = 0; i < MAX; i++)
{
printf("Address of var[%d] = %x\n", i, ptr);
printf("Value of var[%d] = %d\n", i, *ptr);
/* move to the next location */
ptr++;
}
return 0;
}
-------------------------------------------------------------
49) Decrementing a pointer
-------------------------------------------------------------
#include <stdio.h>
const int MAX = 3;
int main()
{
int var[] = { 10, 100, 200 };
int i, *ptr;
/* let us have array address in pointer */
ptr = &var[MAX - 1];
for (i = MAX; i > 0; i--)
{
printf("Address of var[%d] = %x\n", i, ptr);
printf("Value of var[%d] = %d\n", i, *ptr);/
* move to the previous location */
ptr--;
}
return 0;
}
-------------------------------------------------------------
50) Decimal to binary
-------------------------------------------------------------
#include <stdio.h>
int main()
{
int n, c, k;
printf("Enter an integer in decimal number system\n");
scanf("%d", &n);
printf("%d in binary number system is:\n", n);
for (c = 31; c >= 0; c--)
{
k = n >> c;
if (k & 1)
printf("1");
elseprintf("0");
}
printf("\n");return 0;
}
-------------------------------------------------------------
51) Pointers and Structures
-------------------------------------------------------------
#include <stdio.h>struct person
{
int age;char *name;
};
int main()
{
struct person first;
struct person *ptr;
first.age = 21;
char *fullname = "full name";
first.name = fullname;
ptr = &first;
printf("age=%d, name=%s\n", first.age, ptr->name);
}
-------------------------------------------------------------
52) Pointer to a pointer
-------------------------------------------------------------
#include <stdio.h>
int main()
{
int var;
int *ptr;
int **pptr;
var = 3000;
/* take the address of var */
ptr = &var;
/* take the address of ptr using address of operator & */
pptr = &ptr;
/* take the value using pptr */
printf("Value of var = %d\n", var);
printf("Value available at *ptr = %d\n", *ptr);
printf("Value available at **pptr = %d\n", **pptr);
return 0;
}
-------------------------------------------------------------
53) Pointer comparison
-------------------------------------------------------------
#include <stdio.h>
const int MAX = 3;
int main()
{
int var[] = { 10, 100, 200 };
int i, *ptr;
* let us have address of the first element in pointer */
ptr = var;
i = 0;
while (ptr <= &var[MAX - 1])
{
printf("Address of var[%d] = %x\n", i, ptr);
printf("Value of var[%d] = %d\n", i, *ptr);
/* point to the previous location */
ptr++;
i++;}
return 0;
}
-------------------------------------------------------------
54) Implementing a Queue using array
-------------------------------------------------------------
/** C Program to Implement a Queue using an Array*/
#include <stdio.h>
#define MAX 50int 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");
}
/*End of switch*/
}
/*End of while*/
}
/*End of main()
*/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;
}
}
/*End of insert()*/
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;
}
}
/*End of delete()
*/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");
}
}
/*End of display() */
-------------------------------------------------------------
55) Delete given number from the array
-------------------------------------------------------------
/** C program to accept an array of integers and delete the* specified integer from the list*/
#include <stdio.h>
void main()
{
int vectorx[10];
int i, n, pos, element, found = 0;
printf("Enter how many elements\n");
scanf("%d", &n);
printf("Enter the elements\n");
for (i = 0; i < n; i++)
{
scanf("%d", &vectorx[i]);
}
printf("Input array elements are\n");
for (i = 0; i < n; i++)
{
printf("%d\n", vectorx[i]);
}
printf("Enter the element to be deleted\n");
scanf("%d", &element);
for (i = 0; i < n; i++)
{
if (vectorx[i] == element)
{
found = 1;
pos = i;
break;
}
}
if (found == 1)
{
for (i = pos; i < n - 1; i++)
{
vectorx[i] = vectorx[i + 1];}
printf("The resultant vector is \n");
for (i = 0; i < n - 1; i++)
{printf("%d\n", vectorx[i]);
}
}
else {printf("Element %d is not found in the vector\n", element);
}
}
-------------------------------------------------------------
56) Delete element from the array
-------------------------------------------------------------
#include <stdio.h>
int main()
{
int array[100],
position, c, n;
printf("Enter number of elements in array\n");
scanf("%d", &n);
printf("Enter %d elements\n", n);
for ( c = 0 ; c < n ; c++ )
scanf("%d", &array[c]);
printf("Enter the location where you wish to delete element\n");
scanf("%d", &position);
if ( position >= n+1 )
printf("Deletion not possible.\n");
else
{
for ( c = position - 1 ; c < n - 1 ; c++ )
array[c] = array[c+1];
printf("Resultant array is\n");
for( c = 0 ; c < n - 1 ; c++ )
printf("%d\n", array[c]);
}
return 0;
}
-------------------------------------------------------------
57) Linked List
-------------------------------------------------------------
# include <stdio.h>
# include <stdlib.h>
struct NODE {int number;struct NODE *next;
};
int search_value(struct NODE *llist, int num);
void append_node(struct NODE *llist, int num);
void display_list(struct NODE *llist);
void delete_node(struct NODE *llist, int num);
int main(void) {int num = 0;int input = 1;
int retval = 0;
struct NODE *llist;llist = (struct NODE *) malloc(sizeof(struct NODE));
llist->number = 0;
llist->next = NULL;
while (input != 0)
{
printf("\n-- Menu Selection --\n");
printf("0) Quit\n");
printf("1) Insert\n");
printf("2) Delete\n");
printf("3) Search\n");
printf("4) Display\n");
scanf("%d", &input);
switch (input)
{
case 0:default:printf("Goodbye ...\n");
input = 0;
break;
case 1:printf("Your choice: `Insertion'\n");
printf("Enter the value which should be inserted: ");
scanf("%d", &num);
append_node(llist, num);
break;
case 2:printf("Your choice: `Deletion'\n");
printf("Enter the value which should be deleted: ");
scanf("%d", &num);
delete_node(llist, num);
break;
case 3:printf("Your choice: `Search'\n");
printf("Enter the value you want to find: ");
scanf("%d", &num);
if ((retval = search_value(llist, num)) == -1)
printf("Value `%d' not found\n", num);
elseprintf("Value `%d' located at position `%d'\n", num, retval);
break;
case 4:printf("You choice: `Display'\n");
display_list(llist);
break;
}
/* switch */} /* while */free(llist);
return (0);
}
void display_list(struct NODE *llist)
{
while (llist->next != NULL)
{
printf("%d ", llist->number);
llist = llist->next;
}
printf("%d", llist->number);
}
void append_node(struct NODE *llist, int num)
{
while (llist->next != NULL)
llist = llist->next;
llist->next = (struct NODE *) malloc(sizeof(struct NODE));
llist->next->number = num
llist->next->next = NULL;
}
void delete_node(struct NODE *llist, int num) {struct NODE *temp;temp = (struct NODE *)
malloc(sizeof(struct NODE));
if (llist->number == num)
{
/* remove the node */
temp = llist->next;
free(llist);
llist = temp;
}
else
{
while (llist->next->number != num)
llist = llist->next;
temp = llist->next->next;
free(llist->next);llist->next = temp;
}
}
int search_value(struct NODE *llist, int num)
{int retval = -1;int i = 1;
while (llist->next != NULL)
{
if (llist->next->number == num)
return i;
else
i++;
llist = llist->next;
}
return retval;
}
-------------------------------------------------------------
58) Linked Queue
-------------------------------------------------------------
# include <stdio.h>
# include <stdlib.h>
struct qnode
{
int data;
int prio;
struct qnode *next;
};
void quein(struct qnode **, struct qnode **, int, int);
int quedel(struct qnode **, struct qnode **, int *, int *);
int main(void) {int tab[10] = { 2, 8, 3, 5, 4, 9, 6, 7, 1, 0 };
struct qnode *first = NULL;
struct qnode *last = NULL;
int val, prio, i;
for (i = 0; i < 10; i++)
{
val = tab[i], prio = i;
printf("Inserting: value: %d with priority: %d\n", prio, val);
quein(&first, &last, val, prio);
}
printf("=-=\n");
for (i = 0; i < 11; i++)
{
val = tab[i], prio = i;
if (quedel(&first, &last, &val, &prio) != -1)
printf("Deleting: value: %d with priority: %d\n", prio, val);
}
return 0;
}
int quedel(struct qnode **first, struct qnode **last, int *prio, int *val)
{
struct qnode *tmp = NULL;
if ((NULL == *last) && (*last == *first))
{
fprintf(stderr, "Empty queue.....\n");
return -1;
}
*val = (*first)->data, *prio = (*first)->prio;tmp = *first, *first = (*first)->next;
if (*last == tmp)*last = (*last)->next;
free(tmp);
return 0;
}
void quein(struct qnode **first, struct qnode **last, int prio, int val) {struct qnode *tmp = NULL;
struct qnode *tmp1 = NULL;
tmp = malloc(sizeof(struct qnode));
tmp->data = val;
tmp->prio = prio;
tmp->next = NULL;
if (*last == NULL) {*last = tmp;*first = *last;
}
else
{
if ((*first)->prio < prio)
{
tmp->next = *first;
*first = tmp;
}
else
{
if ((*last)->prio > prio)
{
(*last)->next = tmp;
*last = tmp;}
else
{
tmp1 = *first;
while ((tmp1->next)->prio >= prio)
{
tmp1 = tmp1->next;
}
tmp->next = tmp1->next;
tmp1->next = tmp;
}
}
}return;}
-------------------------------------------------------------
59) ncr and npr
-------------------------------------------------------------
#include<stdio.h>
long factorial(int);
long find_ncr(int, int);
long find_npr(int, int);
main()
{
int n, r;
long ncr,
npr;
printf("Enter the value of n and r\n");
scanf("%d%d", &n, &r);
ncr = find_ncr(n, r);
npr = find_npr(n, r);
printf("%dC%d = %ld\n", n, r, ncr);
printf("%dP%d = %ld\n", n, r, npr);
return 0;
}
long find_ncr(int n, int r)
{
long result;
result = factorial(n) / (factorial(r) * factorial(n - r));
return result;
}
long find_npr(int n, int r) {long result;
result = factorial(n) / factorial(n - r);
return result;
}
long factorial(int n)
{
int c;
long result = 1;
for (c = 1; c <= n; c++)
result = result * c;
return (result);
}
int main()
{
int array[100], n, c, d, position, swap;
printf("Enter number of elements\n");
scanf("%d", &n);
printf("Enter %d integers\n", n);
for (c = 0; c < n; c++)
scanf("%d", &array[c]);
for (c = 0; c < (n - 1); c++)
{
position = c;
for (d = c + 1; d < n; d++)
{
if (array[position] > array[d])position = d;
}
if (position != c)
{
swap = array[c];
array[c] = array[position];
array[position] = swap;
}
}
printf("Sorted list in ascending order:\n");
for (c = 0; c < n; c++)
printf("%d\n", array[c]);
return 0;
}
-------------------------------------------------------------
44) HCF and LCM
-------------------------------------------------------------
#include <stdio.h>
int main()
{
int a, b, x, y, t, gcd, lcm;
printf("Enter two integers\n");
scanf("%d%d", &x, &y);
a = x;
b = y;
while (b != 0)
{
t = b;
b = a % b;
a = t;
}
gcd = a;
lcm = (x * y) / gcd;
printf("Greatest common divisor of %d and %d = %d\n", x, y, gcd);
printf("Least common multiple of %d and %d = %d\n", x, y, lcm);
return 0;
}
-------------------------------------------------------------
45) Perfect number
-------------------------------------------------------------
#include <stdio.h>;
int main()
{
//The first perfect number is 6, because 1, 2, and 3//are its proper positive divisors, and 1 + 2 + 3 = 6.int n, i = 1,
sum = 0;
printf("Enter a number: ");
scanf("%d", &n);
while (i < n)
{
if (n % i == 0)
sum = sum + i;
i++;
}
if (sum == n)
printf("%d is a perfect number", i);
elseprintf("%d is not a perfect number", i);
return 0;
}
-------------------------------------------------------------
46) Leap year
-------------------------------------------------------------
#include <stdio.h>
int main()
{
int year;
printf("Enter a year to check if it is a leap year\n");
scanf("%d", &year);
if ( year%400 == 0)
printf("%d is a leap year.\n", year);
else if ( year%100 == 0)
printf("%d is not a leap year.\n", year);
else if ( year%4 == 0 )
printf("%d is a leap year.\n", year);
else printf("%d is not a leap year.\n", year);
return 0;
}
-------------------------------------------------------------
47) Null pointer
-------------------------------------------------------------
#include <stdio.h>
int main ()
{
int *ptr = NULL;
printf("The value of ptr is : %x\n", ptr );
return 0;
}
-------------------------------------------------------------
48) Incrementing a pointer
-------------------------------------------------------------
#include <stdio.h>
const int MAX = 3;
int main()
{
int var[] = { 10, 100, 200 };
int i, *ptr;
/* let us have array address in pointer */
ptr = var;
for (i = 0; i < MAX; i++)
{
printf("Address of var[%d] = %x\n", i, ptr);
printf("Value of var[%d] = %d\n", i, *ptr);
/* move to the next location */
ptr++;
}
return 0;
}
-------------------------------------------------------------
49) Decrementing a pointer
-------------------------------------------------------------
#include <stdio.h>
const int MAX = 3;
int main()
{
int var[] = { 10, 100, 200 };
int i, *ptr;
/* let us have array address in pointer */
ptr = &var[MAX - 1];
for (i = MAX; i > 0; i--)
{
printf("Address of var[%d] = %x\n", i, ptr);
printf("Value of var[%d] = %d\n", i, *ptr);/
* move to the previous location */
ptr--;
}
return 0;
}
-------------------------------------------------------------
50) Decimal to binary
-------------------------------------------------------------
#include <stdio.h>
int main()
{
int n, c, k;
printf("Enter an integer in decimal number system\n");
scanf("%d", &n);
printf("%d in binary number system is:\n", n);
for (c = 31; c >= 0; c--)
{
k = n >> c;
if (k & 1)
printf("1");
elseprintf("0");
}
printf("\n");return 0;
}
-------------------------------------------------------------
51) Pointers and Structures
-------------------------------------------------------------
#include <stdio.h>struct person
{
int age;char *name;
};
int main()
{
struct person first;
struct person *ptr;
first.age = 21;
char *fullname = "full name";
first.name = fullname;
ptr = &first;
printf("age=%d, name=%s\n", first.age, ptr->name);
}
-------------------------------------------------------------
52) Pointer to a pointer
-------------------------------------------------------------
#include <stdio.h>
int main()
{
int var;
int *ptr;
int **pptr;
var = 3000;
/* take the address of var */
ptr = &var;
/* take the address of ptr using address of operator & */
pptr = &ptr;
/* take the value using pptr */
printf("Value of var = %d\n", var);
printf("Value available at *ptr = %d\n", *ptr);
printf("Value available at **pptr = %d\n", **pptr);
return 0;
}
-------------------------------------------------------------
53) Pointer comparison
-------------------------------------------------------------
#include <stdio.h>
const int MAX = 3;
int main()
{
int var[] = { 10, 100, 200 };
int i, *ptr;
* let us have address of the first element in pointer */
ptr = var;
i = 0;
while (ptr <= &var[MAX - 1])
{
printf("Address of var[%d] = %x\n", i, ptr);
printf("Value of var[%d] = %d\n", i, *ptr);
/* point to the previous location */
ptr++;
i++;}
return 0;
}
-------------------------------------------------------------
54) Implementing a Queue using array
-------------------------------------------------------------
/** C Program to Implement a Queue using an Array*/
#include <stdio.h>
#define MAX 50int 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");
}
/*End of switch*/
}
/*End of while*/
}
/*End of main()
*/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;
}
}
/*End of insert()*/
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;
}
}
/*End of delete()
*/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");
}
}
/*End of display() */
-------------------------------------------------------------
55) Delete given number from the array
-------------------------------------------------------------
/** C program to accept an array of integers and delete the* specified integer from the list*/
#include <stdio.h>
void main()
{
int vectorx[10];
int i, n, pos, element, found = 0;
printf("Enter how many elements\n");
scanf("%d", &n);
printf("Enter the elements\n");
for (i = 0; i < n; i++)
{
scanf("%d", &vectorx[i]);
}
printf("Input array elements are\n");
for (i = 0; i < n; i++)
{
printf("%d\n", vectorx[i]);
}
printf("Enter the element to be deleted\n");
scanf("%d", &element);
for (i = 0; i < n; i++)
{
if (vectorx[i] == element)
{
found = 1;
pos = i;
break;
}
}
if (found == 1)
{
for (i = pos; i < n - 1; i++)
{
vectorx[i] = vectorx[i + 1];}
printf("The resultant vector is \n");
for (i = 0; i < n - 1; i++)
{printf("%d\n", vectorx[i]);
}
}
else {printf("Element %d is not found in the vector\n", element);
}
}
-------------------------------------------------------------
56) Delete element from the array
-------------------------------------------------------------
#include <stdio.h>
int main()
{
int array[100],
position, c, n;
printf("Enter number of elements in array\n");
scanf("%d", &n);
printf("Enter %d elements\n", n);
for ( c = 0 ; c < n ; c++ )
scanf("%d", &array[c]);
printf("Enter the location where you wish to delete element\n");
scanf("%d", &position);
if ( position >= n+1 )
printf("Deletion not possible.\n");
else
{
for ( c = position - 1 ; c < n - 1 ; c++ )
array[c] = array[c+1];
printf("Resultant array is\n");
for( c = 0 ; c < n - 1 ; c++ )
printf("%d\n", array[c]);
}
return 0;
}
-------------------------------------------------------------
57) Linked List
-------------------------------------------------------------
# include <stdio.h>
# include <stdlib.h>
struct NODE {int number;struct NODE *next;
};
int search_value(struct NODE *llist, int num);
void append_node(struct NODE *llist, int num);
void display_list(struct NODE *llist);
void delete_node(struct NODE *llist, int num);
int main(void) {int num = 0;int input = 1;
int retval = 0;
struct NODE *llist;llist = (struct NODE *) malloc(sizeof(struct NODE));
llist->number = 0;
llist->next = NULL;
while (input != 0)
{
printf("\n-- Menu Selection --\n");
printf("0) Quit\n");
printf("1) Insert\n");
printf("2) Delete\n");
printf("3) Search\n");
printf("4) Display\n");
scanf("%d", &input);
switch (input)
{
case 0:default:printf("Goodbye ...\n");
input = 0;
break;
case 1:printf("Your choice: `Insertion'\n");
printf("Enter the value which should be inserted: ");
scanf("%d", &num);
append_node(llist, num);
break;
case 2:printf("Your choice: `Deletion'\n");
printf("Enter the value which should be deleted: ");
scanf("%d", &num);
delete_node(llist, num);
break;
case 3:printf("Your choice: `Search'\n");
printf("Enter the value you want to find: ");
scanf("%d", &num);
if ((retval = search_value(llist, num)) == -1)
printf("Value `%d' not found\n", num);
elseprintf("Value `%d' located at position `%d'\n", num, retval);
break;
case 4:printf("You choice: `Display'\n");
display_list(llist);
break;
}
/* switch */} /* while */free(llist);
return (0);
}
void display_list(struct NODE *llist)
{
while (llist->next != NULL)
{
printf("%d ", llist->number);
llist = llist->next;
}
printf("%d", llist->number);
}
void append_node(struct NODE *llist, int num)
{
while (llist->next != NULL)
llist = llist->next;
llist->next = (struct NODE *) malloc(sizeof(struct NODE));
llist->next->number = num
llist->next->next = NULL;
}
void delete_node(struct NODE *llist, int num) {struct NODE *temp;temp = (struct NODE *)
malloc(sizeof(struct NODE));
if (llist->number == num)
{
/* remove the node */
temp = llist->next;
free(llist);
llist = temp;
}
else
{
while (llist->next->number != num)
llist = llist->next;
temp = llist->next->next;
free(llist->next);llist->next = temp;
}
}
int search_value(struct NODE *llist, int num)
{int retval = -1;int i = 1;
while (llist->next != NULL)
{
if (llist->next->number == num)
return i;
else
i++;
llist = llist->next;
}
return retval;
}
-------------------------------------------------------------
58) Linked Queue
-------------------------------------------------------------
# include <stdio.h>
# include <stdlib.h>
struct qnode
{
int data;
int prio;
struct qnode *next;
};
void quein(struct qnode **, struct qnode **, int, int);
int quedel(struct qnode **, struct qnode **, int *, int *);
int main(void) {int tab[10] = { 2, 8, 3, 5, 4, 9, 6, 7, 1, 0 };
struct qnode *first = NULL;
struct qnode *last = NULL;
int val, prio, i;
for (i = 0; i < 10; i++)
{
val = tab[i], prio = i;
printf("Inserting: value: %d with priority: %d\n", prio, val);
quein(&first, &last, val, prio);
}
printf("=-=\n");
for (i = 0; i < 11; i++)
{
val = tab[i], prio = i;
if (quedel(&first, &last, &val, &prio) != -1)
printf("Deleting: value: %d with priority: %d\n", prio, val);
}
return 0;
}
int quedel(struct qnode **first, struct qnode **last, int *prio, int *val)
{
struct qnode *tmp = NULL;
if ((NULL == *last) && (*last == *first))
{
fprintf(stderr, "Empty queue.....\n");
return -1;
}
*val = (*first)->data, *prio = (*first)->prio;tmp = *first, *first = (*first)->next;
if (*last == tmp)*last = (*last)->next;
free(tmp);
return 0;
}
void quein(struct qnode **first, struct qnode **last, int prio, int val) {struct qnode *tmp = NULL;
struct qnode *tmp1 = NULL;
tmp = malloc(sizeof(struct qnode));
tmp->data = val;
tmp->prio = prio;
tmp->next = NULL;
if (*last == NULL) {*last = tmp;*first = *last;
}
else
{
if ((*first)->prio < prio)
{
tmp->next = *first;
*first = tmp;
}
else
{
if ((*last)->prio > prio)
{
(*last)->next = tmp;
*last = tmp;}
else
{
tmp1 = *first;
while ((tmp1->next)->prio >= prio)
{
tmp1 = tmp1->next;
}
tmp->next = tmp1->next;
tmp1->next = tmp;
}
}
}return;}
-------------------------------------------------------------
59) ncr and npr
-------------------------------------------------------------
#include<stdio.h>
long factorial(int);
long find_ncr(int, int);
long find_npr(int, int);
main()
{
int n, r;
long ncr,
npr;
printf("Enter the value of n and r\n");
scanf("%d%d", &n, &r);
ncr = find_ncr(n, r);
npr = find_npr(n, r);
printf("%dC%d = %ld\n", n, r, ncr);
printf("%dP%d = %ld\n", n, r, npr);
return 0;
}
long find_ncr(int n, int r)
{
long result;
result = factorial(n) / (factorial(r) * factorial(n - r));
return result;
}
long find_npr(int n, int r) {long result;
result = factorial(n) / factorial(n - r);
return result;
}
long factorial(int n)
{
int c;
long result = 1;
for (c = 1; c <= n; c++)
result = result * c;
return (result);
}
No comments:
Post a Comment