C - Program for Pigeon Breeding Problem
#include<stdio.h> #include<process.h> struct node { int age; struct node *link; }; typedef struct node* NODE;
NODE getnode() { NODE x; x=(NODE)malloc(sizeof(struct node)); if(x==NULL) { printf(“Out of memory\n”); exit(1); } return x; } void main() { unsigned long int count=1; unsigned int months,i; NODE first=getnode();/*this is the intial adult pair*/ first->age=2; /*assume the age of initial adult pair as 2*/ first->link=NULL; printf(“Enter the no. of months\n”); scanf(“%u”,&months); for(i=0;iage>=2)&&(temp->age<=60)) { NODE temp1=getnode(); temp->age+=1; temp1->age=1; temp1->link=first; first=temp1; temp=temp->link; ++count; } else { temp->age+=1; temp=temp->link; } } } printf(“Total no. of pairs after %u months=%ld\n”,months,count); }