C Problems and Solutions: Introduction
Some printing formats are shown in this code.
#include<stdio.h>
int main(){
int x;
double y;
x = 78341;
printf("//12345678//\n",x);
printf(" *%d*\n");
printf(" *%8d*\n"); //right aligned with a
width of 8 lengths
printf(" *%-8d*\n"); //left aligned with a
width of 8 lengths
printf("\n\n"); //for seeing convinence I have
parted those two.
y = 567.388129;
printf("// 1 2 3//\n");
printf("//123456789012345678901234567890//\n");
printf(" *%lf*\n",y); //default 6 digits after
decimal point
printf(" *%.2lf*\n",y);
//note the round off: 567.39 should be printed. bcz
the 3rd digit is greater than 4.
printf(" *%.10lf*\n",y); //extra zeros have been
added.
printf(" *%.25lf*\n",y);
//can you see an Extra Number? this is bcoz of
precision ERROR! This occurs in floating point number.
printf(" *%20.8lf*\n",y);
//total 20 digits will be used & 8 digit will be
printed after decimal point, RIGHT aligned
printf(" *%-20.8lf*\n",y); //same, just LEFT
aligned
return 0;
}
Math.h Functions
#include<stdio.h>
#include<math.h>
int main(){
double x,y;
scanf("%lf%lf",&x,&y);
printf("sin(x rad) = %lf\n",sin(x)); //x in radians
printf("x to the power y = %lf\n",pow(x,y)); //call >> pow(base,
power)
double pi;
pi = 2*acos(0);
/*
acos(x) = cos_inverse(x).
we know that cos_inverse(0) = 90 deg = pi/2;
so pi = 2*acos(0)
*/
printf("pi = %.15lf\n",pi); //check this value with PI
/// %.15lf means 15 digits should be printed after the decimal point
//similarly
printf("pi = %.8lf\n",pi);
//e^x
printf("e^x = %lf\n",exp(x));
return 0;
}
Custom Functions
#include<stdio.h>
int absolute(int x){
if(x<0)
return -x;
else
return x;
}
int f(int x){
return x*x + x + 1;
}
int g(int x){
return x - 1;
}
double r(int x){
return (3*x + 2) / (4*x - 5);
int main(){
int n;
scanf("%d",&n);
printf("%d\n",absolute(n));
printf("%d\n",g(n));
printf("%d\n",f(n));
printf("%d\n",f(g(n)));
printf("%d\n",g(f(n)));
printf("%d\n",f(n)*g(n));
printf("%lf\n",r(n));
return 0;
}
C Problems and Solutions : If-else
Checking AND-OR-NOT Conditions
#include<stdio.h>
int main(){
int a,b,c,d;
printf("Enter 4 Numbers: ");
while(scanf("%d%d%d%d",&a, &b, &c, &d)==4){
printf("a = %d, b = %d, c = %d, d = %d\n", a, b, c, d);
if(a == b && c == d){
printf("a == b && c ==d AND\n");
}
if(a==b || c==d)
printf("a==b || c==d OR\n");
if(a!=b)
printf("a != b NOT\n");
printf("Enter 4 Numbers: ");
return 0;
}
Write a program to determine whether a number is even/odd.
#include<stdio.h>
int main(){
int n;
printf("Enter the number\n");
scanf("%d",&n);
if(n%2 == 0)
printf("The number %d is even\n", n);
else
printf("The number %d is odd\n", n);
return 0;
}
Write a program to determine whether a given year is leap year or not.
#include<stdio.h>
int main(){
int year;
scanf("%d",&year);
if(year %400 == 0 || (year%4 == 0 && year%100!= 0))
printf("Yes! The year %d is a leap year.\n", year);
else
printf("No! The year %d is not a leap year.\n", year);
return 0;
}
Input two numbers. Check whether the first number is greater/smaller/equal to
second number.
#include<stdio.h>
int main(){
int a,b;
scanf("%d%d",&a,&b);
if(a > b)
printf("a is greater than b\n");
else if(a < b)
printf("a is smaller than b\n");
else
printf("a is equal to b\n");
return 0;
}
Write a program to check whether a given triangle is a Right triangle by taking
coordinates of 3 points of triangle.
#include<stdio.h>
int main(){
int x1,x2,x3,y1,y2,y3;
int aa,bb,cc;
while(scanf("%d%d%d%d%d%d",&x1,&y1,&x2,&y2,&x3,&y3)==6){
if( (x1==x2 && y1==y2) || (x2==x3 && y2==y3) || (x3==x1 && y3==y1) ){
printf("no\n");
continue;
}
aa = (x2-x3)*(x2-x3) + (y2-y3)*(y2-y3);
bb = (x3-x1)*(x3-x1) + (y3-y1)*(y3-y1);
cc = (x1-x2)*(x1-x2) + (y1-y2)*(y1-y2);
if(aa==bb+cc || bb==cc+aa || cc==aa+bb) printf("Yes\n");
else printf("No\n");
}
return 0;
}
C Problems and Solutions: Loops
Write a program to print 1 to n.
#include<stdio.h>
int main(){
int n,i;
scanf("%d",&n);
for(i=1;i<=n;i++){
printf("%d\n", i);
}
return 0;
}
Write a program to take input & print the number. Input is terminated by
getting a value of 0 or pressing 'Ctrl+Z'.
#include<stdio.h>
int main(){
int n;
while(scanf("%d",&n) == 1 && n){
printf("Input is = %d\n",n);
}
return 0;
}
Write a program to print the summation of 1 + 2 + ... + n.
#include<stdio.h>
int main(){
int n,i,sum;
sum = 0;
scanf("%d",&n);
for(i=1;i<=n;i++){
sum+=i;
}
printf("sum = %d\n",sum);
return 0;
}
Write a program to print the summation of all integers from 1 to n that are not
divisible by 13.
#include<stdio.h>
int main(){
int n,i,sum;
sum = 0;
scanf("%d",&n);
for(i=1;i<=n;i++){
if(i%13==0) //i mod 13 = 0, i.e. I am checking IF i is
divisible by 13.
continue;
sum+=i;
}
printf("summation = %d\n",sum);
return 0;
}
Write a program to compute the digit sum of n.
#include<stdio.h>
int main(){
int n, sum;
scanf("%d",&n);
printf("Digit sum of %d is ",n);
sum = 0;
while(n){
sum += n % 10;
n /= 10;
}
printf("%d\n",sum);
return 0;
}
Write a program to compute the digit root of n.
#include<stdio.h>
int main(){
int n, sum;
scanf("%d",&n);
printf("Digit root of %d is ",n);
while(1){
sum = 0;
while(n){
sum += n % 10;
n/=10;
}
if(sum < 10)
break;
else
n = sum;
}
printf("%d\n",sum);
return 0;
}
Write a program to take an integer input and print output according to the
output format.
5
*
**
***
****
*****
#include<stdio.h>
int main(){
int i, j;
int n;
while(scanf("%d",&n) == 1 && n){
for(i=1; i<=n ;i++){
for(j=1; j<=i;j++){
printf("*");
if(j != i) // for excluding last space
printf(" ");
}
printf("\n");
}
}
return 0;
}
Write a program to take an integer input and print output according to the
output format.
6
******
*****
****
***
**
*
#include<stdio.h>
int main(){
int i, j;
int n;
while(scanf("%d",&n) == 1 && n){
for(i=n; i>0; i--){
for(j=1; j<=i; j++)
printf("* ");
printf("\n");
}
}
return 0;
}
Write a program to take an integer input and print output according to the
output format.
7
*******
******
*****
****
***
**
*
#include<stdio.h>
int main(){
int i, j;
int n;
while(scanf("%d",&n) == 1 && n){
for(i=n; i>0 ;i--){
for(j=1; j<=n-i; j++)
printf(" ");
for(j=1; j<=i; j++)
printf("* ");
printf("\n");
}
}
return 0;
}
Write a program to take an integer input and print output according to the
output format.
5
*
**
***
****
*****
#include<stdio.h>
int main(){
int i, j;
int n;
while(scanf("%d",&n) == 1 && n){
for(i=1; i<=n ;i++){
for(j=n-i; j>0; j--)
printf(" ");
for(j=1; j<=i; j++)
printf("* ");
printf("\n");
}
}
return 0;
}
Write a program to take an integer input and print output according to the
output format.
8
*
***
*****
*******
*********
***********
*************
***************
#include<stdio.h>
int main(){
int i, j, r;
int n;
while(scanf("%d",&n) == 1 && n){
for(r=1; r<=n ;r++){
for(j=1; j<=n-r; j++)
printf(" ");
for(i=0; i<r-1; i++)
printf("* ");
printf("* ");
for(i=r-2; i>=0; i--)
printf("* ");
printf("\n");
}
}
return 0;
}
Write a program to take an integer input and print output according to the
output format.
6
***********
*********
*******
*****
***
*
#include<stdio.h>
int main(){
int i, j, r;
int n;
while(scanf("%d",&n) == 1 && n){
for(r=n; r>0 ;r--){
for(j=1; j<=n-r;j++)
printf(" ");
for(i=0; i<r-1;i++)
printf("* ");
printf("* ");
for(i=r-2; i>=0;i--)
printf("* ");
printf("\n");
}
}
return 0;
}
Write a program to take an integer input and print output according to the
output format.
6
1
12
123
1234
12345
123456
#include<stdio.h>
int main(){
int i, j;
int n;
while(scanf("%d",&n) == 1 && n){
for(i=1; i<=n;i++){
for(j=1; j<=i; j++)
printf("%d ",j);
printf("\n");
}
}
return 0;
}
Write a program to reverses the digits of an integer.
#include<stdio.h>
int rev(int X){
int R = 0;
while(X){
R = R*10 + (X%10);
X/=10;
}
return R;
}
int main(){
int n;
while(scanf("%d",&n)==1){
printf("reverse of %d is = %d\n",n,rev(n));
}
return 0;
}
A C Program showing idea of Nested Looping.
Write a code printing all pairs (i,j) such that 0<i<=i_max & 0<j<=j_max & i & j are
integers where i_max = 4 and j_max = 4.
#include<stdio.h>
int main(){
int i,j;
int i_max,j_max;
i_max = 4;
j_max = 4;
//the following is nested looping::
printf("NESTED\n");
/////////////
for(i=1;i<=i_max;i++){
for(j=1;j<=j_max;j++){
printf("i = %d j = %d\n",i,j);
}
}
//the following is NOT::
printf("NOT NESTED\n");
////////////
for(i=1;i<=i_max;i++){
printf("i = %d\n",i);
}
for(j=1;j<=j_max;j++){
printf("j = %d\n",j);
}
return 0;
}
Write a C program to print Pythagorean Triplets up to N and count no of
triplets found.
Finding Pythagorean triplets
Given N, printing all Pythagorean triplets of (a,b,c) such that
1. a,b,c are integers
2. 0 < a < b < c < N
3. a^2 + b^2 = c^2
#include<stdio.h>
int main(){
int a,b,c,N,count;
scanf("%d",&N);
count=0;
for(a=1;a<N;a++){
for(b=a+1;b<N;b++){
for(c=b+1;c<N;c++){
if(a*a + b*b == c*c){
printf("%d^2 + %d^2 = %d^2\n",a,b,c);
count++;
}
}
}
}
printf("%d\n",count);
return 0;
}
Write a program to compute nth Fibonacci number [Fibonacci with
backtracking - without memoization]
//fibonacci with backtracking [without memoization]
#include<stdio.h>
int fib(int n){
if(n<2)
return n;
return fib(n-1) + fib(n-2);
}
int main(){
int n,F;
while(scanf("%d",&n) == 1){
F = fib(n);
printf("%d\n",F);
}
return 0;
}
Write a program to compute nth Fibonacci number [Fibonacci with
backtracking - with memoization]
//fibonacci with backtracking [with memoization]
#include<stdio.h>
int mem[100];
//F47 exceeds int's RANGE
int fib(int n){
if(n<2)
return n;
if(!mem[n])
mem[n] = fib(n-1) + fib(n-2);
return mem[n];
}
int main(){
int n,F;
mem[0] = 0; //F0 = 0 [by definition]
mem[1] = 1; //F1 = 1 [by definition]
while(scanf("%d",&n)==1){
F = fib(n);
printf("%d\n",F);
}
return 0;
}
Write a program to compute nth Fibonacci number - DP
//fibonacci with DP
#include<stdio.h>
int fib[47];
//fib[47] exceeds int's RANGE
int main(){
int n,i;
//generating all:
fib[0] = 0;
fib[1] = 1;
for(i=2;i<47;i++)
fib[i] = fib[i-1] + fib[i-2];
while(scanf("%d",&n)==1){
printf("%d\n",fib[n]);
}
return 0;
}
Write a program to compute nCr. [BKTK]
#include<stdio.h>
//C(n,r) = C(n-1,r) + C(n-1,r-1)
int C(int n,int r){
if(n<0 || n<r || r<0) return 0;
if(!r || n==r) return 1;
return C(n-1,r) + C(n-1,r-1);
}
int main(){
int n,r;
while(scanf("%d%d",&n,&r)==2){
printf("%dC%d = %d\n",n,r,C(n,r));
}
return 0;
}
Write a program to compute nCr. [DP]
#include<stdio.h>
int C[100][100];
int main(){
int n,r;
/*
IMPORTANT::
C(n,r) = C(n-1,r) + C(n-1,r-1)
so the loop for n should be the major loop while calculating with DP
*/
C[0][0] = 1;
for(n=1;n<100;n++)
for(r=0;r<=n;r++)
C[n][r] = C[n-1][r] + C[n-1][r-1];
while(scanf("%d%d",&n,&r)==2){
if(r<0 || n<0 || r>n){
printf("%dC%d = 0\n",n,r);
continue;
}
printf("%dC%d = %d\n",n,r,C[n][r]);
}
return 0;
}
Print 1st fifteen rows of a Pascal's Triangle.
#include<stdio.h>
int C[100][100];
int main(){
int n,r,limit;
C[0][0] = 1;
for(n=1;n<100;n++)
for(r=0;r<=n;r++)
C[n][r] = C[n-1][r] + C[n-1][r-1];
limit = 15;
for(n=0;n<limit;n++){
for(r=0;r<=n;r++)
printf("%d ",C[n][r]);
printf("\n");
}
return 0;
}
C Problems and Solutions: Bitwise Operations
Write a program to print the binary of an integer using bitwise operations.
#include<stdio.h>
#define B(K) (K&1?0:1)
void print_binary(int n){
int i;
for(i=31;i>=0;i--){
if(n&(1<<i))
printf("1");
else
printf("0");
}
printf("\n");
}
int main(){
int x;
while(scanf("%d",&x)==1){
print_binary(x);
}
return 0;
}
Write a macro using bitwise operation to determine whether an integer is even or
odd.
#include<stdio.h>
#define B(K) (K&1?0:1)
int main(){
int n;
while(scanf("%d",&n)==1){
if(B(n))
printf("%d is Even", n);
else
printf("%d is Odd", n);
printf("\n");
}
return 0;
}
Write a program using bitwise operation to print the 1's complement of an integer (Way 1)
#include<stdio.h>
#include<math.h>
int n,i;
void print(int n,int l){
for(i = l-1;i>=0;i--)
if(n & (1<<i)) printf("1");
else printf("0");
printf("\n");
}
int main(){
double d;
int l,i;
while(scanf("%d",&n) == 1){
if(n == 0){
printf("0\n");
continue;
}
d = (double)(log(n+1)/ log(2));
l = (int)ceil(d);
printf("Binary of %d is: ", n);
print(n,l);
printf("1's complement of %d is: ", n);
for(i=l-1;i>=0;i--){
n = n^(1<<i);
}
print(n,l);
}
return 0;
}
Write a program using bitwise operation to print the 1's complement of an integer (Way 2)
#include<stdio.h>
#include<math.h>
int n,i;
void print(int n,int l){
for(i = l-1;i>=0;i--)
if(n & (1<<i)) printf("1");
else printf("0");
printf("\n");
}
int main(){
double d;
int l;
while(scanf("%d",&n) == 1){
if(n == 0){
printf("0\n");
continue;
}
d = (double)(log(n+1)/ log(2));
l = (int)ceil(d);
printf("Binary of %d is: ", n);
print(n,l);
printf("1's complement of %d is: ", n);
n = ~n;
print(n,l);
}
return 0;
}
C Problems and Solutions: Arrays
Bubble Sort
#include<stdio.h>
#define size 10
void sortAscendingOrder(int a[]){
int temp,i,j;
for(i=0;i<size-1;i++){
for(j=0;j<size-1;j++){
if(a[j]>a[j+1]){ // Ascending order 1,2,3,......
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
}
}
void sortDecendingOrder(int a[]){
int temp,i,j;
for(i=0;i<size-1;i++){
for(j=0;j<size-1;j++){
if(a[j]<a[j+1]){ // Decending order
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
}
}
int main(){
int a[size] = {-1, 2, 3, 898, 0, -45, 34, 23, -56, 0};
int i;
sortAscendingOrder(a);
printf("Sorting in Ascending Order\n");
for(i=0;i<10;i++)
printf("%d ",a[i]);
printf("\n");
sortDecendingOrder(a);
printf("Sorting in Decending Order\n");
for(i=0;i<10;i++)
printf("%d ",a[i]);
return 0;
}
Write a program to combine two Sorted Arrays
#include<stdio.h>
// Combine two Sorted arrays in a 3rd array
void combine(int sizeA,int sizeB,int A[],int B[],int C[]){
int i, j, k;
i = j = k = 0;
while(i<sizeA || j<sizeB){
if(i == sizeA)
C[k++] = B[j++];
else if(j == sizeB)
C[k++] = A[i++];
else{
if(A[i] <= B[j])
C[k++] = A[i++];
else
C[k++] = B[j++];
}
}
}
int main(){
int a[10] = {4,7,9,20};
int b[10] = {1,5,6,50,60,710};
int c[20];
int n = 4, m = 6 , i;
/*
n = number of elements in a
m = number of elements in b
*/
combine(n,m,a,b,c);
printf("After combining:\n");
for(i=0; i<n+m; i++){
printf("%d ", c[i]);
}
printf("\n");
return 0;
}
Sample Program showing Matrix Add, Subtraction and Multiplication.
#include<stdio.h>
#define MAX 10
void print_result(int C[][MAX],int r,int c){
int i,j;
for(i=0;i<r;i++){
for(j=0;j<c;j++)
printf("%4d",C[i][j]);
printf("\n");
}
}
void input(int A[][MAX],int r,int c){
int i,j;
for(i=0;i<r;i++){
for(j=0;j<c;j++)
scanf("%d",&A[i][j]);
}
}
void matrix_Sum(int A[][MAX],int B[][MAX],int C[][MAX],int r,int c){
int i,j;
for(i=0;i<r;i++){
for(j=0;j<c;j++){
C[i][j]=A[i][j] + B[i][j];
}
}
printf("Sum of the Matrices:\n\n");
print_result(C,r,c);
void matrix_Sub(int A[][MAX],int B[][MAX],int C[][MAX],int r,int c){
int i,j;
for(i=0;i<r;i++){
for(j=0;j<c;j++){
C[i][j]=A[i][j] - B[i][j];
}
}
printf("Subtraction of the Matrices:\n\n");
print_result(C,r,c);
void matrix_Multiplication(int A[][MAX],int B[][MAX],int C[][MAX],int r1, int
c2, int c1){
int i,j,k,sum=0;
for(i=0;i<r1;i++){
for(j=0;j<c2;j++){
sum=0;
for(k=0;k < c1 ;k++)
sum+=A[i][k] * B[k][j];
C[i][j]=sum;
}
}
printf("Multiplication of the Matrices:\n\n");
print_result(C,r1,c2);
}
int main(){
int choice;
int rowA, colA;
int rowB, colB;
int A[MAX][MAX],B[MAX][MAX],C[MAX][MAX];
printf("Enter choice:\n\n\t1: Addition\n\t2: Subtraction\n");
printf("\t3: Mutiplication\n");
while(scanf("%d",&choice)==1){
if(!(choice>=1 && choice<=3)){
printf("Invalid choice\n\n");
printf("Enter choice:\n\n\t1: Addition\n\t2: Subtraction\n");
printf("\t3 Mutiplication\n");
continue;
}
if(choice==1 || choice==2 || choice==3){
printf("1st Matrix:\n");
printf("No. of rows: ");
scanf("%d",&rowA);
printf("No. of columns: ");
scanf("%d",&colA);
printf("Enter elements for 1st Matrix:\n");
input(A,rowA,colA);
printf("\n2nd Matrix:\n");
printf("No. of rows: ");
scanf("%d",&rowB);
printf("No. of columns: ");
scanf("%d",&colB);
printf("Enter elements for 2nd Matrix:\n");
input(B,rowB,colB);
if(choice == 1){
if(rowA != rowB || colA != colB)
printf("\nMatix Addition Operation is not posible\n");
else
matrix_Sum(A,B,C,rowA,colA);
if(choice==2){
if(rowA != rowB || colA != colB)
printf("Matix Subtraction Operation is not posible\n");
else
matrix_Sub(A,B,C,rowA,colA);
}
if(choice==3){
if(colA == rowB)
matrix_Multiplication(A,B,C,rowA, colB, colA);
else
printf("Matrix multiplication is not possible\n");
}
}
printf("\nEnter choice:\n\n\t1: Addition\n\t2: Subtraction\n");
printf("\t3: Mutiplication\n\t4:Search\n\n");
}
return 0;
}
Write a function to return the Length of a string: int strlen(char s[ ] )
#include<stdio.h>
#define MAX 1000
int strlen(char s[]){
int i;
for(i=0; s[i]; i++);
return i;
}
int main(){
char s[MAX];
int len;
while(gets(s)){
len = strlen(s);
printf("Length of the string %s is %d\n", s, len);
}
return 0;
}
Write a function to Reverse the string: void strrev(char *s)
#include<stdio.h>
#define MAX 1000
int strlen(char s[]){
int i;
for(i=0; s[i]; i++);
return i;
}
void strrev(char *s){
int i, j;
char t;
for(i=0, j=strlen(s)-1; i<j; i++, j--){
t = s[i];
s[i] = s[j];
s[j] = t;
}
}
int main(){
char s[MAX];
while(gets(s)){
printf("Input String: %s\n", s);
strrev(s);
printf("%s\n", s);
return 0;
Sample C program to convert all alphabets in a string to uppercase or lowercase.
#include<stdio.h>
#define MAX 1000
void tolower(char s[]){
// converts all alphabets in string to lowercase
int i;
for(i=0; s[i]; i++)
if(s[i] >= 'A' && s[i] <= 'Z')
s[i] = s[i] - 'A' + 'a';
void toupper(char s[]){
// converts all alphabets in string to uppercase
int i;
for(i=0; s[i]; i++)
if(s[i] >= 'a' && s[i] <= 'z')
s[i] = s[i] - 'a' + 'A';
void strcpy(char s2[], char s1[]){ // copy string from s1 to s2
int i;
for(i=0; s1[i]; i++)
s2[i] = s1[i];
s2[i] = '\0';
}
int main(){
char s[MAX];
char s1[MAX];
while(gets(s)){
strcpy(s1, s);
tolower(s1);
printf("%s\n", s1);
strcpy(s1, s);
toupper(s1);
printf("%s\n", s1);
}
return 0;
}
Write a C program to check whether input string is palindrome or not.
#include<stdio.h>
int main(){
char s[10000];
int i,j,L;
while(gets(s)){
for(L=0;s[L];L++); // computing the string length
for(i=0,j=L-1;i<j;i++,j--)
if(s[i]!=s[j])
break;
if(i<j)
printf("%s is not a palindrome.\n",s);
else
printf("%s is a palindrome.\n",s);
}
return 0;
}
C Problems and Solutions: Recursion
Write a recursive function to calculate n!
#include<stdio.h>
double fact(int n){
if(n == 0 || n == 1)
return 1;
return n * fact(n-1);
}
int main(){
int n;
while(scanf("%d",&n)==1 && n != -1) // Give -1 to terminate
printf("fact of %d is %.0lf\n", n, fact(n));
return 0;
}
Write a recursive function to calculate x^n , n>=0
#include<stdio.h>
int power(int x, int n){
if(n == 1)
return x;
return x * power(x, n-1);
}
int main(){
int x, y;
while(scanf("%d%d",&x, &y) == 2){
printf("power of %d ^ %d = %d\n", x, y, power(x, y));
}
return 0;
}
C Problems and Solutions: Stack, Queue
Sample Program Showing Stack
/*
Stack
LIFO = Last in first out
*/
#include<stdio.h>
int size; //size = Stack size;
int S[100000];
int main(){
int n , order,i;
size = 0;
while(scanf("%d",&order)==1){
/*
give
0 as input for order to PUSH
1 as input for order to POP
*/
if(order==0){
scanf("%d",&n);
S[size] = n;
size++;
}
else if(order==1){
if(size==0){
printf("nothing to process, the stack is empty\n");
/*
S empty.
*/
}
else{
size--;
printf("processed %d from stack\n",S[size]);
}
}
else{
printf("wrong direction!\n");
}
//the Stack printing:
printf("<< ");
for(i=0;i< size;i++)
printf("%d ",S[i]);
printf(">>\n");
}
return 0;
}
Sample Program Showing Queue
/*
Queue
FIFO = first in first out
*/
#include<stdio.h>
#define PUSH 0
#define POP 1
int head,tail,Q[100000];
int main(){
int n , order,i;
head = tail = 0;
while(scanf("%d",&order)==1){
/*
give
0 as input for order to PUSH
1 as input for order to POP
*/
// at any case, tail<=head
if(order==0){
scanf("%d",&n);
Q[head] = n;
head++;
}
else if(order==1){
if(head==tail){
printf("nothing to process, the queue is empty\n");
/*
Queue empty.
*/
}
else{
printf("processed %d from queue\n",Q[tail]);
tail++;
}
}
else{
printf("wrong direction!\n");
}
//the Queue printing:
printf("<< ");
for(i=tail;i< head;i++)
printf("%d ",Q[i]);
printf(">>\n");
return 0;
}
http://www.nonktube.com/video/26527/