Showing posts from July, 2018

C program to find all the permutations of the string

Program: #include<stdio.h> #include<string.h> void swap(char *x, char *y) {     char temp;     temp = *x;     *x = *y;     *y = temp; } void permute(char *a,int l,int r) {     if(l==r)     {         printf("%s \n",a);     }     else    {        for (int i = l; i <= r; i++)        {             swap((a+l),(a+i));             permute(a,l+1,r);             swap((a+l),(a+i));        }    } } int main() {     char str[]="car";     int l=strlen(str);     permute(str,0,l-1); }

Output: car                                                                                                                                             cra                                                                                                                                             acr                                                                                                                                             arc���������������������������������������������…

monetization problem

Problem: consider that you have rs 1000,500,100,50,10,5,2,1 rupee notes.Given an amount of money you have to find the possible combination of rupee notes of best possible solution (i.e combination with least no.of.notes)
Example : 450 rs
The solution is (100+100+100+100+50) and not (100+100+50+50+50+50+50) or (50+50+50+50+50+50+50+50)
This problem is a classic example of greedy method.
Program: #include<stdio.h> int main() {     int amount=0,i,count=0;     int money[9]={1000,500,100,50,20,10,5,2,1};     printf("Enter the amount \n");     scanf("%d",&amount);     for(i=0;i<9;i++)     {         if(amount>=money[i])         {             int times=amount/money[i];             count=count+times;             for(int j=0;j<times;j++)             {                 printf("%d ",money[i]);             }             amount=amount%money[i];         }     }     printf("\n %d notes are needeed.",count);     return 0; }
output: Enter the a…

Function pointer

Program: #include<stdio.h> void fun(int a) {     printf("The value of a is %d",a); } int main() {     void (*ptr)(int);     ptr=&fun;     (*ptr)(20); }

output: The value of a is 20

Kollywood Game

A simple game which we have all played once in their life,Actually according to the program the user has to give the movie name and then he has to find it which sounds silly. i could not come up with any better solution for this , so if you have any better solution please do comment . works better with two players .one has to provide the movie name as input and the other can find the movie name.

code: #include<stdio.h> #include<string.h> #include<conio.h> int checkflag(int size,char str[size]) {     for(int i=0;str[i]!='\0';i++)     {         if(str[i]=='_')         {             return 1;         }     }     return 0; } int checkkolly(int size,char str[size]) {     for(int i=0;str[i]!='\0';i++)     {         if(str[i]!='*')         {             return 1;         }     }     return 0; } int main() {     char movie_name[100];     printf("\n Enter the movie name(player 1)\n");     scanf(" %[^\n]s",movie_name);     c…