WordPress.com



Table of Contents

Types, Operators and Expressions 1

Control Flow 2

Functions and Program structure 3

Pointers and Arrays 5

Structures 6

Input and Output 7

Types, Operators and Expressions

1. Write a program to convert a given number of days into months and days.

#include

int main()

{

int days,months;

printf("Enter the number of days:");

scanf("%d",&days);

months=(days/30);

days=days-months*30; // days=days%30;

printf("The entered number of days are equal to %d months%d days",months,days);

}

Output:

[pmanne@oradb ~]$ gcc dy_to_mn_conv.c

[pmanne@oradb ~]$ ./a.out

Enter the number of days:45

The entered number of days are equal to 1 months15 days

2. Write a program to read length and width from the input and compute perimeter and area of the rectangle.

#include

int main()

{

float length,width;

float area,perimeter;

printf("Enter the length and width of a reactangle:");

scanf("%f %f",&length,&width);

area=length*width; //calculates area

perimeter=2*(length+width);//calculates perimeter

printf("Area is %f and perimeter is%f",area,perimeter);

}

Output:

[pmanne@oradb ~]$ gcc area_peri.c

[pmanne@oradb ~]$ ./a.out

Enter the length and width of a reactangle:2.5 3

Area is 7.500000 and perimeter is11.000000[

3. Write a program to read the diameter of the circle and compute the perimeter and area of the circle.

#include

int main()

{

int diameter;

float perimeter,area;

printf("Enter the diameter of the circle");

scanf("%d",&diameter);

perimeter=diameter*3.14; //calculates perimeter

area=(3.14*diameter*diameter)/4;//calculates area

printf("Area of circle is %f and perimeter of circle is %f",area,perimeter);

}

Output:

[pmanne@oradb ~]$ gcc areaofcircle.c

[pmanne@oradb ~]$ ./a.out

Enter the diameter of the circle26

Area of circle is 530.659973 and perimeter of circle is 81.639999

4. Write a program to read a floating point number from the standard input and print right most digit of the integral part and left most digit of real part.

#include

#define EXIT_SUCCESS 0

int main()

{

float num,real_part;

int int_part,r_num,i_num;

printf("Enter floating type number:");

scanf("%f",&num);

int_part=num;

real_part=num-int_part;

printf("integral part is %d\n real part is %f",int_part,real_part);

i_num=int_part%10;//gives the leftmostdigit of realpart

real_part=real_part*10;//gives the rightmost digit of intger part

r_num=(int)real_part;

printf("\n leftmost digit of realpart is %d\n right most digit of integral part is %d",r_num,i_num);

return EXIT_SUCCESS;

}

Output:

[pmanne@oradb ~]$ gcc floatir.c

[pmanne@oradb ~]$ ./a.out

Enter floating type number:1234.56

integral part is 1234

real part is 0.560059

leftmost digit of realpart is 5

right most digit of integral part is 4

5. Write a program to read values of “x” and “y” from the input and evaluate the following expression and print the result

Expr: 7x5 + 3x3 + 12 x2 + 5x + 10

#include

#include

#define exit 0

int main()

{

int x,res;

printf("Enter the values of X:");

scanf("%d",&x);

res=(7*pow(x,5))+(3*pow(x,3))+(12*pow(x,2))+(5*x)+10;

printf("\nResult of the expresison is%d",res);

return exit;

}

Output:

[pmanne@oradb ~]$ gcc -lm eval_expr.c -o eval

[pmanne@oradb ~]$ ./eval

Enter the values of X:12

Result of the expresison is1748806

6. Write a program to determine the ranges of char, short, int, float and long variables both signed and unsigned.

Output:- Range of signed char is -128 to 127.

Range of unsigned char is from 0 to 255.

#include

#include//contains the functions to check the range of char,so on

#define exit 0

int main()

{

printf("Max value of type char is %d\n",CHAR_MAX);

printf("Min value of type char is %d\n",CHAR_MIN);

printf("Max value of type SIGNED char is %d\n",SCHAR_MAX);

printf("Min value of type SIGNED char is %d\n",SCHAR_MIN);

printf("Max value of type UNSIGNED char is %u\n",UCHAR_MAX);

printf("Max value of short is %d\n",SHRT_MAX);

printf("Min value of short is %d\n",SHRT_MIN);

printf("Max value of UNSIGNED short is%u\n",USHRT_MAX);

printf("Max&MIN value of type int is%d%d\n",INT_MAX,INT_MIN);

printf("Max value of type UNSIGNED int is %d\n",UINT_MAX);

printf("Max value of type long is %ld\n",LONG_MAX);

printf("Min value of type long is %ld\n",LONG_MIN);

printf("Max value of UNSIGNED long is%ld\n",ULONG_MAX);

}

Output:

[pmanne@oradb ~]$ gcc range.c -o range

[pmanne@oradb ~]$ ./range

Max value of type char is 127

Min value of type char is -128

Max value of type SIGNED char is 127

Min value of type SIGNED char is -128

Max value of type UNSIGNED char is 255

Max value of short is 32767

Min value of short is -32768

Max value of UNSIGNED short is65535

Max&MIN value of type int is2147483647-2147483648

Max value of type UNSIGNED int is -1

Max value of type long is 2147483647

Min value of type long is -2147483648

Max value of UNSIGNED long is-1

7. Write a loop equivalent to below for loop without using ’&&’ or ‘||’

#include

#define exit 0

int main()

{

/* int i; //given loop in the assignment

char c;

char s[100];

int limit=100;

for(i=0;iequal to newline comeout

break;

s[i++]=c; //if both the above conditions are satisfied,copy it into s

}

s[i]='\0'; //terminate the string

printf("%s",s);

return exit;

}

Output:

[pmanne@oradb ~]$ gcc equivfor.c -o for

[pmanne@oradb ~]$ ./for

this is a program to write the loop without using && or ||

this is a program to write the loop without using && or ||

8. Write a program to give the count of No of 1s in binary format of a number given.

Eg: count = NoOf1sCount(155) = 5 (as Binary form of 155 is 10011011)

#include

#define exit 0

int main()

{

long base=1,number,snum,count=0,rem,bin=0;

printf("Enter an decimal number");

scanf("%d",&number);

//----converting into binary----//

snum=number;

while(number>0)

{

rem=number%2;

if(rem==1)//if it finds a 1 then increment the count

{

count++;

}

bin=bin+rem*base;

number=number/2;

base=base*10;

}

printf("Input number is:%d \n",snum);

printf("Binary equivalent is: %d\n",bin);

printf("NO.of 1's are: %d \n",count);

return exit;

}

Output:

[pmanne@oradb ~]$ gcc count_binary.c -o count

[pmanne@oradb ~]$ ./count

Enter an decimal number155

Input number is:155

Binary equivalent is: 10011011

NO.of 1's are: 5

9. Write a program to get product of 2 pow n and a given number without using “*’ operation

Eg: res = myProduct(32, 2) = 32 * 4 = 128

myProduct(25, 4) = 25 * 16 = 400

#include

#define exit 0

int main()

{

int x,y,prod;

printf("Enter a number and the value of n in 2 pow n:");

scanf("%d%d",&x,&y);

prod=x=0;j--)

printf("%c",bin[j]);

//fliiping 1's and 0's to get 1s complement

for(j=i;j>=0;j--)

{

if(j==i)

{

if(bin[j]=='0')

bin[j]='1';

else if(bin[j]=='1')

bin[j]='0';

}

}

printf("\n 1's Complement is %s",bin);

//converting again into integer

l=strlen(bin);

l--;

for(i=l;bin[i]>=0;i--)

{

if(bin[i]=='1')

{

s=pow(2,k)+s;

k++;

}

else

k++;

}

printf("\nEquivalent integer is %d",s);

}

Output:

[pmanne@oradb ~]$ gcc -lm withoutild.c -o wtild

[pmanne@oradb ~]$ ./wtild

Enter an integer:170

Equivalent binary is :10101010

1's Complement is 01010101

Equivalent integer is 85

11. Write a program to get hexadecimal representation of given number using bit wise operations.

#include

void hexconv(int a);

main()

{

int a;

printf("Enter a no. in decimal system:- ");

scanf("%d",&a);

hexconv(a);

}

void hexconv(int a)//converts an integer into hex

{

int b,c=0,hex[5],i=0;

b=a;

while (b>15)

{

hex[i]=b%16;

b=b>>4;

i++;

c++;

}

hex[i]=b;

printf("Its hexadecimal equivalent is ");

for (i=c;i>=0;--i)

{

if (hex[i]==10)

printf("A");

else if (hex[i]==11)

printf("B");

else if (hex[i]==12)

printf("C");

else if (hex[i]==13)

printf("D");

else if (hex[i]==14)

printf("E");

else if (hex[i]==15)

printf("F");

else

printf("%d",hex[i]);

}

return;

}

Output:

[pmanne@oradb ~]$ vi Hex_conv.c

[pmanne@oradb ~]$ gcc Hex_conv.c -o hex

[pmanne@oradb ~]$ ./hex

Enter a no. in decimal system:- 123

Its hexadecimal equivalent is 7B

12. Write a function setbits(x, p,n,y) that returns x with the n bits that begin at position p set to the rightmost n bits of y, leaving the other bits unchanged.

Eg: res = setbits(0xB26A, 9,4,0xA)= 0xB2FA

Control Flow

13. Write a function htoi(s), which converts a string of hexa-decimal digits (including an optional 0x or 0X) into its equivalent integer value.

(The allowed digits are 0 through 9, a through f, and A through F)

Input: - oxAA

Output: - 170

#include

#include

#include

#define EXIT_SUCCESS 0

long htoi(char *);

void strrev(char *);

int main()

{

long number=0;

char inputstr[30];

printf("Enter a hexadecimal string ");

scanf("%s",inputstr);

printf("string is %s\n",inputstr);

number=htoi(inputstr);

printf("Integer is %ld\n",number);

return EXIT_SUCCESS;

}

long htoi(char * inputstr)//converts the hexrepresentation to integer

{

int length=0, base=1,i=0,k;

long hnumber=0;

char str[30];

strrev(inputstr);

length=strlen(inputstr);

//to include optional ox

if((inputstr[length-1]=='O' || inputstr[length-1]=='o')&&(inputstr[length-2]=='X' || inputstr[length-2]=='x'))

{

length=length-2;

}

while(inum==x)

{

temp1=temp->next;

head=temp1;

free(temp);

}

else

while(temp->next!=NULL)

{

if(temp->next->num==x)

{

temp1=temp->next;

temp->next=temp1->next;

free(temp1);

}

temp=temp->next;

}

}

}

void display()

{

struct node * temp=head;

if(head==NULL)

printf("List is empty");

else

{

do

{

printf("%d ",temp->num);

temp=temp->next;

}while(temp!=NULL);

}

}

50. Write C functions Push(), Pop() to insert and delete item from a stack using arrays.

Structures

51. Write C functions Push(), Pop() to insert and delete item from a stack using single linked lists.

#include

#include

void push();

void pop();

void display();

struct node

{

int data;

struct node *next;

}*top=NULL,*p,*newp,*temp;

typedef struct node N;

int main()

{

int ch,x;

do

{

printf("\n\n1.Push\n");

printf("\n2.Pop\n");

printf("\n3.Display\n");

printf("\n4.Exit\n");

printf("\nSelect any one of the above:");

scanf("%d",&ch);

switch(ch)

{

case 1: /*printf("\nEnter the element to be pushed into the stack:");

scanf("%d",&x);*/

push();

break;

case 2: pop();

break;

case 3: display();

break;

default:printf("\n EXIT");

}

}while(ch!=4);

}

