Thursday, March 31, 2016

C program to print Square with inner pattern.

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

#include<stdio.h>
int main()
{
 int i,j, n=5;
 for(i=0; i<n*2; i++)
  printf("* ");
 printf("\n");

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

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

 for(i=0; i<n*2; i++)
  printf("* ");

 printf("\n");
 return 0;
}

C Program to Print Pattern like character H.

For N=5,

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

int main()
{
int i,j,n=5;

for(j=0; j<n-2; j++)
{
printf("* ");
for(i=0; i<n-2; i++) printf("  ");
printf("* \n");
}

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

printf("\n");

for(j=0; j<n-2; j++)
{
printf("* ");
for(i=0; i<n-2; i++) printf("  ");
printf("* \n");
}
 return 0;
}

//Output:

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

C program to print triangle pattern.

C program to print triangle as shown below:
For N=5, print output as

* * * * *

 * * * *
  * * *
   * *
    *

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

for(j=0; j<n; j++)
{
for(i=n; i>n-j; i--)
     printf("  ");
for(i=0; i<n-j; i++)
     printf("*   ");
printf("\n");
}
 return 0;
}

C program to implement receiver side confidentiality.

//Assumption: test1.txt is encrypted using Ceasar cipher (Key=3).

#include<stdio.h>
int main() {
 
   FILE *fp1, *fp2;
   char a;

   fp1 = fopen("test1.txt", "r");
 
   if (fp1 == NULL) {
      puts("cannot open test.txt file.");
      exit(1);
   }

   fp2 = fopen("test2.txt", "w");
 
   if (fp2 == NULL) {
      puts("Not able to test1.txt file.");
      fclose(fp1);
      exit(1);
   }

   a = fgetc(fp1);
   fprintf(fp2,"%c",a-3);

   while(a != EOF)
   {
    a = fgetc(fp1);
    fprintf(fp2,"%c",a-3);
   }
 
  printf("test1.txt is successfully decrypted and stored to test2.txt.");
  printf("\nUser can read test2.txt file.");
 
  fclose(fp1);
  fclose(fp2);
  return 0;
}
/*Output of program:
test1.txt is successfully decrypted and stored to test2.txt.
User can read test2.txt file.
*/

C program to implement sender side confidentiality.

//Create test.txt file with some text in the same folder where you are saving this program.

#include<stdio.h>

int main() {
 
   FILE *fp1, *fp2;
   char a;

   fp1 = fopen("test.txt", "r");
 
   if (fp1 == NULL) {
      puts("cannot open test.txt file.");
      exit(1);
   }

   fp2 = fopen("test1.txt", "w");
 
   if (fp2 == NULL) {
      puts("Not able to test1.txt file.");
      fclose(fp1);
      exit(1);
   }

   a = fgetc(fp1);
   fprintf(fp2,"%c",a+3);

   while(a != EOF)
   {
    a = fgetc(fp1);
    fprintf(fp2,"%c",a+3);
   }
 
  printf("test.txt is successfully encrypted and stored to test1.txt.");
  printf("\ntest1.txt can be forwarded to destination.");
 
  fclose(fp1);
  fclose(fp2);
  return 0;
}

/*Output of program
test.txt is successfully encrypted and stored to test1.txt.
test1.txt can be forwarded to destination.
*/

Wednesday, March 23, 2016

C program to copy content of one file to other.

#include<stdio.h>
int main() {
   FILE *fp1, *fp2;
   char a;

   fp1 = fopen("test.txt", "r");
   if (fp1 == NULL) {
      puts("cannot open test.txt file.");
      exit(1);
   }

   fp2 = fopen("test1.txt", "w");
   if (fp2 == NULL) {
      puts("Not able to test1.txt file.");
      fclose(fp1);
      exit(1);
   }

   do {
      a = fgetc(fp1);
      fputc(a, fp2);
   } while (a != EOF);
  printf("test.txt is successfully copied to test1.txt.");
  fclose(fp1);
  fclose(fp2);
  return 0;
}

