Quiz-7


What is the output of the following code:

#include<stdio.h>
int main()
{
        int i=5;
        while(i<10)
        {
              printf("%d ",i);
              if(i==7)
              {
                   while(1)
                   {
                         printf("K ");
                         i++;
                         if(i==9)
                            break;
                   }
              }
              else printf("%d ",++i);
        }
        return 0;
}

Choices:

1) Compile error or execution stops.
2) 5 6 6 7 K K 9
3) 5 6 6 7 7 K K 9
4) 5 6 6 7  7 K K 9 10
5)None of the above(If your choice is 5, then provide your answer and explanation).
 
Answer is posted on our Facebook fan page  here 

Insertion sort

Program:

/***  insertion sort function   ***/
#include<stdio.h>
void insertionsort(int a[],int size);
#define MAX 10
int main()
{
         int a[MAX];
         int i;
         printf("enter %d elements:: ",MAX);
         for(i=0;i<MAX;i++)                     /** read the numbers to be sorted  **/
         scanf("%d",a+i);
         int length=MAX;
         insertionsort(a,length);              /**   function call  **/
         for(i=0;i<MAX;i++)                   /** print numbers in non-decreasing order **/
         printf("%d ",*(a+i));
         return 0;
}
void insertionsort(int a[],int size)
{
         int i,j,key;
         for(i=1;i<size;i++)
         {
                key=a[i];
                j=i-1;
               while((key<a[j])&&(j>=0))
               {
                     a[j+1]=a[j];
                     a[j]=key;
                      j--;
                }
          }
          return;
}
 

Sample input:

1 4 6 5 2 3 7 4 8 9

Output:

1 2 3 4 4 5 6 7 8 9
 
Algorithm:
 
for i=1 to length of array
                key=a[I]
                j=i-1
               while(key<a[j])AND(j>=0)
                     a[j+1]=a[j]
                     a[j]=key
                      j--
return array.

Quiz-6

Choose one of the following choices in relevance to the output of the following code.

#include<stdio.h>
int main()
{
      int a[20];
      int i=15;
      printf("a[%d]=",i++);
      printf("%d",++i);
      return 0;
}

1) Compile error (or) execution stops.
2) a[15]=17
3) a[16]=17
4) a[16]=16
5) a[15]=16
6) None of the above.

For Answer and Explanation visit our Facebook fan page here
 

Quiz5

Choose one of the choices given below.

#include<stdio.h>
int main()
{
   printf("%d%d",printf("hai"), printf("hello"));
   return 0;
}

Quiz 4

Choose one, among the given choices according to the output of the following code.
Code:
/* code to check collinear points(?)    */
#include<stdio.h>
int main()
{
      int a,b,c,d,e,f;
      a=1;b=1;c=1;d=2;e=1;f=3;
      if(((d-b)/(c-a))==((f-d)/(e-c)))
      printf("collinear points");
      else printf("Not");
      return 0;
}

Choices:

1) Compile error.
2) Compiles but stops execution.
3) Not
4) collinear points
5) None of the above.

Answer, explanation and trick(if any) for the above will be given on our Facebook page
                                                         visit Facebook page

Recursion example-3 (medium):: Length of longest contiguous increasing subsequence

Length of longest contiguous increasing subsequence.


Program:

#include<stdio.h>
int len_longseq(int, int, int);              /* function prototype  */
int max=-1;                                      /* declare max variable globally   */
int main()
{
       int n,prev;  
       printf("enter number of numbers");
       scanf("%d",&n);              
       if(n>0)
       {                                                      /* if n>0 proceed  */
             scanf("%d",&prev);                 /* read first integer  */
             int count=1;                       /* initialize count to 1  */
             if(n==1)                           /* if n=1 then print count=1 and stop  */
             printf("%d",count);
             else
            {                                                     /* if n>1   */
                    count=len_longseq(prev,n-1,1);          /* function call  */
                    printf("%d",count);                             /* print output  */
             }
        }          
        return 0;
}
int len_longseq(int prev,int n,int count)      /* function definition  */
{
    if(max<count)                                  /* to keep the length of longest subsequence so far */
           max=count;
    if(n==0)                                     /* base case if n=0,return max and stop  */
          return max;
    int x;
   scanf("%d",&x);                              /* read next number  */
   if(x>prev)                                   /* if read value is greater than previous read value  */
          return(len_longseq(x,n-1,++count));
 
   return (len_longseq(x,n-1, 1));               /* if read value is not greater than previous read value  */
}

