How to learn and master C language

If you want to master the C language I propose you my approach which I think is best for a student.

Here I am assuming that you don't have any programming or CS background.

here goes the list..

1. NPTEL :: Computer Science and Engineering , is a simple introductory online course from IIT Kanpur, You can download and watch them whenever you want.

2. Introduction to Computer Science , This is a similar course from Harvard university but digs in deep. It challenges you for few weeks but its possible to complete with medium efforts. The main learning, in this course lies in solving the assignments. So don't skip those.

3. The C Programming Language, this is a book from Dennis Ritchie. It needs your patience to complete this. But you will learn much from this.

Do you think, now that you are mastered? Yes, if you keep in touch with programming. No, otherwise.

If you cannot invest that much time, try to complete the first course listed above, and keep in touch with programming..

That's all you need to do..I hope it helps somebody..  If you like it, share it and let others know it..

Feel free to ask any questions.

Honeywell interview experience


  • on-campus,  No CGPA cut-off
  • First Round: Written test comprising the questions from core subjects(I am from telecommunication systems engg)
  • Second Round : Technical Personal Interview
    • everything about C
    • what is static, #define, typedef and difference between the last two
    • macro function
    • pointers to constants and constant pointers difference with example codes
    • postfix and prefix with live example
    • stack push operation, explanation and syntax for data structure used for the stack
    • why don't you learn machine learning, oops, IoT?
    • what differentiates you from a student from normal colleges(I am from IIT)
    • why honeywell?
    • Where do you want to see yourself in two years
  • Finally I was expecting HR round but there was no such for me. In the final selected list my name was in wait list.
  • My observations:
    • Their time-management for interview was bad and hence there was no time for them to conduct HR round for some of us(I am one among such).
    • They give lots of importance to those who used C programming for their academic project.
 If you have something, feel free to ask. I hope it helps some people.

Brute Force method to operate on every possible set of an array

Problem Statement: Given an array, operate (possibly sum/product) on every possible non-empty subset.
Its helpful in so many contexts if you have no idea to solve the problem optimally. It runs in exponential time.

Given n, m. There are m elements in the array. You have to find the number of ways in which given sum = n can be obtained  using non-empty subsets of the array.

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

long long coinChange(int cost[], int m, int n){
    char binary[m];
    memset(binary, 0, sizeof(binary));
    long long x = (long long)pow(2, m) ;
    long long i, ans = 0;
    for (i = 1; i < x; i++){
        int j = m-1;
        while (binary[j] != 0 && j >= 0){
            binary[j] = 0;
            j--;
        }
        binary[j] = 1;
        int k;
        long long sum = 0;
        for (k = 0; k < m; k++){
            if (binary[k] == 1)
                sum += cost[k];
        }
        if (sum == n)
            ans++;
     
    }
    return ans;
}

int main() {
    int n, m;
    scanf("%d%d", &n, &m);
    int cost[m];
    int i;
    for (i = 0; i < m; i++)
        scanf("%d", &cost[i]);
    printf("%lld", coinChange(cost, m, n));
    return 0;
}

How to read mix of integers and characters from input?

To Better understand the essence of this program try the below quiz program before proceeding

To begin with, you must understand how scanf and keyboard buffers work when try to read the input.

when you read an integer with a statement like

scanf("%d", integer); // integer is of int type.
you will type integer(say 6) followed by enter key.
The keyboard buffer strores 6 and "new line"('\n').
the first scanf statement takes one integer from the keyboard buffer and as a result the new line character remains in keyboard buffer.
the next scanf statement takes this character.
scanf("%c", character) // character is of char type
So that the character variable takes that newline character into it.

Our intended function of those two scanf statements is to read one integer followed by some code to process that data, followed by a character input as in following program;

#include <stdio.h>
int main()
{
      int a, b, c;
      printf("\nenter a: ");
      scanf("%d", &a);
      printf("enter b: ");
      scanf("%d", &b);
      c = a+ b;
      char ch;
      printf("enter character") ;
      scanf("%c", &ch);
      printf("\nthe entered character is: %c", ch);
      return 0;
}

As pointed out earlier, the problem was due to the storage of new line character in the keyboard buffer.
So add a statement while((ch = getchar()) != '\n'); inbetween the statements for reading integer and reading character. This statement takes the newline in the buffer into it. So the scanf statement for charcter input behaves as expected.

