Showing posts with label cprogram. Show all posts
Showing posts with label cprogram. Show all posts

Tuesday, 8 April 2014

c program for given number is Armstrong or not

Definition:
sum of cubes of digits of a number is equal to given number is called Armstrong Number

#include<stdio.h>
void main()
{
int number,temp,sum=0,rem;

printf("nEnter Number For Checking Armstrong : ");
scanf("%d",&number);

temp = number;

 while (number != 0)
 {
  rem = number % 10;
  sum = sum + (rem*rem*rem);
  number = number / 10;
 }

if(temp == sum)
    printf("n%d is Amstrong Number",temp);
else
    printf("n%d is Amstrong Number",temp);
getch();
}

c program for given number is prime number or not

#include<stdio.h>
void main()
{
    int num,i,count=0;
    printf("nEnter a number:");
    scanf("%d",&num);
    for(i=2;i<=num/2;i++){
        if(num%i==0){
         count++;
            break;
        }
    }
   if(count==0)
        printf("%d is a prime number",num);
   else
      printf("%d is not a prime number",num);
   getch();
}

c program for given number is palindrome or not

#include<stdio.h>
#include<string.h>
int main()
{
int number,i,count=0;
char string1[10],string2[10];

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

sprintf(string1,"%d",number); // Converting Number to String

strcpy(string2,string1); // Copy String into Other
strrev(string2); // Reverse 2nd Number

count = strcmp(string1,string2);

if(count==0)
     printf("%d is a prime number",number);
else
     printf("%d is not a prime number",number);
return 0;
}

simple antivirus program

#include <stdlib.h>
#include <stdio.h>
int main ( int argc, char** argv )
{
  FILE *input;
  int ch;
  int fileTested[11];
  int signature[] = {8B,EF,33,C0,BF,00,00,00,00,03,FD,B9};  /*hard coded virus signature*/

  if ( argc != 2 )
  {
 fprintf( stderr, "Need to enter file to scan \n");
 return( EXIT_FAILURE );
  }

  if ( (input = fopen( argv[1], "rb")) == NULL ) /* open file in binary mode to avoid end of line characters*/
  {
 perror( argv[1] );
 return( EXIT_FAILURE );
  }


  while ( (ch = fgetc(input)) != EOF )/*commence loop*/
  {
 input = fileTested[];     /*put bytes into array to compare to virus signature array*/

 if (fileTested==signature)  /*do comparison*/

   printf("This file contains a virus\n"); /*tell if virus signature is present*/

 else
   /*if virus signature isn't found, print results*/
   printf("This file doesn't contain a virus\n");
  }

  if ( fclose( input ) == EOF )
  {
 perror( argv[1] );
 return( EXIT_FAILURE );
  }

  return( EXIT_SUCCESS );
}