Sample input:

enter number of numbers: 7
5  6  7  8  9  1  2

Output: 5

Recursion example-2 (easy)


Product of n numbers read from the input.

Program:

#include<stdio.h>
int read_mul(int);                                       /*  function prototype   */
int main()
{
       int n;
       printf("enter number of numbers");
       scanf("%d",&n);                                  /* read number of numbers  */
       printf("%d",read_mul(n));                   /* print product   */
       return 0;
}
int read_mul(int n)                                      /* function definition  */
{
       if(n==0)                                  /* base case i.e., if n=0 stop recursion and return 1 */
       return 1;
       int x;                                                     /* if n is not zero read and evaluate product  */
       scanf("%d",&x);
       return x*read_mul(n-1);                      /*  recursive function call   */

}

Sample input:

5
1 2 3 4 5

Output:
120

Recursion example-1(easy)


Sum of n numbers read from the input.

Program:

#include<stdio.h>
int read_sum(int);                                       /*  function prototype   */
int main()
{
       int n;
       printf("enter number of numbers");
       scanf("%d",&n);                                  /* read number of numbers  */
       printf("%d",read_sum(n));                   /* print sum   */
       return 0;
}
int read_sum(int n)                                      /* function definition  */
{
       if(n==0)                                  /* base case i.e., if n=0 stop recursion and return 0 */
       return 0;
       int x;                                                     /* if n is not zero read and evaluate sum  */
       scanf("%d",&x);
       return x+read_sum(n-1);                      /*  recursive function call   */

}

Sample input:

5
1 2 3 4 5

Output:
15

I Love You program in c




Program:                                                                  like us on facebook 

#include<stdio.h>
int main()
{
       char ch;
       char a[20][20];                  /*   2D array declaration   */
       printf("enter i or I");
       scanf(" %c",&ch);              /*  read input     */
       printf("\n\n\n");                  /* for new lines (optional)   */
       int i,j,k;
       switch(ch)
       {
             case 'I':
             case 'i':
                            for(i=0;i<8;i++){             /*   for every row   */
                            if(i==0||i==7){                 /* if row is either 1st or last   */
                            for(j=0;j<11;j++)             /* for every column   */
                            printf("\x03");                  /* print love symbol   */
                            printf("\n");
                             }
                            else{                                 /* if row is neither 1st nor last   */
                            printf("     \x03\n");
                            }
                            }
                            printf("\n\n\n");                /* after printing I, new lines   */

            case 'L':   for(i=0;i<10;i++)              /* 1st store love symbol in all element spaces  */
                           {
                                for(j=0;j<12;j++){
                                a[i][j]='\x03';
                                }
                           }
                          for(i=0;i<2;i++)          /* replace some of the above symbols with white spaces */
                         a[i][5]=' ';                     /* top space   */
                         for(i=2;i<10;i++)         /* left spaces   */
                         {
                               for(j=0;j<i-1;j++)
                               a[i][j]=' ';
                        }
                        for(i=2;i<10;i++){       /* right spaces   */
  
                            for(j=10;j>11-i;j--)
                            a[i][j]=' ';
                        }
                        for(i=0;i<10;i++){          /*  print */
                            for(j=0;j<11;j++)
                            printf("%c",a[i][j]);
                            printf("\n");
                        }
 case 'U':          for(i=0;i<8;i++){                      /* for every row   */
                         if(i!=7)                                      /* if its not last row   */
                               printf("\x03         \x03");
                        else{                                             /*   if its last row   */
                              for(j=0;j<11;j++)
                              printf("\x03");
                              }
                       printf("\n");
                       }
    
         }
 
 return 0;
 }
 
Note: Purposefully I have not put break statements and default statement in the switch for user friendly. You can add them at appropriate places.
 
