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

Thursday, June 16, 2016

C Program to demonstrate use of bitwise AND operator.

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

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
*/

#include <stdio.h>
void main()
{
    int num1 = 30, num2 = 25;
    printf("Bitwise AND on num1 & num2 is: %d", num1&num2);
}

//Output
Bitwise AND operation on num1 and num2 is: 24

C Program to demonstrate use of relational operators.

// Relational operators are <  >  ==  !=  >=  <=
#include<stdio.h>
void main()
{
int a, b;

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

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

if(a > b)
{
printf("Value of A is greater than B.");
}
else
{
printf("Value of A is not greater than B.");
}
}
//Output
Enter number A:5
Enter number B:3
Value of A is greater than B.

C Program to demonstrate use of logical OR operator


#include<stdio.h>
int main()
{
int a, b;

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

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

if(a > 0 || b > 0)
{
printf("Evaluation of a>0 OR b>0 is True");
}
else
{
printf("Evaluation of a>0 OR b>0 is False");
}
return 0;
}

Output of program

Enter number a:5
Enter number b:-5
Evaluation of a>0 OR b>0 is True

C Program to demonstrate use of logical AND operator


#include<stdio.h>
int main()
{
int a, b;

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

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

if(a > 0 && b > 0)
{
printf("Both are positive numbers.");
}
else
{
printf("Both are not positive numbers.");
}
return 0;
}

Output of program

Enter number a:5
Enter number b:-7
Both are not positive numbers.

Wednesday, June 15, 2016

C program to find factorial of a given number.

/* C program to find factorial of a given number. Factorial of number n is defined as follow:
      n! = 1 * 2 * 3 * 4 * ....* n 
*/
#include<stdio.h>
int main()
{
   int i, n;
   double fact=1;
    
printf("Enter number (n): ");
   scanf("%d",&n);

   if (n < 0)
      printf("Factorial is not possible for negative number.");
   else
   {
      for(i=1; i<=n; i++)
      {
          fact = fact * i;     
      }
      printf("Factorial of number %d = %f", n, fact);
   }
}
* * * * *


Output of program:

Enter number (n): 5
Factorial of number 5 = 120.000000

C program to swap values of two variables using third variable.

#include<stdio.h>
int main()
{
int a,b,c;

printf("Enter number1:");
scanf("%d",&a);

printf("Enter number2:");
scanf("%d",&b);

c = a;  //assigning value to temporary variable
a = b;
b = c;

printf("Value in number1 is %d\n",a);
printf("Value in number2 is %d",b);
return 0;
}

Output of program

Enter number1:5
Enter number2:10
Value in number1 is 10
Value in number2 is 5

Tuesday, June 14, 2016

C program to read gender character and print it on screen.


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

printf("Enter your gender:");
scanf("%c", &ch);

printf("Your gender is:%c",ch);
return 0;
}

Output of program

Enter your gender:M
Your gender is:M

C program to read two floating point numbers and display answer on screen.


#include<stdio.h>
int main()
{
float f1,f2, total;

printf("Enter floating point number1:");
scanf("%f",&f1);

printf("Enter floating point number2:");
scanf("%f",&f2);

total = f1 + f2;

printf("Addition is %f ",total);
 return 0;
}

Output of program

Enter floating point number1:2.25
Enter floating point number2:3.25
Addition is 5.500000

Read two numbers from user and display addition on screen.


#include<stdio.h>
int main()
{
int number1, number2;

printf("Enter your number1:");
scanf("%d",&number1);

printf("Enter your number2:");
scanf("%d",&number2);

printf("Addition is %d", number1 + number2);
 return 0;
}

Output of program

Enter your number1:20
Enter your number2:30
Addition is 50

Monday, June 13, 2016

Use of conditional operator in C.


//Use of conditional operator in C.

#include<stdio.h>
int main()
{
int n1=5, n2=10;

(n1>n2) ? printf("n1 is big.") : printf("n2 is big.");

 return 0; }

Output of program

n2 is big.

C program to print ASCII values of A to Z characters.


//C program to print ASCII values of A to Z characters.

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

printf("ASCII Value:Its character\n");
printf("=========================\n");
for(i=65; i<91; i++) 
{
printf(" %d = %c \n", i, i);
}
 return 0;
}

Output of program

ASCII Value:Its character
=========================
 65 = A
 66 = B
 67 = C
 68 = D
 69 = E
 70 = F
 71 = G
 72 = H
 73 = I
 74 = J
 75 = K
 76 = L
 77 = M
 78 = N
 79 = O
 80 = P
 81 = Q
 82 = R
 83 = S
 84 = T
 85 = U
 86 = V
 87 = W
 88 = X
 89 = Y
 90 = Z

C program to check whether entered number is odd or even.

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

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

if(number%2 == 0)
printf("Entered number is even.");
else
printf("Entered number is odd.");
}

Output

Enter number:10
Entered number is even.

C program to find the sum of first 100 natural numbers.

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

sum=0; 
temp=1;

for( i=0; i<100; i++ )
{
sum = sum + temp;
temp = temp + 1;
}

printf("%d",sum);
 return 0;
}

Output of the program:

5050

C Program to check given string is Palindrome or not.


// C Program to check given string is Palindrome or not.

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

void main()
{
   char a[10], b[10];

   printf("Enter your string:\n");
   gets(a);

   strcpy(b,a);
   strrev(b);

   if (strcmp(a,b) == 0)
      printf("Entered string is a palindrome.\n");
   else
      printf("Entered string is not a palindrome.\n");
}

Output of program

Enter your string:
ABA
Entered string is a palindrome.

C Program to count characters in given string

//Program to count characters in given string
//This program will count 'A' in given string AHMEDABAD

#include<stdio.h>
#include<string.h>
int main()
{
  char str[20]="AHMEDABAD";
  int i,cnt=0,len=0;
  
  len=strlen(str);
  
  for(i=0; i < len; i++)   {
      if( str[i] == 'A')
          cnt++;      
  } 
  printf("Total A in given string : %d",cnt);
  return 0;
}



Output:

Total A in given string : 3



* * * * *



C program to read and print your first name.

//C program to read and print your first name.


#include<stdio.h>
#include<string.h>
void main()
{
char str[10];
printf("Enter your name:");
scanf("%s",str);
printf("Your name is %s \n",str);
}
// This program will run and generate output as:
Enter your name:ABC
ABC

String Initialization

C Program To demonstrate String Initialization

#include <stdio.h> 
#include<string.h>
int main () 

char name[10];
name[0]='H';
name[1]='E';
name[2]='L';
name[3]='L';
name[4]='O';
name[5]='\0';
printf("Greeting message: %s", name); 
return 0;
}

Output of program

HELLO

Saturday, June 11, 2016

strcat function in C program.


//String Processing  - Concatenation using strcat function.

#include<stdio.h>
#include<string.h>
int main()
{
    char str1[10]="12345", str2[10]="abcd";

    strcat(str1,str2);

    puts(str1);
return 0;
}

//Output

12345abcd

Use of strstr function in C program.


//String Processing using strstr function.

#include<stdio.h>
#include<string.h>
int main()
{
    char str1[10]="12345", str2[10]="23";
int n;

n=strstr(str1,str2);

puts(n);
    return 0;
}

//Output

2345

Friday, June 10, 2016

Write an algorithm for Subtracting two Numbers.

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.