Friday, February 19, 2016

Simple Hash function implementation on 8 bits using XOR operation.

#include<stdio.h>
int main()
{
char ch, input[9], s[9];
int i, len=0;

printf("Enter your input bits:");
scanf("%s",input);

printf("Enter your key S (8 bits):");
scanf("%s",s);

printf("Your hash code is:");
for(i=0; i<8; i++)
{
ch=input[i]^s[i];
printf("%d",ch);
}
 return 0;
}

Output or program

Enter your input bits:11001100
Enter your key S (8 bits):01010101
Your hash code is:10011001

Program for password decryption using Caesar cipher. (Receiver side)

Caesar Cipher Decryption


//Program for password decryption using Caesar cipher. (Receiver side)
#include<stdio.h>
#include<string.h>
int main()
{
 char password[20];
 int i, len=0;
 
 printf("Enter your encrypted password:");
 scanf("%s",password);
 
 len = strlen(password);
 printf("Your decrypted password is: ");
 
 for(i=0; i<len; i++)
  printf("%c",password[i]-3); 
 
 return 0;
}

Output of the program
Enter your encrypted password:DEFG Your decrypted password is: ABCD
 




Program to encrypt password using Caesar cipher.


Caesar cipher - Password Encryption Practical


#include<stdio.h>
#include<string.h>
int main()
{
char password[20];
int i, len=0;

printf("Enter your password:");
scanf("%s",password);

len = strlen(password);
printf("Your encrypted password is: ");

for(i=0; i<len; i++)
printf("%c",password[i]+3);

return 0;
}

Output of program

Enter your password:ABCD
Your encrypted password is: DEFG

Tuesday, February 16, 2016

C program to validate user input.

//Check marks is in the range of 0 to 100.

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

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

if(marks > 100 || marks < 0 )
printf("Invalid marks.\nEnter marks in a range of 0-100.");
else
printf("Your marks are %d", marks);
}
/*Output1
Enter marks:80
Your marks are 80
*/

/*Output2
Enter marks:120
Invalid marks.
Enter marks in a range of 0-100.
*/


Use of GOTO statement in C program.

#include<stdio.h>
int main()
{
    int num1=10, num2=20, num3=30;
    printf("%d \n", num1);
    goto ABC;
    printf("%d \n", num2);
    ABC:
    printf("%d \n", num3);
return 0;
}
Output of program:
10
30




You may also like to learn following programs:

Monday, February 15, 2016

C program to write a word in text file.

#include <stdio.h>
#include <string.h>
int main()
{
FILE *fp;
char str[80];
fp = fopen("test.txt", "w+");
printf("Enter your message:");
gets(str);
fprintf(fp, "%s",str);
printf("Your message is written in test.txt file.");
fclose(fp);
//File validation is to be performed...
   return 0;
}


Output of program

Enter your message:hello student
Your message is written in test.txt file.


Thursday, February 11, 2016

C Program to find largest number from given three numbers.

#include<stdio.h>
int main()
{
int num1, num2, num3;

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

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

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

  if(num1 > num2 && num1 > num3)
  {
    printf("Number1 is largest number.");
  }
  if(num2 > num1 && num2 > num3)
  {
    printf("Number2 is largest number.");
  }
  if(num3 > num1 && num3 > num2)
  {
    printf("Number3 is largest number.");
  }
    return 0;
}
/*
Enter number1:5
Enter number2:4
Enter number3:3
Number1 is largest number.
*/
//Note: Add logic to update this program when two/three numbers are same.

C Program to find bigger number.

#include<stdio.h>
int main()
{
int num1, num2;

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

  printf("Enter number2:");
  scanf("%d",&num2);
  if(num1 > num2)
  {
    printf("Number1 is big.");
  }
  if(num2 > num1)
  {
    printf("Number2 is big.");
  }
  if(num1 == num2)
  {
    printf("Number1 and Number2 are equal.");
  }
    return 0;
}
/*
Enter number1:5
Enter number2:7
Number2 is big.
*/

Wednesday, February 10, 2016

Example of nested if statement. Check: A is greater than B and C.

#include <stdio.h>
void main ()
{
int a, b, c;
  printf("Enter number A:");
  scanf("%d",&a);

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

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

  if( a > b )
  {
    if( a > c )
      printf("A is greater than B and C.");
  }
}
/* Output
Enter number A:5
Enter number B:4
Enter number C:2
A is greater than B and C.
*/

C Program to convert given number (between 1 to 5) into words using switch.

#include<stdio.h>
int main ()
{
int number;
printf("Enter your number:");
scanf("%d",&number);
switch(number)
{
case 1 :printf("One");
          break;
case 2 :printf("Two");
          break;
case 3 :printf("Three");
          break;
case 4 :printf("Four");
          break;
case 5 :printf("Five");
          break;
default :printf("Invalid number\n" );
}
 return 0;
}