void push()

{

newp=(N*)(malloc(sizeof(N)));

printf("Enter the element:");

scanf("%d",&newp->data);

newp->next=NULL;

if(top==NULL)

top=newp;

else

{

p=top;

while(p->next!=NULL)

p=p->next;

p->next=newp;

}

}

void pop()

{

if(top==NULL)

printf("\n stack empty");

else if(top->next==NULL)

{

printf("The deleted item is:%d",top->data);

free(top);

top=NULL;

}

else

{

p=top;

while(p->next!=NULL)

{

temp=p;

p=p->next;

}

printf("Deleted item is:%d",p->data);

temp->next=NULL;

free(p);

}

}

void display()

{

if(top==NULL)

printf("\nStack is empty");

else

{

printf("\nThe elements are :\n ");

p=top;

while(p!=NULL)

{

printf("%d",p->data);

p=p->next;

printf("\n");

}

printf("\n");

}

}

52. Write a function to insert the given item into a Binary Search Tree (BST).

#include

#include

struct bst //bst-->binary search tree

{

int element;

struct bst *left,*right;

}*root;

typedef struct bst *node;

node insert(int,node);

void display(node,int);

int main()

{

int ch;

int a;

// node temp;

while(1)

{

printf("\n1.Insert \n2.Display\n3.Exit\nEnter ur choice:");

scanf("%d",&ch);

switch(ch)

{

case 1:

printf("Enter an integer element to insert");

scanf("%d",&a);

root=insert(a,root);

break;

case 2:

if(root==NULL)

printf("\nEmpty tree");

else

display(root, 1);

break;

case 3:

exit(0);

break;

default:

printf("Exit");

}

}

}