The modified code is the following:
#include <stdio.h>
int main()
{
      int a, b, c;
      printf("\nenter a: ");
      scanf("%d", &a);
      printf("enter b: ");
      scanf("%d", &b);
      c = a+ b;
      char ch;
      printf("enter character: ") ;
      while((ch = getchar()) != '\n');
      scanf("%c", &ch);
      printf("the entered character is: %c", ch);
      return 0;
}

Note: There are some alternatives for the same purpose, if you are interested, discuss in comments section.

Quiz 8

This is sensible question. You may not understand the purpose of this question until you compile and run. However, I strongly recommend you to trace the output for your input and check your result with original result by running it in your computer.


#include <stdio.h>
int main()
{
      int a, b, c;
      printf("\nenter a: ");
      scanf("%d", &a);
      printf("enter b: ");
      scanf("%d", &b);
      c = a+ b;
      char ch;
      printf("enter character") ;
      scanf("%c", &ch);
      printf("\nthe entered character is: %c", ch);
      return 0;
}

Explanation: how to read mix of integers and characters

how to upgrade to windows 10 from windows 8.1

If you want to upgrade to windows 10 from windows 8.1 then follow* this procedure.

step1 : make sure you have installed all updates from windows. This is done as follows.
           control panel > search for update in control panel search box > click on windows update > check for updates > if there are any updates, install them. once again make sure, you have installed all updates asa same as above.

step2 : download file from
step 3: run the file
step 4 : click on upgrade this pc.
now the download should start. 
if you got error like "something happened", then do the following
 open control panel > search for region > click on region > click on administrative > chane local> put english(united states) in that and click ok
now run that downloaded file again.
your download should start now.
be patient ... you need to do nothing until it gets downloaded 100%
then some processes start by themselves.
be patient..
in the entire process, it asks your permission thrice only.
You got windows 10 on your PC.
All the best.

If you have some issue/solution, Feel free to share here.

*this is for information only. There is no guarantee that it will work for u.

Simple calculator

Problem Statement: Execute simple arithmetic operations sum, subtract, multiplication, division on integers.

How its different from others on internet :  There are so many websites which have code for simple calculator program. However most of them, asks for two integers first and then asks for operator which is not the case in real time calculator. In this code, you can directly enter expressions like        76 * 75, which is similar to real time calculator.

Input : operand1 operator operand2
Output : result of the input arithmetic expression.

Code:

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

float calculate(int op1, char op, int op2, unsigned short * flagptr); // calculator function
int main()
{
 char oparr1[10], oparr2[10];
 char op;
 float result;
 unsigned short flag = 1;   // flag which indicates any error in input
 unsigned short *flagptr = &flag; // pointer to the above flag
 printf("enter expression operand1 operator operand2\n");
 int ch;
 ch = getchar();  // start reading input
 int i= 0;
 while(ch >= '0' && ch <= '9')   //store in array of operand1 if its number
 {
    oparr1[i] = ch;
    ch = getchar();
    i++;
 }
 while(ch == ' ')       // skip all the spaces between first operand and operator, if any
 {
    ch = getchar();
 }
 op = ch;      // store operator in op
 ch = getchar(); 
 while(ch == ' ')
 {
    ch = getchar();
 }
 i= 0;
 while(ch >= '0' && ch <= '9')      //start reading operand2
 {
    oparr2[i] = ch;
    ch = getchar();
    i++;
 }
 int op1, op2;
 op1 = atoi(oparr1);     // convert char array of operand1 to integer
 op2 = atoi(oparr2);
 result = calculate(op1, op, op2, flagptr);
 if (flag == 1)              // no error in input
    printf("result = %f\n", result);
 else
    printf("undefined result\n");
 return 0;
}

float calculate(int op1, char op, int op2, unsigned short *flagptr)
{
    float result;
    switch(op)
    {
        case '+': result = op1 + op2;
                break;
        case '-': result = op1 - op2;
                break;
        case '*': result = op1 * op2;
                break;
        case '/':
                if (op2 != 0)
                    result = (float)op1/op2;
                else
                    {
                       printf("divide by zero error\n");
                       *flagptr = 0;   // indicate error in input
                    }

                break;
        default : printf("Use integers only\nUse +, -, * and / only\n");
                     *flagptr = 0;
                      break;

    }
    return result;
}
 



Sample input: 25 * 25
Output : 625.00000

What can be improved? 
Ans: Well, this program works only for integers. Try for floating point values and use it as your own calculator. Discuss in comments, how to implement it for float values.

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