Sunday, July 31, 2016

Rules for defining Variable Name in C language

In C language any user defined name is called identifier. This name may be variable name, function name, goto label name, any other data type name like structure, union, enum names or typedef name.

We need to follow following rules while declaring variables (identifiers) in C program.

Rule 1:  Name of identifier includes alphabets, digit and underscore.

Example of Valid variable name: india, add12, total_of_number etc.

Example of Invalid variable name: show*marks, factorial#, avg value etc.

Rule 2: First character of any identifier must be either alphabets or underscore.

Valid name:  _total, _3, a_,   etc.

Invalid name: 3_, 10_function, 12345 etc.

Rule 3: Name of identifier cannot be any keyword of c program.

Invalid name: int, float, char, if, else, for, switch, etc

Rule 4: Name of identifier is case sensitive i.e. num and Num are two different variables.

Rule 5: Only first 31 characters are significant of   identifier name.

Example:

abcdefghijklmnopqrstuvwxyz123456aaa, 

abcdefghijklmnopqrstuvwxyz123456bbb, 

abcdefghijklmnopqrstuvwxyz123456cad

All three identifier name are same because only first 31 characters has meaning. Rest has not any importance.

Rule 6: Must not contain white space.

Valid name: MyMarks

Invalid name: My Marks

* * * * *

<Back to Home>

Saturday, July 2, 2016

C Program to print A to Z using for loop.

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

 for(i=65; i<=90; i++)
 {
  printf("%c ",i);
 }
 return 0;
}

Output of the program:
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z


C Program to print series 10 20 30 40 50 ... N

C Program to print series 10 20 30 40 50 ... N for the given value of N.


For example, if N=5, output should be
10 20 30 40 50



#include<stdio.h>
void main()
{
 int i, N;
 printf("Enter N:");
 scanf("%d", &N);

 for(i=1; i<=N; i++)
 {
     printf("%d ",i*10);
 }
}


Output of program

Enter N:5
10 20 30 40 50

C program to print Even number series 2 4 6 8 ... N.

C Program to print Even number series 2 4 6 8 ... N for the given value of N.

For example, if N=10, output should be
2 4 6 8 10

Program Code

#include<stdio.h>
int main()
{
  int i, N;
  printf("Enter N:");
  scanf("%d", &N);

  for(i=2; i<=N; i=i+2)
  {
    printf("%d ",i);
  }
  return 0;
}

Program Code

Enter N:10
2 4 6 8 10




C Program to print Odd number series 1 3 5 7 ... N for the given value of N.



C Program to print Odd number series 1 3 5 7 ... N for the given value of N.

For example, if N=10, output should be
1 3 5 7 9


#include<stdio.h>
int main()
{
 int i, N;
 printf("Enter N:");
 scanf("%d", &N);

 for(i=1; i<=N; i=i+2)
 {
   printf("%d ",i);
 }
 return 0;
}

Output of program

Enter N:10
1 3 5 7 9

C Program to print series 5, 4, 3, 2, 1 for the given value of N.


C Program to print series 5, 4, 3, 2, 1 for the given value of N.

For example, if N=5, output should be
5, 4, 3, 2, 1


#include<stdio.h>
int main()
{
 int i, N;
 printf("Enter N:");
 scanf("%d", &N);

 for(i=N; i>0; i--)
 {
   printf("%d",i);
   //Logic avoid last , 
   if(i!=1)
     printf(", ");
 }
 return 0;
}

Output of program

Enter N:5
5, 4, 3, 2, 1


C Program to print series 1, 2, 3, 4, 5.....N for the given value of N.

C Program to print series 1, 2, 3, 4, 5.....N for the given value of N.
For example, if N=5, output should be
1, 2, 3, 4, 5


#include<stdio.h>
int main()
{
 int i, N;
 printf("Enter N:");
 scanf("%d", &N);

 for(i=1; i<=N; i++)
 {
  printf("%d",i);
 
  //Logic avoid last , 
  if(i!=N)
   printf(", ");
 }
 return 0; }

Output of program
Enter N:5
1, 2, 3, 4, 5


Friday, July 1, 2016

C Program to Calculate Simple Interest based on given values.


#include<stdio.h>
int main()
{
 float p, r, n, si;
 
 printf("Enter your principle amount:");
 scanf("%f",&p);
 
 printf("Enter your rate of interest:");
 scanf("%f",&r);
 
 printf("Enter number of years:");
 scanf("%f",&n);
 
 si = (p*r*n)/100;
 
 printf("Simple Interest is %.2f", si);
 return 0;
}

Output of program

Enter your principle amount:1000
Enter your rate of interest:12
Enter number of years:2
Simple Interest is 240.00

C Program to Print inverted Number Pattern



C Program to Print inverted Number Pattern as given below:
For N=5,

5 4 3 2 1
5 4 3 2
5 4 3
5 4
5


#include<stdio.h>
int main()
{
 int i, j, n=5;

 for(i=0; i<5; i++)
 {
  for(j=5; j>i; j--)
  {
    printf("%d ",j);
  }
    printf("\n");
 }
 return 0; 
}

Output of program

5 4 3 2 1
5 4 3 2
5 4 3
5 4
5


C Program to Print given number Pattern.


C Program to Print Number Pattern as given below:
For N=5,

1
1 2
1 2 3
1 2 3 4
1 2 3 4 5

#include<stdio.h>
int main()
{
 int i, j, n=5;

 for(i=0; i<5; i++) //this loop tracks no. of lines
 {
  for(j=0; j<=i; j++)//this loop tracks numbers in a row
  {
    printf("%d ",j+1); //this line prints a number
  }
    printf("\n");
 } 
 return 0;
}

Output of program

1
1 2 
1 2 3 
1 2 3 4 
1 2 3 4 5

 

Algorithm to C program conversion

/* Example of Algorithm to C program conversion.

Problem Definition: Write an algorithm to add given two numbers.

Step 1: Start.
Step 2: Read two numbers A and B.
Step 3: Total = A+B.
Step 4: Display Total.
Step 5: Stop.

*/

#include<stdio.h>
int main()
{
 int A, B, Answer;

 printf("Enter number A:");
 scanf("%d",&A);

 printf("Enter number B:");
 scanf("%d",&B);

 Answer = A + B;
 printf("%d", Answer);
 return 0;
}


Output of program:

Enter number A:5
Enter number B:10
15