Wednesday, August 17, 2016

Use of #define in C program.

Use of #define in C program.

#define p printf
#define s scanf
int main()
{
      float rs, paise;
      p("Enter Price:");
      s("%f", &rs);
           
      paise = rs * 100;
      p("Price in Paise = %.2f", paise);        
      return 0;
}


Output of the Program

Enter Price:5.5
Price in Paise = 550.00



Sunday, August 14, 2016

C program to perform mathematical operations on characters.



C program to perform mathematical operations on characters.

#include<stdio.h>
int main()
{
            char ch1, ch2;
            ch1 = 'A';
            ch2 = 'B';
           
            printf("Addition of two characters: %d", ch1 + ch2);
            //Ascii of A is 65, Ascii of B is 66, hence addition is 131.
            //"%d" in printf will print 131.
            //"%c" will print corresponding character of 131.
            return 0;
}


Output of program

Addition of two characters: 131

Use of various Data Types in C program.


Use of various Data Types in C program.


#include<stdio.h>
int main()
{
            int rollno;
            char name[20];
            float age;
            char grade;
                       
            printf("Enter your roll no.:");
            scanf("%d", &rollno);
                       
            printf("Enter your name:");
            scanf("%s", name);
           
            printf("Enter your age:");
            scanf("%f", &age);
           
            printf("Enter your grade:");
            scanf(" %c", &grade);
            //Note: if program not read value properly,
            //leave one space before " %c"
           
            //Logic for Printing output....
            printf("========================\n");
            printf("Your Roll No is %d.\n", rollno);
            printf("Your Name is %s.\n", name);
            printf("Your Age is %.1f.\n", age);
            //Note: "%.1f" will print with one decimal point
            printf("Your Grade is %c.\n", grade);
            printf("========================\n");
            return 0;
}


Output of the Program:

Enter your roll no.:101
Enter your name:ABC
Enter your age:23.5
Enter your grade:A
========================
Your Roll No is 101.
Your Name is ABC.
Your Age is 23.5.
Your Grade is A.
========================


Friday, August 12, 2016

C program to ring a bell using ASCII value 7.

C program to generate a beep sound.

#include<stdio.h>
int main()
{
  // Following printf will ring a beep sound.
         
  printf("%c", 7);
           
  /* Note: 7 is ASCII value of a beep sound.
  Use a loop to ring a series of beep sound. */

  return 0;
}

Output:

You will listen a beep sound.

C program to ring a bell.

This program will play a beep sound.

 
#include<stdio.h>
int main()
{
  //following printf will ring a beep sound 3 times.

  printf("\a \a \a");
           
  //Note: Here \a stands for Alert
  //Each \a will ring a one beep sound

  return 0;         
}

Output of Program:

You will listen a beep sound. 



You may also like to learn following programs:





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.