Input:  Give input i or I.
 

quiz 3

Output of the following?

#include<stdio.h>
int main()
{
   int a,b,c,d,e,f;
   a=1;b=4;c=2;d=3;e=5;f=3;
   printf("%d",1-a+b*c%d/(e-f));
   return 0;
}

Choices:

a) compile error     b) execution stops     c) 0     d). 1     e) -3      f) 2    g)none
For explanation visit "click here for program" link below.



step.1: 1-a+b*c%d/(5-3)     ( brackets have highest priority among all)
step.2: 1-a+b*c%d/2
step.3: 1-a+4*2%d/2          ( *,/,% have same priority and have left to right associativity )
step.4: 1-a+8%3/2
step.5: 1-a+2/2
step.6: 1-a+1
step.7: 1-1+1                     ( +, - have same priority and have left to right associativity)
step.8: 0+1
step.9: 1
Now the expression returns its result. so 1 is printed
 
Don't want to miss updates from us!! follow us or subscribe to us.

C program to convert the given decimal number to its binary equivalent.

 


Program:

/* c program to convert the given decimal number into its binary equivalent. */
#include<stdio.h>
#define SIZE 80
struct stack{                      /*   defining a structure for stack    */
    int line[SIZE];
 int top;                       /* top index of stack  */
 };
void push(struct stack*, int);
int pop(struct stack*);
int main()
{
        struct stack s;  
        s.top=-1;                /* initializing  empty stack i.e, top index=-1 */
        int num;
        int rem,temp;
        printf("Decimal number to be converted: ");
        scanf("%d",&num);
        if(num==0)
            printf("%d",num);
        while(num>0)  /* while num>0, divide the number by 2 and store the remainder in stack  */
        {
               rem=num%2;
               push(&s,rem);
               num=num/2; 
        }
        printf("Binary number: ");
        while(s.top!=-1)        /* while stack is not empty */
        {
               temp=pop(&s);
               printf("%d ",temp);
        }
         return 0;
}
void push(struct stack *ps, int x)      /* function to push, ps is pointer to stack */
{
       if((ps->top)==SIZE-1)                /* if no more element can be accommodated */
      {
              printf("stack overflow");
              return;
      }
      int a=++(ps->top);                   /* increment top index   */
      ps->line[a]=x;                       /* push the element onto stack */
      return;
}
int pop(struct stack* ps)
{
       if(ps->top==-1)                        /* if stack is empty */
      {
             printf("stack underflow");
             return;
      }
      int x=(ps->line[(ps->top)]);           /* pop the value from the top of stack */
      (ps->top)--;                           /* decrement top index of stack   */
      return x;
}

Sample input and output:

Decimal number to be converted: 100
Binary number: 1 1 0 0 1 0 0
 
Now you can do some changes in the code to write the code for decimal to octal conversion.

C program to find out if a solved Sudoku is valid or invalid.


Object of this program is to find out if a solved Sudoku is valid or invalid.

/* when the program encounters "break;" statement, execution will come out of the innermost loop in which it present. */

Program:

#include<stdio.h>
int main()
{
         int a[9][9];
         int i,j,sum,temp,flag=1;
         printf("\n enter input::\n");
         for(i=0;i<9;i++){
             for(j=0;j<9;j++){
                  scanf("%d",&temp);
                  if(temp>0 && temp<10)          /*   check for valid input for Sudoku   */
                  a[i][j]=temp;
                  else{                               /* if any element is not invalid then successive breaks  */
                        flag=0;                             
                        printf(" invalid input ");
                        break;
                      }
             }
             if(flag==0)
             break;
         }
          if(flag)             /* if every input is in valid range then go for validation of Sudoku  */
         {
               for(i=0;i<9;i++)
               {
                    sum=0;
                    for(j=0;j<9;j++)
                    sum=sum+a[i][j];
                    if(sum!=45)
                       break;            /*   if sum is not 45 in any row then  go out of for loop  */
               }
               if(sum==45)                /*  if sum is 45 in all rows then check for columns   */
               {
                     for(i=0;i<9;i++)
                    {
                          sum=0;
                          for(j=0;j<9;j++)
                          sum=sum+a[j][i];
                          if(sum!=45)           
                          break;        /*   if sum is not 45 in any column then  go out of for loop  */
                    }
                    if(sum==45)                              /* if sum is 45 in all columns too    */
                       printf("Valid");
                    else printf("invalid");
               }
               else
                   printf("\n Invalid");
         }
         return 0;
}


