9. Assignment 9
Assignment 1
/----------------------------------------------------------------------------------------/
(1)
#include<stdio.h>
void main()
{
short int num,i,j,pos,n1,n2;
printf("To swap first and last nibble in given short int \n");
printf("Enter a number : ");
scanf("%d",&num);
for(pos=15;pos>=0;pos--)
printf("%d",num>>pos&1);
printf("\n");
for(i=0,j=12;i<4,j<16;i++,j++)
{
n1=num>>i&1;
n2=num>>j&1;
if(n1!=n2)
{
num=num^(1<<j);
num=num^(1<<i);
}
}
for(pos=15;pos>=0;pos--)
printf("%d",num>>pos&1);
printf("\n");
}
/----------------------------------------------------------------------------------------/
(2)
#include<stdio.h>
void main()
{
char ch;
int i,j,pos,n1,n2;
printf("To reverse the bits of given character \n");
printf("Enter a character : ");
scanf("%c",&ch);
for(pos=7;pos>=0;pos--)
printf("%d",ch>>pos&1);
printf("\n");
for(i=0,j=7;i<j;i++,j--)
{
n1=ch>>i&1;
n2=ch>>j&1;
if(n1!=n2)
{
ch=ch^(1<<i);
ch=ch^(1<<j);
}
}
for(pos=7;pos>=0;pos--)
printf("%d",ch>>pos&1);
printf("\n");
}
/----------------------------------------------------------------------------------------/
(3)
#include<stdio.h>
void main()
{
while(1)
{
int num,i,c=0;
printf("To check if a number is divisible by 8 \n");
printf("Enter a number :");
scanf("%d",&num);
num&7 ? printf("Not divisible by 8\n") : printf("Divisible by 8\n");
}
}
/----------------------------------------------------------------------------------------/
(4)
#include<stdio.h>
void main()
{
unsigned short int num;
int rot,pos,i,n1;
printf("To rotate a bit\n ");
printf("Enter a number : ");
scanf("%d",&num);
printf("Enter number of rotations :");
scanf("%d",&rot);
for(pos=15;pos>=0;pos--)
printf("%d",num>>pos&1);
printf("\n");
for(i=0;i<rot;i++)
{
n1=num&1;
num=num>>1;
num=num|(n1<<15);
}
for(pos=15;pos>=0;pos--)
printf("%d",num>>pos&1);
printf("\n");
}
/----------------------------------------------------------------------------------------/
(5)
#include<stdio.h>
void main()
{
unsigned short int num,n1;
int pos,bits,poss,j;
printf("To delete number of bits from given location\n");
printf("Enter a number : ");
scanf("%d",&num);
printf("Enter the position :");
scanf("%d",&pos);
printf("Enter the number of bits to be deleted :");
scanf("%d",&bits);
for(poss=15;poss>=0;poss--)
printf("%d",num>>poss&1);
printf("\n");
for(j=0;j<bits;j++)
{
n1=num<<(16-pos);
n1=n1>>(16-pos);
num=num>>(pos+1);
num=num<<pos;
num=n1|num;
}
for(poss=15;poss>=0;poss--)
printf("%d",num>>poss&1);
printf("\n");
}
/----------------------------------------------------------------------------------------/
(6)
#include<stdio.h>
void main()
{
int i,j,pos,n1,n2,num;
printf("To reverse 1st (6 bits) with last (6 bits) \n");
printf("Enter a number : ");
scanf("%d",&num);
for(pos=31;pos>=0;pos--)
printf("%d",num>>pos&1);
printf("\n");
for(i=0,j=31;i<6,j>25;i++,j--)
{
n1=num>>i&1;
n2=num>>j&1;
if(n1!=n2)
{
num=num^(1<<i);
num=num^(1<<j);
}
}
for(pos=31;pos>=0;pos--)
printf("%d",num>>pos&1);
printf("\n");
}
/----------------------------------------------------------------------------------------/
(7)
#include<stdio.h>
void main()
{
unsigned short int num;
int pos,i;
printf("Enter the number : ");
scanf("%x",&num);
for(pos=15;pos>=0;pos--)
printf("%d",num>>pos&1);
printf("\n");
num=num|(0x000F);
num=num&(0xFF0F);
for(i=8;i<12;i++)
num=num^(1<<i);
for(pos=15;pos>=0;pos--)
printf("%d",num>>pos&1);
printf("\n");
}
/----------------------------------------------------------------------------------------/
(8)
#include<stdio.h>
void main()
{
int r,num,add=0;
printf("Enter the number :");
scanf("%d",&num);
for(;num;num=num/10)
{
r=num%10;
if((r&1)==0)
{
add=add+r;
}
}
printf("%d\n",add);
}
Assignment 2
/----------------------------------------------------------------------------------------/
(1)
#include<stdio.h>
int num,j,add=0;
void main()
{
printf("To find perfect numbers between 1 to 51\n");
for(num=1;num<=51;num++)
{
for(j=1,add=0;j<num;j++)
{
if(num%j==0)
{
add=add+j;
}
}
if(num==add)
{
printf("%d\n",num);
}
}
}
/----------------------------------------------------------------------------------------/
(2)
#include<stdio.h>
void main()
{
printf("Program to find fibonacii series between 0 to 31\n");
int a=0,b=1,c=0;
printf(" %d %d",a,b);
while(1)
{
c=a+b;
if(c>31)
break;
printf(" %d",c);
a=b;
b=c;
}
printf("\n");
}
/----------------------------------------------------------------------------------------/
(3)
#include<stdio.h>
int num,j,rev,r;
void main()
{
for(num=51;num<=151;num++)
{
for(rev=0,j=num;j;j=j/10)
{
r=j%10;
rev=rev*10+r;
}
if(num==rev)
{
printf("%d\n",num);
}
}
}
/----------------------------------------------------------------------------------------/
(4)
#include<stdio.h>
char ch;
int num1,num2,num3=0;
void main()
{
printf("Basic Calculator \n");
printf("Enter the number 1 : ");
scanf("%d",&num1);
printf("Enter the number 2 : ");
scanf("%d",&num2);
printf(" + Addition\n - Subtraction\n * Multiplication\n / Division\nEnter the operation : ");
scanf(" %c",&ch);
switch(ch)
{
case '+' :
num3=num1+num2;
printf("%d\n",num3);
break;
case '-' :
num3=num1-num2;
printf("%d\n",num3);
break;
case '*' :
num3=num1*num2;
printf("%d\n",num3);
break;
case '/' :
num3=num1/num2;
printf("%d\n",num3);
break;
default :
printf("Invalid Input\n");
}
}
/----------------------------------------------------------------------------------------/
(5)
#include<stdio.h>
int totalfact,fact,num,temp,r,i;
void main()
{
for(num=1;num<=251;num++)
{
for(totalfact=0,temp=num;temp;temp=temp/10)
{
r=temp%10;
for(i=r,fact=1;i>0;i--)
{
fact=fact*i;
}
totalfact=totalfact+fact;
}
if(totalfact==num)
{
printf("%d is a strong number\n",num);
}
}
}
/----------------------------------------------------------------------------------------/
(6)
#include<stdio.h>
int num,c,i;
void main()
{
for(num=51,c=0;num<=111;num++)
{
for(i=2;i<num;i++)
{
if(num%i==0)
{
break;
}
}
if(i==num)
{
printf("%d\n",num);
c++;
}
}
printf("Total Numbers of Prime Numbers : %d\n",c);
}
/----------------------------------------------------------------------------------------/
(7)
#include<stdio.h>
int num,fact,i;
void main()
{
for(num=2;num<=11;num++)
{
for(fact=1,i=num;i>0;i--)
{
fact=fact*i;
}
printf("Factorial of %d is %d \n",num,fact);
}
}
/----------------------------------------------------------------------------------------/
(8)
#include<stdio.h>
int num,temp,r,n1,n2,c,i;
void main()
{
for(num=1;num<=501;num++)
{
for(c=0,temp=num;temp;temp=temp/10)
{
c++;
}
for(temp=num,n2=0;temp;temp=temp/10)
{
r=temp%10;
for(n1=1,i=1;i<=c;i++)
{
n1=n1*r;
}
n2=n2+n1;
}
if(n2==num)
{
printf("%d is a armstrong number\n",num);
}
}
}
/----------------------------------------------------------------------------------------/
(9)
#include<stdio.h>
int c,add,num,r;
void main()
{
printf("Enter a number : ");
scanf("%d",&num);
for(c=0,add=0,r=0;num;num=num/10)
{
r=num%10;
c++;
add=add+r;
if(c==4)
break;
}
printf("%d\n",add);
}
/----------------------------------------------------------------------------------------/
(10)
#include<stdio.h>
int i,j;
void main()
{
for(i=2;i<=9;i++)
{
for(j=1;j<=10;j++)
{
printf("%d * %d = %d\n",i,j,i*j);
}
printf("\n");
}
}
/----------------------------------------------------------------------------------------/
(11)
#include<stdio.h>
int num,c,i;
void main()
{
for(num=21,c=0;c<7;num++)
{
for(i=2;i<num;i++)
{
if(num%i==0)
{
break;
}
}
if(i==num)
{
printf("%d\n",num);
c++;
}
}
printf("Total Numbers of Prime Numbers : %d\n",c);
}
/----------------------------------------------------------------------------------------/
(12)
#include<stdio.h>
int num,j,rev,r,c=0;
void main()
{
for(num=100;c<=5;num--)
{
for(rev=0,j=num;j;j=j/10)
{
r=j%10;
rev=rev*10+r;
}
if(num==rev)
{
printf("%d\n",num);
c++;
}
}
}
/----------------------------------------------------------------------------------------/
(13)
#include<stdio.h>
int num=456787756,c,i,r;
void main()
{
for(c=0;num;num=num/10)
{
r=num%10;
for(i=2;i<r;i++)
{
if(r%i==0)
{
break;
}
}
if(i==r)
{
c++;
}
}
printf("Total Numbers of Prime Numbers : %d\n",c);
}
/----------------------------------------------------------------------------------------/
(14)
#include<stdio.h>
int rev,num=234547,r,onum;
void main()
{
for(rev=0;num;num=num/10)
{
r=num%10;
if(r==4)
continue;
rev=rev*10+r;
}
for(onum=0;rev;rev=rev/10)
{
r=rev%10;
onum=onum*10+r;
}
printf("%d\n",onum);
}
/----------------------------------------------------------------------------------------/
(15)
#include<stdio.h>
int num,input,i,j,add,temp,totalfact,r,fact,rev,n1,n2,c;
void main()
{
printf("Enter a number : ");
scanf("%d",&num);
printf(" 1.Prime Number\n 2.Perfect Number\n 3.Strong Number\n 4.Palindrome Number\n 5.Armstrong Number \n\nEnter the input : ");
scanf("%d",&input);
switch(input)
{
case 1:
for(i=2;i<num;i++)
if(num%i==0)
break;
if(i==num)
printf("Its a prime number\n");
else
printf("Not a prime number\n");
break;
case 2:
for(j=1,add=0;j<num;j++)
if(num%j==0)
add=add+j;
if(num==add)
printf("Its a perfect number\n ");
else
printf("Its not a perfect number\n");
break;
case 3:
for(totalfact=0,temp=num;temp;temp=temp/10)
{
r=temp%10;
for(i=r,fact=1;i>0;i--)
fact=fact*i;
totalfact=totalfact+fact;
}
if(totalfact==num)
printf("%d is a strong number\n",num);
else
printf("%d is not a strong number\n",num);
break;
case 4:
for(rev=0,j=num;j;j=j/10)
{
r=j%10;
rev=rev*10+r;
}
if(num==rev)
printf("Its a palindrome number\n");
else
printf("Its not a palindrome number\n");
break;
case 5:
for(c=0,temp=num;temp;temp=temp/10)
{
c++;
}
for(temp=num,n2=0;temp;temp=temp/10)
{
r=temp%10;
for(n1=1,i=1;i<=c;i++)
{
n1=n1*r;
}
n2=n2+n1;
}
if(n2==num)
printf("It is a armstrong number\n");
else
printf("It is not a armstrong number\n");
break;
default:
printf("Invalid Input\n");
}
}
Comments
Post a Comment