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

 

linked list 1- insertion at begining of the list

                                                                                    click here for insertion at end

c program to insert the elements at the beginning of the 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:

/*       c program to insert the elements at the beginning of the linked list         */
#include<stdlib.h>
#include<stdio.h>
int main()
{
             typedef struct node_type{  /* structure declaration for a node */
             int dat;
             struct node_type *next;
             }node;
             node *head;  /* pointer to the start node */
             node *temp;  /* temporary pointer to a node   */
             int n;          /* to read data from user */
             char ch;
             head=NULL;  /* initializing list to be empty */
             printf("enter data?\n");
             printf("say y(yes) or n(no): ");
             scanf("%c",&ch);
             fflush(stdin);
             while((ch=='y')||(ch=='Y'))
             {
                     scanf("%d",&n);                             /* read data*/
                     temp=(node *)(malloc(sizeof(node)));  /* heap memory */
                     temp->dat=n;        /* store data in 'data part' of the node */
                     temp->next=head;       /* to insert at beginning of the list */
                     head=temp;
                     fflush(stdin);
                     printf("say y(yes) or n(no): ");
                     scanf("%c",&ch);
             }
              temp=head;
              while((temp)!=NULL){      /*  while list is not empty  */
                       printf("%d  ",temp->dat);
                       temp=temp->next;
             }
             return 0;
}

Sample input:

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

Output:

9  8  7  5
                                                                           click here for insertion at end

 

Fibonacci series(variant)-searching for a number in it


C program to know whether a particular number present in the Fibonacci series or not.

Sample input1:
Number to be searched: 5
Output1:
Found
 
Sample input2:
Number to be searched: 7
Output1:
Not Found
 
Program:
 
#include<stdio.h>
int main()
{
         int key;
         int f0=0,f1=1,f2;
         int a=0;
         printf("\nNumber to be searched: ");
         scanf("%d",&key);
         if((f0==key)||(key==f1))
                printf("\nFOUND");
         else
         {
                f2=f0+f1;
                while(f2<key)
                {
                         f2=f0+f1;
                         if(f2==key)
                         a=1;
                         else{
                         f0=f1;
                         f1=f2;
                         }
                }
                if(a)
                printf("\nFOUND");
                else
                printf("\nNot found");
         }
         return 0;
}

print the elements of a matrix of order n x m, in the spiral order

example:

input matrix:
1     2     3
4     5     6
7     8     9
Output should be 1 2 3 6 9 8 7 4 5


c program to read the matrix elements and to print them in the spiral order.

Program:

#include<stdio.h>
#include<stdlib.h>
int main()
{
              int left,right,top,bottom;
              int n,m;
              int i,j;
              printf("enter n, m: ");
              scanf("%d%d",&n,&m);
              int a[n][m];
              printf("enter matrix elements\n");
              /*     loop for reading matrix elements    */
              for(i=0;i<=(n-1);i++)                  /* for every row   */
                    for(j=0;j<=m-1;j++)             /* for every column in a row   */
                          scanf("%d",&a[i][j]);     /*  read element    */
              left=0;
              top=0;
              right=m-1;
              bottom=n-1;
        /* print every element only once in spiral order  */
              while((left<=right)&&(bottom>=top))    
              {
                      for(i=left;i<=right;i++)
                      {
                       printf("%d",a[top][i]);                  /* print top row from left to right  */
                       }
                       top++;                                         /* after completing one top row    */
                       for(i=top;i<=bottom;i++)
                      {
                      printf("%d",a[i][right]);             /* print right column from top to bottom */
                      }
                      right--;                                       /* after completing one right column  */
                      if(bottom>=top)                         /* check for bottom>=top   */
                      {
                              for(i=right;i>=left;i--)
                              {
                              printf("%d",a[bottom][i]);    /* print bottom row from right to left  */
                              }
                              bottom--;                              /* after completing one bottom row    */
                              for(i=bottom;i>=top;i--)
                              {
                              printf("%d",a[i][left]);            /* print left column from bottom to top  */
                              }
                              left++;                                   /* after completing one left column  */
                      }
              }
              return 0;
}

Sample input 1:
enter n, m: 3 5
enter matrix elements
1    2    3    4    5
6    7    8    9   10
11  12 13  14   15
Output1:
1 2 3 4 5 10 15 14 13 12 11 6 7 8 9

Plot sine wave using files

C program to plot the sine wave using files.

Introduction: If its needed to plot the sine wave, you should take values of sine function at different time instants and store them using file, and then using that file, you can plot the wave in "gnu plot" like tools.
Program:

/* c program to plot the sine wave using files */
#include<stdio.h>
#include<stdlib.h>
#include<math.h>                                                  /* for sin() function */
#define PI 3.141414                                               /* assigning PI to its value */
int main()
{
              int am,fm;                                                 /* amplitude and frequency */
              float t,temp;                                              /* t for time scale */
              float mt;
              FILE *pf;     /* file declaration */
              printf("enter am,fm: ");
              scanf("%d%d",&am,&fm);                      /* read am, fm from user */
              pf=fopen("lmn.txt","w");                         /* opening file on your disk to write the data */
              temp=2/(float)fm;
              for(t=0;t<temp;t=t+(temp/200))               /* loop for taking different values of sine function*/
             {
                     mt=am*sin(2*PI*fm*t);
                     fprintf(pf,"%f\t%f\n",t,mt);               /*  print to file */
             }
             fclose(pf);                                                  /* close the file*/
             return 0;
}