//Insert an element into BST

node insert(int x,node t)

{

if(t==NULL)//if root is null

{

t=(node)malloc(sizeof(node));

t->element=x;

return t;

t->left=NULL;

t->right=NULL;

}

else

{

if(xelement) {

t->left=insert(x,t->left);

return t;}

else if(x>t->element){

t->right=insert(x,t->right); return t;}

}

return t;

}

void display(node t,int level)

{

int i;

if(t)

{

display(t->right,level+1);

printf("\n");

for(i=0;ielement);

display(t->left,level+1);

}

}

53. Write a function to delete a given item from the Binary Search Tree (BST).

#include

#include

struct bst //bst-->binary search tree

{

int element;

struct bst *left,*right;

}*root;

typedef struct bst *node;

node insert(int,node);

node del(int,node);

//void display(node,int);

void display(node);//to print inorder and postorder traversal

node minValue(node);

int main()

{

int ch;node temp;

int a;

while(1)

{

printf("\n1.Insert \n2.Display\n3.Delete\n4.Exit\nEnter ur choice:");

scanf("%d",&ch);

switch(ch)

{

case 1:

printf("Enter an integer element to insert");

scanf("%d",&a);

root=insert(a,root);

break;

case 2:

if(root==NULL)

printf("\nEmpty tree");

else

// display(root,1);

display(root);

break;

case 3:printf("Enter an element to delete");

scanf("%d",&a);

if(root==NULL)

printf("Empty tree");

else

root=del(a,root);

// printf("Deleted item is %d",root);

break;

case 4:

exit(0);

break;

default:

printf("Exit");

}

}

}

