Thursday, June 30, 2016

Example of Algorithm to C program conversion.

/* Example of Algorithm to C program conversion.

Problem Definition: Write an algorithm for Subtracting two Numbers.
Step 1: Start.
Step 2: Read two numbers A and B.
Step 3: Answer = A - B.
Step 4: Display Answer.
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;
}
* * * * *

C Program to Print Pattern like character C.

For N=5, output should be

 * * * *
*
*
*
*
 * * * *



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

 for(i=0; i<n-1; i++)
  printf(" * ");
  
 for(i=0; i<n-1; i++)
  printf("\n*");

 printf("\n");

 for(i=0; i<n-1; i++)
  printf(" * ");
}
//Output:
 * * * *
*
*
*
*
 * * * *


Monday, June 20, 2016

C program to demonstrate use of string length - strlen() function.

The strlen() function can be used to count total number of characters in a given string.




//This program will count total characters in given string.


#include<stdio.h>
#include<string.h>
int main()
{
  char str[20];
  int len=0;
  
  printf("Enter your string: ");
  gets(str);
  
  len=strlen(str);
  
  printf("Total characters in %s is :%d",str,len);
  return 0;
}
//Output of program
Enter your string: ahmedabad
Total characters in ahmedabad is :9


Saturday, June 18, 2016

C program to delete a given file.

NOTE: Be careful while executing following program. This program will delete file which you enter at run time.

C program to delete a given file.


#include<stdio.h>
int main()
{
   int file_check_flag;
   char file_name[30];
   printf("Enter file name to be deleted:\n");

   gets(file_name);

   file_check_flag = remove(file_name);

   if( file_check_flag == 0 )
      printf("Your file %s is deleted successfully.\n",file_name);
   else
      printf("Unable to delete %s file.\n",file_name);
   return 0;
}

Output of program

Enter file name to be deleted:

mydata.txt

Your file mydata.txt is deleted successfully.


C program to show use of Structure concept.

//C program to demonstrate use of Structure for storing a Book detains for Library Program.

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

struct Book{
   int   book_id;
   char  book_title[30];
   char  book_publisher[30];
   char  book_subject[30];
};

int main( ) {

   struct Book myBook;  // Declare variable of type Book

   //Initialise values to myBook variable
   myBook.book_id=101;
 
   printf("Enter Book Title:");
   gets(myBook.book_title);
 
   printf("Enter Book Publisher Name:");
   gets(myBook.book_publisher);
 
   printf("Enter Book Subject:");
   gets(myBook.book_subject);
 
   /* print myBook details */
   printf( "Book ID: %d\n", myBook.book_id);
   printf( "Book Title: %s\n", myBook.book_title);
   printf( "Book Publisher Name : %s\n", myBook.book_publisher);
   printf( "Book Subject : %s\n", myBook.book_subject);
   return 0;
}

Output of program

Enter Book Title:Computer Studies
Enter Book Publisher Name:ABC India
Enter Book Subject:Computer Science
Book ID: 101
Book Title: Computer Studies
Book Publisher Name : ABC India
Book Subject : Computer Science

Friday, June 17, 2016

Examples of Bitwise operations

Examples of Bitwise operations

C programming language allows us to perform bit level operation using bitwise operators (AND, OR, etc.).

Example of bitwise AND operation

Bitwise AND operation on two numbers 30 and 25 is given below:

  30 in binary is 00011110
  25 in binary is 00011001

Bitwise AND operation on 30 and 25 is as under:

   00011110
 & 00011001
  =========
   00011000 i.e. 24

Answer: 30 AND 25 = 24

Example of bitwise OR operation

Bitwise OR operation on two numbers 30 and 25 is given below:

  30 in binary is 00011110
  25 in binary is 00011001

Bitwise OR operation on 30 and 25 is as under:

   00011110
OR 00011001
  =========
   00011111 i.e. 31

Answer: 30 OR 25 = 31

C Program to demonstrate use of bitwise OR operator.


/*
Example of bitwise OR operation on two numbers 30 and 25.

30 in binary is 00011110
25 in binary is 00011001

Bitwise OR operation on 30 and 25 is as under:

   00011110 
OR 00011001
=========
   00011111 i.e. 31
*/

#include <stdio.h>
int main()
{
    int num1 = 30, num2 = 25;
    printf("Bitwise OR operation: num1 OR num2 is %d", num1|num2);
    return 0;
}

Output of program

Bitwise OR operation: num1 OR num2 is 31