Tuesday, May 23, 2017

Formal Parameters

Parameters written in a function definition are called the Formal Parameters. If a function is designed to use arguments, it must declare variables which accept values passed through function arguments. These variables are called the formal parameters of the function. They are like local variables inside the function. As mentioned in the following program; they are declared after the function name inside parenthesis. Formal parameters are separated by comma in function definition.

int sum(int num1, int num2)
{
  int answer;
  answer = num1 + num2;
  return answer;
}


The function sum( ) has two formal parameters: num1 and num2. This function returns answer to the calling function. The formal parameters receive the value of the arguments passed to the function. Remember that that, as local variables, they are destroyed upon exit from the function.


Predict C Program Output-2 Answer



Program-1
#include<stdio.h>
int main()
{
  printf("Hello");
  printf("\rIndia");
  return 0;
}
Output: India
Justification: \r is linefeed character.

Program-2
#include<stdio.h>
int main()
{
  int number = - -5;
  printf("number = %d",number);
  return 0;
}
Output: number = 5
Justification: Here, unary minus sign used twice. Hence minus * minus becomes plus.

Program-3
#include<stdio.h>
#define float char
int main()
{
  float num=65;
  printf("Size of(num) = %d",sizeof(num));
  return 0;
}
Output: Size of(num) = 1
Justification: #define replaces the string float by char, hence num becomes char type variable.

Program-4
#include<stdio.h>
enum colors {RED, BLUE, GREEN};
int main()
{
  printf("%d, %d, %d",BLUE, RED, GREEN);
  return 0;
}
Output: 1, 0, 2
Justification: By default, enum assigns numbers starting from 0 to its variables.

Program-5
#include<stdio.h>
void display(void);
int main()
{
  int i=1;
  for(i=0; i<=5; i++)
  {
     printf("%d",i);
     if (i>2)
        goto ABC;
  }
}
display()
{
  ABC:
  printf("Hello World");
}
Output: Compile time error.
Justification: goto and Label(ABC) is not defined in same function.

Predict C Program Output - 2



Predict the output of following C programs.

Program-1
#include<stdio.h>
int main()
{
 printf("Hello");
 printf("\rIndia");
 return 0;
}

Program-2
#include<stdio.h>
int main()
{
  int number = - -5;
  printf("number = %d",number);
  return 0;
}

Program-3
#include<stdio.h>
#define float char
int main()
{
  float num=65;
  printf("Size of(num) = %d",sizeof(num));
  return 0;
}

Program-4
#include<stdio.h>
enum colors {RED, BLUE, GREEN};
int main()
{
  printf("%d, %d, %d",BLUE, RED, GREEN);
  return 0;
}

