Showing posts with label Example. Show all posts
Showing posts with label Example. Show all posts

C Program Array Example: Average

How to write a C Program Array Example: Average in C Programming Language ?


Solution For C Program :

C Program Array Example: Reverse

How to write a C Program Array Example: Reverse in C Programming Language ?


Solution For C Program :

C Program Switch - Case Example

How to write a C Program Switch - Case Example in C Programming Language ?


Solution For C Program :

C Program if - else if - else Example

How to write a C Program if - else if - else Example in C Programming language ?

Solution For C Program :

C Program Recursion Example

How to write a C Program Recursion Example in C Programming Language ?


Solution For C Program :

C Program Structure Example-2

How to write a C Program Structure Example in C Programming Language ?

Solution For C Program :

C Program Structure Example

How to write a C Program Structure Example in C Programming Language ?

Solution For C Program :

C Program Pointer Example

How to write a C Program Pointer Example in C Programming Language ?


Solution For C Program :

C Program Function Example

How to write a C Program Function Example in C Programming Language ?


Solution For C Program :

C Program String Example

How to write a C Program String Example in C Programming Language ?


Solution For C Program :

C Program Character Example

How to write a C Program Character in C Programming Language ?


Solution For C Program :

C Program sizeof & memcpy Example

How to write a C Program sizeof & memcpy in C Programming Language ?


Solution For C Program :

C Program Array Example

How to write a C Program Array Example in C Programming Language ?


Solution For C Program :

C Program Side Length Example

How to write a C Program Side Length in C Programming Language ?


Solution For C Program :

C Program Pipes()

How to Write a C Program Function Pipes() in C Programming Language ?

  1. Create pipes.
  2. Create a pipe and store file descriptors.
  3. Create a child.
  4. Reroute pipe's write end to child's output.
  5. Close unnecessary file descriptors.
  6. Execute the command.
Solution For C Program :


//Create pipes.
int pipes () {
//File descriptor array.
int fd_arr[2];
//Create a pipe and store file descriptors.
pipe(fd_arr);
//Create a child.
pid_t pid = fork();
//Child.
if (pid == 0) {
//Reroute pipe's write end to child's output.
dup2(fd_arr[1], STDOUT_FILENO);
//Close unnecessary file descriptors.
close(fd_arr[0]);
close(fd_arr[1]);
//Execute the command.
if (execvp(command[0], command) == -1) {
exit(EXIT_FAILURE);
}
}
//Parent.
//Set pipes read end to parent's input.
dup2(fd_arr[0], STDIN_FILENO);
//Close unnecessary file descriptors.
close(fd_arr[0]);
close(fd_arr[1]);
//Wait for child.
int status = 0;
waitpid(pid, &status, WUNTRACED);
//Restore I/O.
dup2(INPUT, STDIN_FILENO);
dup2(OUTPUT, STDOUT_FILENO);
return errno;
}

C Program 0-1000 to Ordinary

How to write a C Program 0-1000 to Ordinary in C Programming Language ?


Solution For C Program :

/*C Program 0-1000 to Ordinary.*/

#include <stdio.h>
#include <string.h>
#include <limits.h>
#include <errno.h>

int main(void)
{
    char buffer[5];
    scanf("%4s", buffer);
    char c;
    if(fgetc(stdin) != '\n')
    {
        while((c=fgetc(stdin)) != '\n' && c!=EOF); // read rest of input
        printf("input has been truncated to: %s\n", buffer);
    }

    if(strcmp(buffer, "0") == 0 || strcmp(buffer, "000") == 0 || strcmp(buffer, "0000") == 0 || strcmp(buffer, "0000") == 0)
    {
        printf("zero\n");
        return 0;
    }

    int i = 0, n = strlen(buffer);
    while(i<n)
    {
        char* res;
        switch(n-i)
        {
            case 2:
            switch(buffer[i] - '0')
                {
                    case 0: res = ""; break;
                    case 1:
                        {
                            switch(buffer[i+1] - '0')
                            {
                                case 0: res = "ten"; break;
                                case 1: res = "eleven"; break;
                                case 2: res = "twelve"; break;
                                case 3: res = "thirteen"; break;
                                case 4: res = "fourteen"; break;
                                case 5: res = "fifteen"; break;
                                case 6: res = "sixteen"; break;
                                case 7: res = "seventeen"; break;
                                case 8: res = "eighteen"; break;
                                case 9: res = "nineteen"; break;
                                default: res = "error"; break;
                            }
                            i+=2;
                            continue;
                        }
                        break;
                    case 2: res = "twenty"; break;
                    case 3: res = "thirty"; break;
                    case 4: res = "forty"; break;
                    case 5: res = "fifty"; break;
                    case 6: res = "sixty"; break;
                    case 7: res = "seventy"; break;
                    case 8: res = "eighty"; break;
                    case 9: res = "ninety"; break;
                    default: res = "error"; break;
                }
                break;
            default:
            switch(buffer[i] - '0')
                {
                    case 0: res = ""; break;
                    case 1: res = "one"; break;
                    case 2: res = "two"; break;
                    case 3: res = "three"; break;
                    case 4: res = "four"; break;
                    case 5: res = "five"; break;
                    case 6: res = "six"; break;
                    case 7: res = "seven"; break;
                    case 8: res = "eight"; break;
                    case 9: res = "nine"; break;
                    default: res = "error"; break;
                }
                break;
    }
        if(res)
        {
            printf("%s ", res);
            res = "";
            if(buffer[i] != '0')
            {
                switch(n-i)
                {
                    case 4: res = "thousand "; break;
                    case 3: res = "hundred "; break;
                    case 2: break;
                    case 1: break;
                    default: break;
                }
                printf("%s", res);
            }
        }
        ++i;
    }
    printf("\n");
}

C Program Roboturtle

How to write a C Program Roboturtle in C Programming Language ?


Solution For C Program  :

/*C Program Roboturtle.*/

#include<stdio.h>
#include<string.h>
int walk,n,i,x=0,y=0,way=1,die=0;
char WAY='E',a[10];
main()
{
    scanf("%d",&n);
    for(i=0; i<n; i++)
    {
        scanf(" %s %d",&a,&walk);
        if(a[0]=='R' && a[1]=='T')
        {
            way++;
            if(way>3)
            {
                way=0;
            }
        }
        else if(a[0]=='L' && a[1]=='T')
        {
            way--;
            if(way<0)
            {
                way=3;
            }
        }
        else if(a[0]=='B' && a[1]=='W')
        {
            way-=2;
            if(way==-1)
            {
                way=3;
            }
            else if(way==-2)
            {
                way=2;
            }
        }
        if(way==0)
        {
            if(y+walk>=50000)
            {
                die=1;
            }
            y=y+walk;
            WAY='N';
        }
        else if(way==1)
        {
            if(x+walk>=50000)
            {
                die=1;
            }
            x=x+walk;
            WAY='E';
        }
        else if(way==2)
        {
            if(y-walk<=-50000)
            {
                die=1;
            }
            y=y-walk;
            WAY='S';
        }
        else if(way==3)
        {
            if(x-walk<=-50000)
            {
                die=1;
            }
            x=x-walk;
            WAY='W';
        }
    }
    if(die==1)
    {
        printf("DEAD");
    }
    else
    {
        printf("%d %d \n%c",x,y,WAY);
    }
}