Tuesday, 23 January 2018

Program to implement cyclic redundancy check (CRC)

Source Code:

#include <stdio.h>
 #include <conio.h>
 #include <string.h>
 void main()
 {
 int i,j,keylen,msglen;
 char input[100], key[30],temp[30],quot[100],rem[30],key1[30];
 clrscr();
 printf("Enter Data: ");
 gets(input);
 printf("Enter Key: ");
 gets(key);
 keylen=strlen(key);
 msglen=strlen(input);
 strcpy(key1,key);
 for(i=0;i<keylen-1;i++)
 {
 input[msglen+i]='0';
 }
 for(i=0;i<keylen;i++)
 temp[i]=input[i];
 for(i=0;i<msglen;i++)
 {
 quot[i]=temp[0];
 if(quot[i]=='0')
 for(j=0;j<keylen;j++)
 key[j]='0';
 else
 for(j=0;j<keylen;j++)
 key[j]=key1[j];
 for(j=keylen-1;j>0;j--)
 {
 if(temp[j]==key[j])
 rem[j-1]='0';
 else
 rem[j-1]='1';
 }
 rem[keylen-1]=input[i+keylen];
 strcpy(temp,rem);
 }
 strcpy(rem,temp);
 printf("\nQuotient is ");
 for(i=0;i<msglen;i++)
 printf("%c",quot[i]);
 printf("\nRemainder is ");
 for(i=0;i<keylen-1;i++)
 printf("%c",rem[i]);
 printf("\nFinal data is: ");
 for(i=0;i<msglen;i++)
 printf("%c",input[i]);
 for(i=0;i<keylen-1;i++)
 printf("%c",rem[i]);
 getch();
 }

Output:

Monday, 22 January 2018

C program for hamming code generation for error detection and correction

Source Code

#include<stdio.h>
#include<conio.h>
void main() {
int data[7],rec[7],i,c1,c2,c3,c;
printf("this works for message of 4bits in size \nenter message bit one by one:  ");
scanf("%d%d%d%d",&data[0],&data[1],&data[2],&data[4]);
data[6]=data[0]^data[2]^data[4];
data[5]=data[0]^data[1]^data[4];
data[3]=data[0]^data[1]^data[2];
printf("\nthe encoded bits are given below: \n");
for (i=0;i<7;i++) {
printf("%d ",data[i]);
}
printf("\nenter the received data bits one by one: ");
for (i=0;i<7;i++) {
scanf("%d",&rec[i]);
}
c1=rec[6]^rec[4]^rec[2]^rec[0];
c2=rec[5]^rec[4]^rec[1]^rec[0];
c3=rec[3]^rec[2]^rec[1]^rec[0];
c=c3*4+c2*2+c1 ;
if(c==0) {
printf("\ncongratulations there is no error: ");
} else {
printf("\nerron on the postion: %d\nthe correct message is \n",c);
if(rec[7-c]==0)
rec[7-c]=1; else
rec[7-c]=0;
for (i=0;i<7;i++) {
printf("%d ",rec[i]);
}
}
getch();

}

Output :


Friday, 12 January 2018

Python Program to Find Those Numbers which are Divisible by 7 and Multiple of 5 in a Given Range of Numbers

Source Code :

lower= int (input("Enter the lower limit :"))
upper= int (input("Enter the upper limit :"))
for i in range(lower,upper+1):
    if(i%7==0 and i%5==0):
        print(i)

    
Output :

Enter the lower limit :100
Enter the upper limit :400
105
140
175
210
245
280
315
350
385

Python Program to Print Largest Even and Largest Odd Number in a List

Source Code :

n = int(input("Enter the no of elements in the list :"))
b=[]
for i in range(0,n):
    a=int(input("Enter elements :"))
    b.append(a)
c=[]
d=[]

for i in b:
    if (i%2==0):
        c.append(i)
    else :
        d.append(i)
c.sort()
d.sort()
count1=0
count2=0
for k in c:
    count1=count1+1
for j in d:
    count2=count2+1
print("Largest even no is : ",c[count1-1])

print("Largest odd no is :", d[count2-1])

Output :

Enter the no of elements in the list :7
Enter elements : 9
Enter elements : 17
Enter elements : 2
Enter elements : 89
Enter elements : 40
Enter elements : 102
Enter elements : 101
Largest even no is :  102
Largest odd no is : 101

Python Program to Print Sum of Negative Numbers, Positive Even Numbers and Positive Odd numbers in a List

Source Code :

n=int(input("Enter the no of elemnets in the list"))
b=[]
for i in range(0,n):
    a=int(input("Elements :"))
    b.append(a)
sum1=0
sum2=0
sum3=0

for j in b :
    if(j>0):
        if(j%2==0):
            sum1=sum1+j
        else :
            sum2=sum2 + j
    else :
        sum3=sum3+j
print("Sum of all positive even no :",sum1)
print("Sum of all positive odd no ",sum2)

print("Sum of all negative no", sum3)

Output :

Enter the no of elements in the list 7
Elements :1
Elements :4
Elements :-6
Elements : -1
Elements : 8
Elements : 1
Elements :9
Sum of all positive even no : 12
Sum of all positive odd no  11

Sum of all negative no -7


Python Program to Compute Simple Interest Given all the Required Values :

Program/Source Code :


n=int(input("Enter the number to print the tables for:"))
for i in range(1,11):
    print(n,"x",i,"=",n*i)

Output :

Enter the no :7
7 X 1 = 7
7 X 2 = 14
7 X 3 = 21
7 X 4 = 28
7 X 5 = 35
7 X 6 = 42
7 X 7 = 49
7 X 8 = 56
7 X 9 = 63
7 X 10 = 70



Python Program to Compute Simple Interest Given all the Required Values :

Program/Source Code :


principle=float(input("Enter the p amount:"))
time=int(input("Enter the time(years) :"))
rate=float(input("Enter the rate :"))
simple_interest=(principle*time*rate)/100
print("the simple interest is :",simple_interest)


Output:


case 1:

Enter the p amount:1200
Enter the time(years) :4
Enter the rate :2
the simle interest is : 96.0


case 2:

Enter the p amount:200
Enter the time(years) :5
Enter the rate :5

the simle interest is : 50.0