Posts

Sum of n numbers using linked list

#include<stdio.h>
#include<stdlib.h>
struct node
{
    int data;
    struct node *next;
};
struct node *head = NULL;
struct node *ptr = NULL;
void insert(int num)
{
    struct node *temp = (struct node*)malloc(sizeof(struct node));
    if(head==NULL)
    {
        temp->data=num;
        head=temp;
        ptr=head;
        head->next=NULL;
    }
    else
    {
        temp->data=num;
        ptr->next=temp;
        ptr=temp;
        ptr->next=NULL;
    }
}
void add()
{
    int sum=0;
    ptr=head;
    while(ptr!=NULL)
    {
        sum+=ptr->data;
        ptr=ptr->next;
    }
    printf("%d",sum);
}
int main()
{
    int num;
    printf("Enter the total numbers :  ");
    scanf("%d",&num);
    for(int i=0;i<num;i++)
    {
        int value;
        scanf("%d",&value);
        insert(value);
    }
    add();
}


output Enter the total numbers :  10���������������������������������������������������������…

Binary Search

Program: #include<stdio.h>
int main()
{
    int numbers;
    printf("Enter the no.of.elements \n");
    scanf("%d",&numbers);
    int arr[numbers];
    int itr=0;
    for(itr=0;itr<numbers;itr++)
    {
        scanf("%d",&arr[itr]);
    }
    int search=0;
    printf("\n Enter the number to be searched : ");
    scanf("%d",&search);
    int first=0;
    int last=numbers-1;
    int middle = (first+last)/2;
    while(first<=last)
    {
        if(arr[middle]<search)         {             first = middle+1;
        }
        else if(arr[middle]==search)
        {
            printf("%d is present at %d position ",search,middle+1);
            break;
        }

        else
        {
            last = middle-1;
        }
        middle = (first+last)/2;
    }
    if(first>last)
    {
        printf("The value is not present ");
    }
}
OUTPUT:
Enter the no.of.elements�����������������������������������������������������������…

Sort numbers based on the no.of factors

Program: #include<stdio.h>
struct number{
    int num;
    int fact;
};
int main()
{
    int n;
    printf("Enter the total numbers \n");
    scanf("%d",&n);
    struct number num[n];
    int i,j;
    for(i=0;i<n;i++)
    {
        printf("Enter the %d number \n",i);
        scanf("%d",&num[i].num);
    }
    for(i=0;i<n;i++)
    {
        int count=0;
        for(j=2;j<=num[i].num;j++)
        {
            if(num[i].num%j==0)
            {
                count++;
            }
        }
        num[i].fact=count;
    }
    for(i=0;i<n;i++)
    {
        for(j=i+1;j<n;j++)
        {
            if(num[i].fact > num[j].fact)
            {
                int temp = num[i].fact;
                num[i].fact = num[j].fact;
                num[j].fact = temp;

                temp = num[i].num;
                num[i].num = num[j].num;
                num[j].num = temp;
            }
        }
    }
    for(i=0;i<n;i++)
    {
        printf("%d   -----   …

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…