Output of the program

test.txt is successfully copied to test1.txt.


Saturday, March 12, 2016

Program to display MENU using do..while

#include<stdio.h>
int main()
{
int choice, n1=5, n2=10;
printf("n1=5, n2=10 \n");
do
{
printf("=== Menu ===\n");
printf("1. Addition\n");
printf("2. Substraction\n");
printf("3. Exit\n");
printf("Enter your choice:");
scanf("%d",&choice);
switch (choice)
{
  case  1: printf("The addition = %d \n", n1 + n2);
         break;
  case  2: printf("The substraction = %d \n", n1 - n2);
         break;
  case  3: printf("Goodbye\n");
         break;
  default: printf("Wrong Choice. Enter again\n");
         break;
}
} while (choice != 3);
 return 0;
}

Output of program

n1=5, n2=10
=== Menu ===
1. Addition
2. Substraction
3. Exit
Enter your choice:1
The addition = 15
=== Menu ===
1. Addition
2. Substraction
3. Exit
Enter your choice:2
The substraction = -5
=== Menu ===
1. Addition
2. Substraction
3. Exit
Enter your choice:3
Goodbye




Program to check Palindrome number.

Example: 121 and 12321 are palindrome numbers.


#include <stdio.h>
#include<string.h>
int main ()
{
char number[10],reverse[10];

puts("Enter your number:");
gets(number);

strcpy(reverse,number);
strrev(number);

printf("Your original number is :%s\n",number);
printf("Reverse number is: %s\n", reverse);

if(strcmp (number,reverse) == 0 )
printf("Its Palindrome");
else
printf("Not a Palindrome number");
 return 0;
}
/* Output
Enter your number:
121
Your original number is :121
Reverse number is: 121
Its Palindrome
*/

Program to check whether entered number is odd or even.

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

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

if(number%2 == 0)
printf("Entered number is even.");
else
printf("Entered number is odd.");
 return 0;
}
/* Output
Enter number:5
Entered number is odd.
*/

Thursday, March 10, 2016

C program to print array elements in reverse order.

Printing Array Elements in Reverse Order

#include<stdio.h>
int main()
{
 int i, arr1[5];
 for(i=0; i<5; i++)
 {
  printf("Enter Element[%d]:", i);
  scanf("%d",&arr1[i]);
 }
 printf("\n");
 printf("Array elements in reverse order are:\n");
 for(i=4; i>=0; i--)
 {
  printf("Element[%d]: %d \n", i,arr1[i]);
 }
 return 0;
}

Output of program

Enter Element[0]:1
Enter Element[1]:2
Enter Element[2]:3
Enter Element[3]:4
Enter Element[4]:5

Array elements in reverse order are:
Element[4]: 5
Element[3]: 4
Element[2]: 3
Element[1]: 2
Element[0]: 1

Thursday, March 3, 2016

C Program to count Consonant in a given string.

#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' && str[i]!='E' && str[i]!='I' && str[i]!='O' && str[i]!='U')
          cnt++;    
  }
  printf("Total Consonants in given string: %d",cnt);
  return 0;
}
/*Output
Total Consonants in given string: 5
*/

C Program to count Vowels in a given string.

//C Program to count Vowels in a given string.

#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' || str[i]=='E' || str[i]=='I' || str[i]=='O' || str[i]=='U')
          cnt++;      
  } 
  printf("Total vowels in given string: %d",cnt);
  return 0;
}
/*Output
Total vowels in given string: 4
*/

Wednesday, March 2, 2016

C program to print memory occupied by int, float and char variables.


#include<stdio.h>

void main()
{
int i;
float f;
char ch;

printf("Size of int variable %d: \n",sizeof(i));
printf("Size of float variable %d: \n",sizeof(f));
printf("Size of char variable %d: \n",sizeof(ch));
}
/* Output may be different on your computer...
Size of int variable 4:
Size of float variable 4:
Size of char variable 1:
*/