Sample input:

1 9 4 8 7 5 6 2 3
8 5 3 9 6 2 4 7 1
2 6 7 3 1 4 9 8 5
9 2 1 6 5 8 7 3 4
6 7 8 4 3 1 2 5 9
4 3 5 2 9 7 8 1 6
5 8 6 7 4 3 1 9 2
3 4 2 1 8 9 5 6 7
7 1 9 5 2 6 3 4 8

Output:

Valid
 

Read into the computer a line of text containing both uppercase and lowercase letters. Printout the text with the uppercase and lowercase letters reversed, but all other characters intact.

Sample input:

I Love Programming.

Output:

i lOVE pROGRAMMING.

 Program:

/* c program to convert the upper case text to lower case text and vice versa    */
#include<stdio.h>
#include<ctype.h>
#include<string.h>
int main()
{
       char line[80];
       printf("enter some text: ");
       scanf(" %[^\n]",line);
       int len= strlen(line);
       int I;
       for(i=0;i<len;i++)
       {
                if(line[i]>='a' && line[i]<='z')
                        line[i]=toupper(line[i]);
                else if(line[i]>='A' && line[i]<='Z')
                        line[i]=tolower(line[i]);
        }
        printf("%s",line);
        return 0;
}

Note: Here the tests for lowercase and uppercase are not necessary. Think and comment if you need any help

quiz 2

What is the output of the following program? Read the choices before answering.

#include<stdio.h>
int main()
{
         int  a,b;
         printf(enter a,b: );
         scanf("%d%d",&a,b);
         printf("%d  %d",a,b);
         return 0;
}

Input:

4   5

choices:


1. compile error
2. compilation success, but program stops when run
3. 4   5
4. strange output
5. None of the above
 

quiz on printf()

what is the output of the following code?

#include<stdio.h>
int main()
{
         int a,b,c;
         a=1;
         b=1;c=2;
         printf("%d %d %d %d",a,b,c);
         return 0;
}

choose one answer among the following.

1. compile error
2. compiled, but stops when run
3. 1 1 2
4.strange output
5. None

Print the intermediate values between two successive elements. Let n=4, and array elements are 4, 2, 7, 5. The output would be ( 2 3 4 ) ( 2 3 4 5 6 7 ) ( 5 6 7 )


Program:

#include<stdio.h>
int main()
{
       int n;
       int i, j;
       printf("n: ");
       scanf("%d",&n);
       int a[n];
       printf("enter elements\n");
       for(i=0;i<n;i++)
       scanf("%d",&a[i]);
       for(i=0;i<n-1;i++)
       {
                 printf("( ");
                 if(a[i+1]>a[i])
                {
                        for(j=a[i];j<=a[i+1];j++)
                        printf("%d ",j);
                 }
                 else if(a[i+1]<a[i])
                 {
                       for(j=a[i+1];j<=a[i];j++)
                       printf("%d ",j);
                 }
                 else printf("%d %d ",a[i],a[i]);
                 printf(") ");
        }
        return 0;
}

How many times does each element occur in the given array? Let n=7, and the elements are: 12, 34, 32, 12, 36, 32, 32. The output should be: (12: 2); (34: 1); (32: 3); (36,1) but should not be (12: 2); (34: 1); (32: 3); (12: 2); (36: 1); (32: 3); (32: 3).


Program:

