Wednesday, August 10, 2016

C program to read and print your name and address.

C program to read and print your name and address.

#include<stdio.h>
#include<string.h>
int main()
{
  char name[20], address[80];
  printf("Enter your name:");
  gets(name);
  printf("Enter your address:");
  gets(address);

  printf("=====================\n");
  printf("Your name: %s \n",name);
  printf("Your address: %s\n", address);
  printf("=====================\n");

  return 0;
}

Output of the Program

Enter your name:ABC PATEL
Enter your address:11-PQR Society, Ahmedabad, India.
=====================
Your name: ABC PATEL
Your address: 11-PQR Society, Ahmedabad, India.
=====================

C program to print your name and address(Static program).


C program to print your name and address(Static program).

#include<stdio.h>
int main()
{
            printf("Name: ABC PATEL \n");
            printf("Address: 11-PQR Society, Nr. Bank \n");
            printf("Ahmedabad, Gujarat, India.");
            return 0;
}



Output of the Program

Name: ABC PATEL
Address: 11-PQR Society, Nr. Bank
Ahmedabad, Gujarat, India.


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