//Insert an element into BST

node insert(int x,node t)

{

if(t==NULL)//if root is null

{

t=(node)malloc(sizeof(node));

t->element=x;

return t;

t->left=NULL;

t->right=NULL;

}

else

{

if(xelement) {

t->left=insert(x,t->left);

return t;}

else if(x>t->element){

t->right=insert(x,t->right); return t;}

}

return t;

}

//delete an element from the BST

node del(int x,node t)

{

node temp;

if(t==NULL)

printf("Empty tree");

else

{ //if the entered element does not have any children

if(xelement)

t->left=del(x,t->left);

else if(x>t->element)

t->right=del(x,t->right);

else

{ //if the enetered element has two children

if(t->left&&t->right)

{ //replace the deleted node with minvalue of the children

temp=minValue(t->right);

t->element=temp->element;

t->right=del(t->element,t->right);

}

else if(t->left==NULL)

t=t->right;

else

t=t->left;

}

}return t;

}

/*void display(node t,int level)*/

void display(node t) //for inorder and postorder display

{

int i;

if(t)

{/*

display(t->right,level+1);

printf("\n");

for(i=0;ielement);

display(t->left,level+1);*/

/* //inorder traversal

display(t->left);

printf("%d",t->element);

display(t->right);*/

//postorder traversal

display(t->left);

display(t->right);

printf("%d",t->element);

}

}

/* find the minimum value..In a BST minimum value will be always present on the left side*/

node minValue(node temp)

{

if(temp==NULL||temp->left==NULL)

return temp;

return minValue(temp->left);

}

/*Inorder traversal

void inorder(node */

54. Write a function to find if the given list has duplicate item, if so delete them and sort the list.

(Should not create another list)

#include

#include

#include

#define MAX 10

struct list

{

int num;

struct list * next;

}*head,*end;

typedef struct list * node;

void add(int);

void display();

void sort();

void removeDuplicates(node);

int main()

{

int ch,x,y;

do

{

printf("\n\n1.Add element\n");

printf("\n2.Display all the elements\n");

printf("\n3.Sort the list\n");

printf("\n4.Find and remove duplicates\n");

printf("\n5.EXIT \n");

printf("\nSelect any one of the above : ");

scanf("%d",&ch);

switch(ch)

{

case 1: printf("\nEnter the element to add:");

scanf("%d",&x);

add(x);

break;

case 2: display();

break;

case 3: sort();

display();

break;

case 4: sort();

removeDuplicates(head);

display();

break;

default: printf("\n EXIT\n");

}

}while(ch!=5);

return EXIT_SUCCESS;

}

//insert an element into list

void add(int x)

{

node temp,newnode;

if(head==NULL)

{

head=malloc(sizeof *head);

head->num=x;

head->next=NULL;

end=head;

}

else

{

/* temp=head;

while(temp->next!=NULL)

temp=temp->next;*/

for(temp=head;temp->next!=NULL;temp=temp->next);

newnode=(node) malloc(sizeof(node));

temp->next=newnode;

newnode->num=x;

newnode->next=NULL;

end=newnode;

}

}

//sort the element in list

void sort()