#include<stdio.h>
#define undefined 10000    /* take a value which is beyond the range of values in the program */
int main()
{
       int n;
       int i, j,count;
       printf("n: ");
       scanf("%d",&n);
       int a[n];
       printf("enter elements\n");
       for(i=0;i<n;i++)
       scanf("%d",&a[i]);
       for(i=0;i<n;i++)
      {
             count=1;
             if(a[i]!=undefined)
            for(j=i+1;j<n;j++)
            {
                  if(a[i]==a[j])
                  {
                         count++;
                         a[j]=undefined;
                   }
            }
            if(a[i]!=undefined)
            printf("(%d,%d); ",a[i],count);
     }
     return 0;
}

Point of intersection of two lines

Write a program that reads a, b, c, p, q and r. Let ax + by + c = 0 and px + qy + r = 0 be the equations
of two lines. Print their point of intersection.

Program:

#include<stdio.h>
int main()
{
      int a,b,c;
      int p,q,r;
      printf("a,b,c: ");
      scanf("%d%d%d",&a,&b,&c);
      printf("p,q,r: ");
      scanf("%d%d%d",&p,&q,&r);
      int x,y;
      x=(b*r-q*c)/(a*q-b*p);
      y=(p*c-a*r)/(a*q-b*p);
      printf("point of intersection (x,y)=(%d,%d)",x,y);
      return 0;
}

Sample input:

a,b,c: 4 8 12
p,q,r: 2 7 3
Sample output:
point of intersection (x,y)=(-5,1)



expand the given string if it contains a range

Objective of this program is to expand the given string if it
contains a range, for example a-f, 0-5. 

Following are considered valid range expressions: 
1. lower case letters between a and z.  Ex: a-f, d-i
2. Upper case letters between A and Z.  Ex: A-F, D-I
3. Numbers between 0 and 9. Ex: 1-5
4. All the above 3 in the reverse order. Ex: f-a, F-A, 3-0

In case of invalid sequences in the input string, sequence
should be copied as is. 

--------------------------------------
Sample Input       Sample Output
--------------------------------------
a-fuy0-3ZA-F       abcdefuy0123ZABCDEF
a-f6-3F-9          abcdef6543F-9
-ABCD-             -ABCD-
-------------------------------------