Output of the program:

Enter your number:5
Five

* * * * *

Check: Given number is positive or not using if..else statement.

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

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

if(number > 0)
printf("Number is positive.");
else
printf("Number is not positive.");
}
/*Output
Enter number:11
Number is positive.
*/

Check: Given number is positive or not using if statement.

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

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

if(number > 0)
printf("Number is positive.");

if(number <= 0)
printf("Number is not positive.");
}
/* Output
Enter number:5
Number is positive.
*/

Use of MODULUS (%) operator to find odd or even number.


#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 of the program:


Enter number:51
Entered number is odd.




You may also like to learn following programs:

Tuesday, February 9, 2016

C program to print 10 to 1 numbers using for loop.

#include<stdio.h>
int main()
{
int i;
for(i=10; i>0; i--)
{
printf("%d ", i);
}
  return 0;
}

Output of the program:
10 9 8 7 6 5 4 3 2 1


C program to print series 1 3 5 7... 19 using do..while loop.

#include<stdio.h>
int main()
{
int number=1;
do
{
printf("%d ", number);
number = number + 2;
while(number < 20);
  
  return 0;
}

Output of program

1 3 5 7 9 11 13 15 17 19


C program to print 10 to 20 numbers using while loop.

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

while(number <= 20)
{
printf("%d ", number);
number = number + 1;
}
 return 0;
}

Output of program:

10 11 12 13 14 15 16 17 18 19 20

C program to print Z to A using do..while loop.

This C program will print Z to A using do..while loop.


#include<stdio.h> 
int main()
{
  char ch='Z';
  do
  {
printf("%c ", ch);
ch = ch - 1;
  } while(ch >= 'A');
  return 0;
}


Output of program

Z Y X W V U T S R Q P O N M L K J I H G F E D C B A



C program to print A to Z using while loop.

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

  while(ch <= 'Z')
  {
printf("%c ", ch);
ch = ch + 1;
  }
  return 0;
}/*
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 1 to 10 numbers using for loop.


#include<stdio.h>
int main()
{
  int i;
  for(i=1; i<=10; i++)
  {
printf("%d ", i);
  }
  return 0;
}

Output of the program

1 2 3 4 5 6 7 8 9 10


Changing(swapping) value of two variables without use of third variable.

#include<stdio.h>
int main()
{
int n1,n2;
printf("Enter n1:");
scanf("%d",&n1);
printf("Enter n2:");
scanf("%d",&n2);

n1=n1+n2;
n2=n1-n2;
n1=n1-n2;
printf("n1=%d \n",n1);
printf("n2=%d",n2);
return 0;
}
/*Output
Enter n1:10
Enter n2:20
n1=20
n2=10
*/




You may also like to learn following programs:

Simple Calculator using C

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

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

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

  printf("Addition of two numbers: %d\n", number1 + number2 );
  printf("Substraction of two numbers: %d\n", number1 - number2 );
  printf("Multiplication of two numbers: %d\n", number1 * number2 );
  printf("Division of two numbers: %d\n", number1 / number2 );
return 0;
}

Output of program

Enter number1:10
Enter number2:5
Addition of two numbers: 15
Substraction of two numbers: 5
Multiplication of two numbers: 50
Division of two numbers: 2

printf function with formatting.

#include<stdio.h>
int main()
{
float pen, pencil;

printf("Enter your Pen Price:");
scanf("%f", &pen);
printf("Enter your Pencil Price:");
scanf("%f", &pencil);

printf("======= List of Items=========\n");
printf("Item Name\tPrice\n");
printf("Pen\t\tRs. %10.2f\n" , pen);
printf("Pencil\t\tRs. %10.2f\n" , pencil);
printf("======= List of Items=========");
 return 0;
}

Output of program

Enter your Pen Price:10.50
Enter your Pencil Price:5.50
======= List of Items=========
Item Name       Price
Pen             Rs.      10.50
Pencil          Rs.       5.50
======= List of Items=========


Finding square(x*x) and cube(x*x*x) of a given number(x).

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

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

printf("Square of given number is %d \n", number*number);

printf("Cube of given number is %d", number*number*number);
   return 0;
}

Output of the program:

Enter your number:5
Square of given number is 25
Cube of given number is 125

C program to print ASCII value of a given character.

#include<stdio.h>
int main()
{
char ch1;
printf("Enter any character:");
scanf("%c",&ch1);

printf("ASCII value of %c is %d ",ch1,ch1);
return 0;
}

Output of program

Enter any character:A
ASCII value of A is 65