{

node a = NULL,b=NULL,c=NULL,e=NULL,tmp=NULL;

// the `c' node precedes the `a' and `e' node

while(e != head->next) {

c = a = head;

b = a->next;

while(a != e) {

if(a->num > b->num) {

if(a == head) {

tmp = b -> next;

b->next = a;

a->next = tmp;

head = b;

c = b;

} else {

tmp = b->next;

b->next = a;

a->next = tmp;

c->next = b;

c = b;

}

} else {

c = a;

a = a->next;

}

b = a->next;

if(b == e)

e = a;

}

}

}

//display the list

void display()

{

node temp=head;

if(head==NULL)

printf("List is empty");

else

{

do

{

printf("%d-> ",temp->num);

temp=temp->next;

}while(temp!=NULL);

}

}

//remove duplicates in the sorted list

void removeDuplicates(node head)

{

struct list *current=head;

node temp1=current->next;

if(current==NULL)

return;

while(temp1!=NULL)

{

if(current->num==temp1->num)//if the element and the next element are equal

{

// printf("dup is %d",current->num);

current->next=temp1->next;//point that element to the next one and free

free(temp1);

}

else

{

current=temp1;

temp1=temp1->next;

}

}

}

55. Use double linked lists and write functions to insert and delete items into a circular queue.

#include

#include

#include

#define EXIT 0

struct node

{

int num;

struct node * next;

struct node * prev;

}*head=NULL,*end=NULL;

typedef struct node * node;

void add(int);

void display();

void delete();

int i=2;

int main()

{

int ch,x,y;

do

{

printf("\n1.Add");

printf("\n2.Remove");

printf("\n3.Display");

printf("\n4.Exit");

printf("\nSelect any one of the above : ");

scanf("%d",&ch);

switch(ch)

{

case 1: printf("\nEnter the element to add:");

scanf("%d",&x);

add(x);

break;

case 2: delete();

break;

case 3: display();

break;

default: printf("\n EXIT\n");

}

}while(ch!=4);

return EXIT;

}

void add(int x)

{

node temp=head,newnode;

newnode=(node) malloc(sizeof(node));

if(head==NULL)

{

head=(node) malloc(sizeof(node));

head->num=x;

head->next=NULL;

head->prev=NULL;

end=head;

}

else if(head==end)

{

temp->next=newnode;

temp->prev=newnode;

newnode->prev=temp;

newnode->next=temp;

newnode->num=x;

end=newnode;

}

else

{ i++;

while(temp->next!=head)

{

temp=temp->next;

}

temp->next=newnode;

newnode->prev=temp;

newnode->num=x;

newnode->next=head;

head->prev=newnode;

end=newnode;

}

}

void delete()

{

if(head==NULL)

printf("\nList is empty, No elements to delete\n");

else

{

node temp=head;

head=head->next;

head->prev=end;

end->next=head;

free(temp);

printf("Element deleted");

}

}

void display()

{

node temp=head;

if(head==NULL)

printf("List is empty");

else

{

do

{ printf("%d-> ",temp->num);

temp=temp->next;

}while(temp->next!=head->next && head->next!=NULL);

}

}

Output:

[pmanne@oradb ~]$ gcc circ_queue.c

[pmanne@oradb ~]$ ./a.out

1.Add

2.Remove

3.Display

4.Exit

Select any one of the above : 1

Enter the element to add:1

1.Add

2.Remove

3.Display

4.Exit

Select any one of the above : 1

Enter the element to add:2

1.Add

2.Remove

3.Display

4.Exit

Select any one of the above : 1

Enter the element to add:3

1.Add

2.Remove

3.Display

4.Exit

Select any one of the above : 3

1-> 2-> 3->

1.Add

2.Remove

3.Display

4.Exit

Select any one of the above : 4

EXIT

56. Write a function to merge two sorted single linked list. ( should not create another new list)

57. Write a C program to multiply two different order polynomials.

58. Use pointers and function pointers to add, subtract and multiply two matrices/real numbers.

#include

int *a,*b,*c;

int i=0,j=0,k=0;

int r1,c1,r2,c2;

int *add(int*,int*,int,int,int,int);

int *sub(int*,int*,int,int,int,int);

int *mul(int*,int*,int,int,int,int);

int main()

{

printf("\nMatrix A\n"); /*Enter the size for matrix A */

printf("\nEnter Number Of Rows : ");

scanf("%d",&r1);

printf("\nEnter Number Of Columns : ");

scanf("%d",&c1);

printf("\n Matrix B\n"); /*Enter the size for matrix B */

printf("\nEnter Number Of Rows : ");

scanf("%d",&r2);

printf("\nEnter Number Of Columns : ");

scanf("%d",&c2);

printf("\n\n MATRIX A : \n");/* Enter elements for Matrix A*/

a=(int*) malloc(sizeof(int)*r1*c1); /*allocate memory for a */

for(i=0;i ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download