Program:
#include<stdio.h>
int main()
{
       int l,m,n,f,g;
       char a[1000];
       int i;
       int j;
       scanf("%s",a);
       int valid,validr;
       i=0;
       while(a[i]!='\0')
      {
              printf("%c",a[i]);
              i++;
             while((a[i]!='-')&&(a[i]!='\0'))
             {
                    printf("%c",a[i]);
                    i++;
             }
             if(a[i]!='\0')
            {
                   f=i-1;g=i+1;
                   valid=0;validr=0;
                   if(a[f]>='A' && ((a[g]>='A')&&(a[g]>=a[f])))
                   valid=1;
                   else if((a[f]>='a' && ((a[g]>='a')&&(a[g]>=a[f]))))
                   valid=1;
                   else if(a[f]>='0' && ((a[g]>='0')&&(a[g]>=a[f])))
                   valid=1;

                   else if((a[g]>='A' && ((a[f]>='A')&&(a[f]<=a[g]))))
                   validr=1;
                   else if((a[g]>='a' && ((a[f]>='a')&&(a[f]<=a[g]))))
                   validr=1;
                   else if((a[g]>='0' && ((a[f]<='9')&&(a[f]>=a[g]))))
                   validr=1;
                   if(valid)
                   {
                          for(j=a[i-1]+1;j<a[i+1];j++)
                          printf("%c",j);
                          i++;
                  }
                  else if(validr)
                 {
                         for(j=a[i-1]-1;j>a[i+1];j--)
                         printf("%c",j);
                         i++;
                 }
 
           }
           else break;
       }
       return 0;

equilateral triangle of stars.

 C program to print the equilateral triangle of stars

sample input and output:
 
 
 
 
 
 
 
 
 


Program:
 
#include<stdio.h>
int main()
{
    int height_pyramid;
    do
    {
        printf("how much height do tou want to print: ");
        scanf("%d",&height_pyramid);
    }while (height_pyramid < 1 || height_pyramid > 23);
    int i,j,k,n;
    n=height_pyramid;
    for (i=1, j=n; i<=j; i++,n--)
    {
        for(k=0;k<n-1;k++)
            printf(" ");
        for(k=0;k<i;k++)
            printf("* ");
        printf("\n");
    }
    return 0;
}


convert keyboard inputted seconds into days, hours, minutes and seconds



C program to convert keyboard inputted seconds into days, hours, minutes and seconds.

Program:
 
#include<stdio.h>
int main()
{
       int sec;
       int min,hrs,days,temp;
       printf("Total seconds: ");
       scanf("%d",&sec);
       days=(sec/86400);
       temp= sec%86400;
       hrs=temp/3600;
       temp=temp%3600;
       min=temp/60;
       sec=temp%60;
       printf("days: %d\t hours: %d\t minutes: %d\t seconds: %d",days,hrs,min,sec);
       return 0;
}

Sample input:

Total seconds: 86462

Output:

days: 1         hours: 0          minutes: 1           seconds: 2

Series of numbers

 

A series of numbers is as follows:
1, 1, 1, 3, 5, 9, 17, 31, 57, 105, . . . , An = An-1 + An-2 + An-3
Write a Computer Program to compute An for the user input n, where A1 = A2 = A3 = 1.

Program:

#include<stdio.h>
int main()
{
             int num;
             int A1=1,A2=1,A3=1;                 /* initialize to start the series */
             int A4,i;
             printf("enter num: ");
             scanf("%d",&num);
             for(i=4;i<=num;i++)                      /* starting from 4th term  */
            {
                       A4=A1+A2+A3;                 /* next term in the series */
                       A1=A2;
                       A2=A3;
                       A3=A4;
             }
             printf("%dth number in the series= %d",num,A4);
             return 0;
}

Sample input:

 enter num: 10

Output:

10th number in the series= 105

combinations of I, J, K and L, such that :I + J + K = L and I < J < K < L. I, J, K and L be positive integers from 1 to 15



Let I, J, K and L be positive integers from 1 to 15. Write a program that finds and prints all
combinations of I, J, K and L, such that :
                                                           I + J + K = L and I < J < K < L.

Program:

#include<stdio.h>
int main()
{
      int i,j,k,l;
      for(l=1;l<=15;l++)
      for(k=1;k<l;k++)
      for(j=1;j<k;j++)
      for(i=1;i<j;i++)
      {
            if((i+j+k)==l)
            printf("%d+%d+%d=%d \n",i,j,k,l);
       }
       return 0;
}

Output:

1+2+3=6
1+2+4=7
1+3+4=8
1+2+5=8
2+3+4=9
1+3+5=9
1+2+6=9
2+3+5=10
1+4+5=10
1+3+6=10
1+2+7=10
2+4+5=11
2+3+6=11
1+4+6=11
1+3+7=11
1+2+8=11
3+4+5=12
2+4+6=12
1+5+6=12
2+3+7=12
1+4+7=12
1+3+8=12
1+2+9=12
3+4+6=13
2+5+6=13
2+4+7=13
1+5+7=13
2+3+8=13
1+4+8=13
1+3+9=13
1+2+10=13
3+5+6=14
3+4+7=14
2+5+7=14
1+6+7=14
2+4+8=14
1+5+8=14
2+3+9=14
1+4+9=14
1+3+10=14
1+2+11=14
4+5+6=15
3+5+7=15
2+6+7=15
3+4+8=15
2+5+8=15
1+6+8=15
2+4+9=15
1+5+9=15
2+3+10=15
1+4+10=15
1+3+11=15
1+2+12=15
 

Interchange the last two digits of an inputted number


C program to interchange the last two digits of an inputted number.

Sample input:
12369

Output:
12396

Program:

#include<stdio.h>
int main()
{
         int num,temp1,temp2;
         printf("enter num: ");
         scanf("%d",&num);
         temp1=num%10;
         temp2=num/10;
         temp2=temp2%10;
         num=num-(10*temp2)+(10*temp1)-(temp1)+(temp2);
         printf("%d",num);
         return 0;
}
 

Delete the second last digit of an inputted number


C program to delete the second last digit.

Sample Input 12345, Output 1235
 

Program:

#include<stdio.h>
int main()
{
        int num,temp1,temp2;
        printf("enter num: ");
        scanf("%d",&num);
        temp1=num%10;                  /* store last digit in temp1   */
        num=num/10;                       /* making number one digit shorter   */
        temp2=num%10;                  /* store the second last digit in temp2   */
        num=num-temp2+temp1; /* add the last digit and subtract second last digit to shorter number  */
          printf("%d",num);
          return 0;
}

Double the last digit of an inputted number


 c program to double the last digit of an inputted number. Example: Input 72564, Output 72568
(Assume that the last digit is less than 5)

Program:

#include<stdio.h>
int main()
{
          int num,temp;
          printf("enter num: ");
          scanf("%d",&num);
          temp=num%10;
          if(temp<5)
          {
                  num=num+temp;
                  printf("%d",num);
          }
          else
          printf("enter number such that its last digit is less than 5.");
          return 0;
}

Input:

12454

Output:

12458

 

g(x, y) = f(x) + f3(y)

#include<stdio.h>
#include<math.h>
int f(int, int, int);                                  /* function prototype for f(x)   */
int main()
{
          int a,b;
          int x,y;
          double g;                                           /*       g=g(x,y)         */
          printf("a, b: ");
          scanf("%d%d",&a, &b);
          printf("x,y: ");
          scanf("%d%d",&x, &y);
          g=(f(x,a,b))+(pow((f(y,a,b)),3));             /* make sure that return value of pow()
                                                                                                      function is assigned to double  */
          printf("g(%d,%d)=%g",x,y,g);
          return 0;
}
int f(int x, int a, int b)
{
         return(a*x+b);
}

Sample input:

a, b: 5 6
x, y: 2 6

Output:

g(2,6)=46672

 

Linked list 2- insertion at the end of the list

insertion at beginning of linked list

C program to insert the elements at the end of a linked list and to print them from starting node to end node.

Introduction: When you don't know the size of input data, prior to the compilation, using a large size array in the program is not efficient method. So take the advantage of linked list that it takes the memory at the execution time with the help of 'malloc' function according to the size of the input.

Program:

#include<stdio.h>
#include<stdlib.h>
int main()
{
                  typedef struct node_type{                       /* structure for a node */
                  int data;
                  struct node_type *next;
                  }node;
                  node *head;                                           /* pointer to start node */
                  node *tail;                                          /* pointer to tail of the node */
                  node *temp;                                      /* temporary pointer to a node*/
                  head=tail=NULL;    /* initializing empty list */
                  char ch;
                  int n;
                  printf("enter data?\n");
                  printf("say y(yes) or n(no): ");
                  scanf("%c",&ch);
                  fflush(stdin);
                  if((ch=='y')||(ch=='Y'))
                 {
                           scanf("%d",&n);
                           head=(node *)malloc(sizeof(node)); /* only for first element */
                           head->data=n;
                           head->next=NULL;
                           tail=head;
                           fflush(stdin);
                           printf("say y(yes) or n(no): ");
                           scanf("%c",&ch);
                           fflush(stdin);
                          while((ch=='y')||(ch=='Y'))
                          {
                                    scanf("%d",&n);
                                    fflush(stdin);
                                    tail->next=(node *)malloc(sizeof(node));        /* for the rest of elements */
                                    tail->next->data=n;  
                                    tail->next->next=NULL;
                                    tail=tail->next;
                                    printf("say y(yes) or n(no): ");
                                    scanf("%c",&ch);
                                    fflush(stdin);
                          }
                          temp=head;
                          while((temp)!=NULL){                            /*  while list is not empty, print elements  */
                                      printf("%d ",temp->data);
                                      temp=temp->next;
                          }
               }
               else printf("list is empty");
               return 0;
}

Sample input1:
enter data?say y(yes) or n(no): y
9
say y(yes) or n(no): y
8
say y(yes) or n(no): y
7
say y(yes) or n(no): y
6
say y(yes) or n(no): y
5
say y(yes) or n(no): n


Output1:
9 8 7 6 5

Sample input2:
enter data?
say y(yes) or n(no): n

Output2:
list is empty
                                                    insertion at the beginning of linked list