Sample input:
enter am,fm: 5 1000

Output:
To see the output open the folder that contains your program, there you can see the file named "lmn.txt" and open it. The file contains different values of sine function for different t(time).
 

Split a string using pointers

 

write a c program to split a string, using pointers.

input: contains a string of two names(first_name, last_name) separated by a 'space'.
output: print two strings, each with one name.

Sample input 1:
name( first_name<space>last_name): Graham Bell
Output 1:
first name: Graham
last name : Bell


Sample input 2:
name( first_name<space>last_name): GrahamBell
Output 2:
there is no space in the name.
exiting...


Program:

/* c program to split string into two using pointers */
#include<stdio.h>
#include<stdlib.h>
int main()
{
           char s[100];
           char ch;
           int i,j;
           char *ps,*pt;
           ps=s;
           pt=s;
           printf("name( first_name<space>last_name): ");
           scanf("%[^\n]",s);
           while((*ps)!=' '){
           if(*ps=='\0')                                                                         /* No space found till end of line */
              {
                  printf("there is no space in the name.\nexiting...");
                  exit(0);                                                                          /* exit from the program */
              }
           ps++;
           }
           *ps='\0';
           ++ps;
           printf("first name: %s",pt);
           printf("\nlast name : %s\n",ps);
           return 0;
}

Mailing list: A structure example


Design a structure to hold the data for a mailing list. Read and print the data.

C Program:

#include<stdio.h>
#include<string.h>
struct mailing_list {                                 /*    structure definition   */
         char name[50];
         char address_line1[50];
         char address_line2[50];
         char city[50];
         char state[20];
         long  int zip;
};
int main()
{
         int i,n;
         printf("Number of customers: ");
         scanf("%d",&n);
         fflush(stdin);
         struct mailing_list customer[n];
         printf("\n");
         for(i=0;i<n;i++)
         {
                 printf("customer %d address\n",i);
                 printf("name: ");
                 scanf("%[^\n]",customer[i].name);
                 printf("address_line1: ");
                 scanf(" %[^\n]",customer[i].address_line1);
                 printf("address_line2: ");
                 scanf(" %[^\n]",customer[i].address_line2);
                 printf("City: ");
                 scanf(" %[^\n]",customer[i].city);
                 printf("State: ");
                 scanf(" %[^\n]",customer[i].state);
                 printf("zip: ");
                 scanf("%ld",&customer[i].zip);
                 fflush(stdin);
          }
          printf("\n");
          for(i=0;i<n;i++)
          {
                 printf("customer %d address\n",i);
                 printf("Name: %s\n",customer[i].name);
                 printf("Address line1: %s\n",customer[i].address_line1);
                 printf("Address line2: %s\n",customer[i].address_line2);
                 printf("City: %s\n",customer[i].city);
                 printf("State: %s\n",customer[i].state);
                 printf("Zip: %ld\n",customer[i].zip);
                 printf("\n");
          }
          return 0;
}

Input:

Number of customers: 2

customer 0 address
name: pranith
address_line1: c nagar
address_line2: program nagar
City: c program
State: program
zip: 500001

customer 1 address
name: mahadev
address_line1: basic c nagar
address_line2: structure nagar
City: C
State: programming nagar
zip: 500002


Output:
customer 0 address
Name: pranith
Address line1: c nagar
Address line2: program nagar
City: c program
State: program
Zip: 500001


customer 1 address
Name: mahadev
Address line1: basic c nagar
Address line2: structure nagar
City: C
State: programming nagar
Zip: 500002



 

 

Recursive function that counts number of times a perticular number appears in the given array


write a function "count1(number, array, length) that counts the number of times 'number' appears in 'array'. The array has 'length' size. Use recursive function only.

Program:

#include<stdio.h>
int count1(int number,int array[],int length);
int count=0;
int main()
{
                  int i,length,number,count;
                  printf("enter the length of the array: ");
                  scanf("%d",&length);
                  int array[length];
                  printf(" enter array elements: ");
                  for(i=0;i<length;i++)
                  scanf("%d",&array[i]);
                  printf("enter the number to be counted: ");
                  scanf("%d",&number);
                  count=count1(number,array,length);
                  printf("%d",count);
                  return 0;
}
int count1(int number,int array[],int length)
{
                  if(length>0)
                 {
                           if(array[length-1]==number)
                           count++;
                           length--;
                           count1(number,array,length);
                           return count;
                 }
                 else return count;
}
 

Sample input and output:

enter the length of the array: 15
 enter array elements: 2 4 5 6 1 3  4  7 8  9  4  6 4 6 8
enter the number to be counted: 4

4

comparison of two strings


c program to compare whether two given strings are equal or not. Print True if two strings are equal. Otherwise print False.

Program:

#include<stdio.h>
#include<string.h>
int main()
{
             char string1[50];
             char string2[50];
             int a;
             printf("enter string1: ");
             scanf("%s",string1);
             printf("enter string2: ");
             scanf("%s",string2);
             a=strcmp(string1,string2);                /* strcmp() returns 0 if two strings are equal,
                                                                                                            returns 1 if not equal   */
            if(a==0)
           printf("\nTrue");
           else printf("\nFalse");
           return 0;
}


Sample input and output:

enter string1: india
enter string2: india
True
enter string1: india
enter string2: pakistan
False

Numbers to words


c program to convert numbers into words
Example:
input: 4232
output: four two three two
program:

#include<stdio.h>
#include<string.h>
int main()
{
         char number[50];
         printf("enter number");
         scanf("%s",&number);
         int len=strlen(number);
         printf("\n");
         int i;
         for(i=0;i<len;i++)
        {
                switch(number[i])
               {
                      case '0': printf("zero ");
                                   break;
                      case '1': printf("one ");
                                   break;
                      case '2': printf("two ");
                                  break;
                      case '3': printf("three ");
                                   break;
                      case '4': printf("four ");
                                   break;
                     case '5': printf("five ");
                                  break;
                     case '6': printf("six ");
                                  break;
                     case '7': printf("seven ");
                                  break;
                     case '8': printf("eight ");
                                  break;
                     case '9': printf("nine ");
                                  break;
                     default: printf("entered non number character");
             }
      }
 return 0;
}
 

Equivalent resistance of n parallel resistors


c program to calculate the equivalent resistance of n parallel resistors.



Program:

#include<stdio.h>
int main()
{
 int n;                     /*  number of resistors   */
 float er;                /*  equivalent resistance  */
 int r2;
 int i;                     /*  loop variable        */
 float temp;
 printf("enter number of resistors\n");
 scanf("%d",&n);
 printf("enter all resistances\n ");
 scanf("%f",&er);
 for(i=1;i<n;i++)
 {
     scanf("%d",&r2);
     temp=(1/er)+((1/(float)r2));        /* cast r2 to float  */
     er=(1/temp);
 }
 printf("\nequivalent resistance = %f",er);
 return 0;
}

Sample input and output:
enter number of resistors
3
enter all resistances
 100 100 50

equivalent resistance = 25.000000
 

checker board



Print a checker board (8-by-8 grid). Each square should be 5 -by-3
characters wide. A 2-by-2 example is as follows:(if you take 5 by 3 characters wide, then only you will get square.)








Program for 8by8 checker board:

#include<stdio.h>
int main()
{
         char c[33][49];                     /* cheker_board array */
         int i,j,k;                                 /* loop variables */
         for(i=0;i<33;i++)                   /* initializing all elements to white space */
         for(j=0;j<49;j++)
         c[i][j]=32;                        
         int temp;
         for(k=1;k<32;k++)                /* storing columns(unwanted elements will be
                                                                               replaced when rows are stored) */
         {
                for(j=0;j<49;j=j+6)
               {
                     c[k][j]='|';
               }
         }


         for(i=0;i<33;i=i+4)                 /* storing rows */
        {
                 j=0; c[i][j]='+';
                 for(k=0;k<8;k++)
                {
                        temp=j;
                        for(j=(temp+1);j<=(temp+5);j++)
                       {
                        c[i][j]='-';
                       }
                       c[i][j]='+';
               }
       }
       for(i=0;i<33;i++){                         /* print the 2-D array */
      for(j=0;j<49;j++)
      printf("%c",c[i][j]);
      printf("\n");
     }
      return 0;
     }

Output:

 

compressed string



Input Format:
First line contains the integer 'n' denoting the number of words in the dictionary s.t. 1 <= n <= 1,000
Second line would contain the first word.
It will be followed by 'n-1' lines each containing an integer and a trailing string. 
Note: The input is designed such that the integer will always be <= size of previous word formed
Example Input: 
4
india
3 hindu
5 muslim
6 christian


Output Format: 
Output a single string that is the last resulting word of the given dictionary
Example Output:
indhimchristian

Program:

#include<stdio.h>
#include<string.h>
int main()
{

       int n,a;
       char ch;
       char *ps;
       printf("enter nuber of words n:");
       scanf("%d",&n);
       char s[100];
       int i=0;
       fflush(stdin);
       printf("enter 1st word\n");
       scanf("%s",s);
       printf("enter integer space word\n");
       for(i=1;i<=n-1;i++)
       {
             scanf("%d",&a);
             scanf(" %s",s+a);
       }
        printf("%s",s);
        return 0;
}


Sample input and output:
enter nuber of words n:4
enter 1st word
india
enter integer space word
3 hindu
5 muslim
6 christian
indhimchristian



 

print right angled triangle



Give positive integer N
print N rows

Sample input and output:
enter N
6
0
00
000
0000
00000
000000


Program:

#include<stdio.h>
int main()
{
       int i,j,N;
       printf("enter N\n");
       scanf("%d",&N);
       for(i=1;i<=N;i++)
       {
            for(j=1;j<=i;j++)
            {
                 printf("0");
            }
            printf("\n");
       }
       return 0;
}