Program-5
#include<stdio.h>
void display(void);
int main()
{
  int i=1;
  for(i=0; i<=5; i++)
  {
       printf("%d",i);
       if (i>2)
             goto ABC;
  }
}
display()
{
  ABC:
  printf("Hello World");

Sunday, May 21, 2017

C Programming Aptitude Set-1 with answer


C Programming Aptitude Question Set-1 (with output and justification)

Predict the output of following programs with justification.

Question - 1

#include<stdio.h>
int main()
{
    char name[ ]="INDIA";
    int i;
           
    for(i=0; name[i]; i++)
       printf("%c%c%c ",name[i],*(name+i),*(i+name));
    return 0;
}

/*
Output:
III NNN DDD III AAA

Justification: name[i], *(name+i) and *(i+name) are evaluated to same value.
*/

Question - 2

#include<stdio.h>
int main()
{
    int const marks=5;
    printf("%d",marks++);
    return 0;
}

/*
Output: Compiler error

Justification: Cannot modify a constant value.
*/

Question - 3

#include<stdio.h>
int main()
{
  static int number = 3;
  printf("%d ",number--);

  if(number)
main();

  return 0;
}

/*
Output:
3 2 1

Justification:
static storage class is initialized once. The value of a static variable is retained even between the function calls. Main is also function, which can be called recursively. This main function will be called until
value of number remains true(i.e. > 0)
*/

Question - 4

#include<stdio.h>
int main()
{
  float number1 = 5.1;
  double number2 = 5.1;
  if(number1 == number2)
printf("Equal numbers.");
  else
printf("Not equal numbers.");
}

/*
Output:
Not equal numbers.

Justification:
Values of floating point numbers (float, double, long double) are rounded after its defined precision value. Float takes 4 bytes and long double takes 10 bytes. So precision(rounded) value of float is different than long double.

Note: Be cautious while using floating point numbers with relational operators like == , >, <, <=, >= and != .
*/

Question - 5

#include<stdio.h>
int main()
{
   char ch='A';
   switch(ch)
   {
      default : printf("Error");
      case 'A': printf("Hello - A");
                break;
      case 'B': printf("Hello B");
                break;
      case 'C': printf("Hello C");
                break;
   }
}
/*
Output : Hello - A

Justification:
default case can be placed anywhere inside the loop. It will be executed only when other cases does not match.
*/


C Programming Aptitude Set-1

Predict the output of following programs with justification.
Question - 1

#include<stdio.h>
int main()
{
  char name[ ]="INDIA";
  int i;
 
  for(i=0; name[i]; i++)
printf("%c%c%c ",name[i],*(name+i),*(i+name));
  return 0;
}

Question - 2

#include<stdio.h>
int main()
{
  int const marks=5;

  printf("%d",marks++);

  return 0;
}

Question - 3

#include<stdio.h>
int main()
{
  static int number = 3;
  printf("%d ",number--);

  if(number)
main();

  return 0;
}

Question - 4

#include<stdio.h>
int main()
{
  float number1 = 5.1;
  double number2 = 5.1;
  if(number1 == number2)
printf("Equal numbers.");
  else
printf("Not equal numbers.");
}

Question - 5

#include<stdio.h>
int main()
{
  char ch='A';
  switch(ch)
  {
default : printf("Error");
case 'A': printf("Hello - A");
 break;
case 'B': printf("Hello B");
 break;
case 'C':   printf("Hello C");
 break;
  }
}

Click here to check your answers.

Global variables

Global variables are available throughout the C program and may be used by any function and part of a program. Also, their value can be used in any part of C program during execution. We can create global variables by declaring them outside of any function. Generally we declare global variables in the beginning of C program. In the following program, the variable percentage is declared outside of all functions. We can use percentage in all function including main().

#include <stdio.h>
//Declaration of global variables.
int maths, science, english;
float percentage;

void result(void);
void display(void);

int main(void)
{
printf("Enter Subject Marks out of 100.\n");
printf("-------------------------------\n");
printf("Enter Marks of Maths:");
scanf("%d",&maths);
printf("Enter Marks of Science:");
scanf("%d",&science);
printf("Enter Marks of English:");
scanf("%d",&english);

result();
display();

return 0;
}
void result(void)
{
percentage = (maths+science+english)*100/300;
}
void display(void)
{
printf("Your percentage is %.2f.", percentage);
}

Output of Program

Enter Subject Marks out of 100.
-------------------------------
Enter Marks of Maths:80
Enter Marks of Science:90
Enter Marks of English:85
Your percentage is 85.00.

Friday, May 19, 2017

Multiplication Table

C Program to Print Multiplication Table of a given Number.

#include<stdio.h>
int main()
{
 int i, table_number;

 printf("Enter Table Number to be printed: ");
 scanf("%d",&table_number);

 printf("\n Multiplication Table of %d",table_number);
 printf("\n --------------------------");

 for(i=1;i<=10;i++)
 {
     printf("\n %d * %d =  %d",table_number,i,(table_number*i));
 }
 return 0;
}

Output of program


Compound Assignment



Compound Assignment

The compound assignment operators consist of a binary operator and the simple assignment operator. They perform the operation of the binary operator on both operands and store the result of that operation into the left operand.

The following table lists the compound assignment operators, example and its equivalent expressions:


Operator
Example
Equivalent expression

+=
number += 5;
number = number + 5;
-=
number -= 5;
number = number – 5;
*=
number *= 5;
number = number * 5;
/=
number /= 5;
number = number / 5;
&=
number &= 2;
number = number & 2;
%=
number %= 2;
number = number % 2;
^=
number ^= 2;
number = number ^ 2;
Compound assignment statement helps to shortens the expression and program length.

Saturday, May 13, 2017

C program without Semicolon




C program without Semicolon.

#include<stdio.h>
void main()
{
    if(printf("Hello World"))
    {
   
    }
}

Output of program

Hello World

Note: Here printf( ) function is passed as a parameter to if statement; hence semi-colon is not needed at the end of printf() statement.


Wednesday, May 10, 2017

Importing content from other file to c program.



Importing content from other file to c program.

#include<stdio.h>
#include "kp.c"
int main()
{
char state_name[20];
p("Enter your state name:");
s("%s",state_name);

p("Your state name is %s", state_name);

return 0;
}

Output of program

Enter your state name:Gujarat
Your state name is Gujarat

Note:

kp.c file is stored in the same directory where above program is stored. Content of kp.c file is given below.

#define p printf
#define s scanf

fopen() validation

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

int main()
{
    FILE *fp = fopen("input.txt", "w");
    if (fp == NULL)
    {
        printf("File open error..");
        exit(0);
    }
    else
    {
        printf("File opened successfully..", fp);
        //Add your C File Management logic here.
    }
    fclose(fp);
    return 0;
}

Output of program

File opened successfully..

Monday, May 8, 2017

Flowchart of Area of Circle

Flowchart of Simple Interest

Simple Interest Calculation of Savings Bank Accounts / Loan Accounts


Following flowchart describes the process of simple interest calculation. 

Simple Interest calculation is one of the routine activity performed by Banks. Savings Bank account holder gets interests on their deposits in bank. Loan takers from the bank pay the interest to the bank. 

In the following flowchart, P is Principle amount, R is Rate of Interest, N is number of years and I is used for Simple Interest. 

Formula to calculate Simple Interest (I) based on given P, R and N is as under:

I  = (P * R * N) / 100

  
Simple Interest - flowchart


Fig. Flowchart to Calculate Simple Interest

Here, P = Principal amount, R = Rate of Interest, N = No. of years and I = Simple Interest.





Sunday, May 7, 2017

Number to word conversion


C program for number to word conversion.

#include<stdio.h>
#include<conio.h>
int main()
{
  char  number[20] ;  
  int i;

  printf("Enter number: ");
  gets(number);

  printf("%s in words:", number);
  
  for(i=0 ; number[i] != '\0' ; 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("Error ");
  break;
    }
  }
  return 0;
}

Run-1 Output of program:

Enter number: 4321
4321 in words:Four Three Two One

Run-2 Output of program:

Enter number: 12AB34
12AB34 in words:One Two Error Error Three Four