Advertisements

Search results for else

Example:[1]

            Write C function to calculate the factorial of a number and illustrate the use of recursion. 
          

            #include <stdio.h>
#include <stdlib.h>
int fact(int );
int main() {
  int num = 5;
  int res;
  res = fact(num);
  printf("Factorial is %d\n",res);
  return 0;
}
int fact(int x)
{
  if(x > 1)
  {
    return x * fact(x-1);
  }else
  {
    return 1;
  }
}

          
Example:[2]

            Write a C Program macro to check if a character is alphabetic or not.
          

            #include <stdio.h>
#define ALPHA(CH) ((CH >= 'a' && CH <= 'z') || (CH >= 'A' && CH <= 'Z')?1:0)
int alp(int );
int main()
{
    int al;
    char a = 'a';
    al = alp(a);
    if(al == 1)
    {
        printf("the given char is alpha");
    }else
    {
        printf("the given char is not alpha");
    }
    return 0;
}
int alp(int a)
{
    return ALPHA(a);
}
          
Example:[3]

            Write a C program to check whether year is leap year or not using conditional/ternary operator.
          

            #include <stdio.h>
#define YEAR(X) ((X%4==0)||(X%400==0) ? 1:0)
int a = 2013;
int Leap_Year(int a);
int main()
{
    int num1;
    num1 = Leap_Year(a);
    if (num1 == 1)
    {
        printf("the given year is a leap year");
    }
    else
    {
        printf("the given year is not a leap year");
    }
    return 0;
}
int Leap_Year(int a)
{
    return YEAR(a);
}
          
Example:[4]

            Write a C program to find longest sequence of elements in an array.
          

            #include <stdio.h>
int result = 0,f_result = 0;
unsigned int find_long(int a[]);
int main()
{
    int aa[] = {0,2,3,8,9,10,11,12,18,19,20,21,22,23,24};
    unsigned int lon;
    lon = find_long(aa);
    printf("The Long sequence is %d\n",lon);
    return 0;
}
unsigned int find_long(int a[])
{
    int len = 15;
    unsigned int i;
    for(i = 1;i < len;i++)
    {
        if((a[i] - a[i-1]) == 1)
        {
            result++;
            if(result >= f_result)
            {
                f_result = result;
                printf("f_result is %d\n",f_result);
            }
        }else
            {
                result = 0;
                printf("result is %d\n",result);
            }
    }
    return f_result+1;
}

          
Example:[5]

            Write a C program function to take a string as its input and return the first word of the string passed to it.

          

            #include <stdio.h>
void getfword(char *s, char f_word[]);
int main() {
	char str[] = "Hello how are you";
	char res[20];
	getfword(str,res);
	printf("Result is:%s\n", res);
}
void getfword(char *s, char f_word[])
{
	int i = 0;
	while(s[i] != '\0')
	{
		if(s[i] == ' ')
		{
			break;
		}else
		{
			f_word[i] = s[i];
			f_word[i + 1] = '\0';
			i++;
		}
	}
}

          
Example:[6]

            Write a function to determine whether the machine is Little Endian or Big Endian.
          

            #include <stdio.h>
unsigned int EndianCheck();
union endian
{
   unsigned int a;
   char b;
};
int main()
{
   unsigned int res;
   res = EndianCheck();
   if(res == 0)
   {
      printf("Little endian\n");
   }else
   {
      printf("Big endian\n");
   }
   return 0;
}
unsigned int EndianCheck()
{
   union endian test;
   test.a = 1;
   if(test.b == 1)
   {
      return 0;
   }else
   {
      return 1;
   }
}
          
Example:[7]

            Write a C program function to enter different book information(pages, author, search id) in a school library and display the existing books using structure pointers.

          

            #include <stdio.h>
#include <stdlib.h>
#define NO_BOOKS 3
struct Book
{
	int pages;
	char author[10];
	int search_id;
};
void EnterBooks(struct Book *);
void DisBooks(struct Book *);
int main()
{
	struct Book *b;
	b = (struct Book *)malloc(sizeof(struct Book)*NO_BOOKS);
	if(b == NULL)
	{
		printf("Req memory allocation failed\n");
	}else
	{
		EnterBooks(b);
		DisBooks(b);
	}
	free(b);
	return 0;
}
void EnterBooks(struct Book *a)
{
	int i;
	for(i = 0;i < NO_BOOKS;i++)
	{
		printf("Enter %d book pages\n", i+1);
		scanf("%d",&a->pages);
		printf("Enter %d book author\n", i+1);
		scanf("%s",&a->author);
		printf("Enter %d search id\n", i+1);
		scanf("%d",&a->search_id);
		a++;
	}
}
void DisBooks(struct Book *a)
{
	int i;
	for(i = 0;i < NO_BOOKS;i++)
	{
		printf("Book %d pages %d\n",i+1,a->pages);
		printf("Book %d author %s\n",i+1,a->author);
		printf("Book %d book search id %d\n",i+1,a->search_id);
		a++;
	}
}

          
Example:[8]

            Write a C program function to enter different book information(pages, author, search id) in a school library and search any book using search id and structure pointers.
          

            #include <stdio.h>
#include <stdlib.h>
#define NO_BOOKS 3
struct Book
{
	int pages;
	char author[10];
	int search_id;
};
void EnterBooks(struct Book *);
struct Book *SrchBook(struct Book *);
int main()
{
	struct Book *b;
	b = (struct Book *)malloc(sizeof(struct Book)*NO_BOOKS);
	if(b == NULL)
	{
		printf("Req memory allocation failed\n");
	}else
	{
		EnterBooks(b);
		struct Book *s;
		s = SrchBook(b);
		printf("Book pages:%d\n", s->pages);
		printf("Book author:%s\n", s->author);
		printf("Book search_id:%d\n", s->search_id);
	}
	free(b);
	return 0;
}
void EnterBooks(struct Book *a)
{
	int i;
	for(i = 0;i < NO_BOOKS;i++)
	{
		printf("Enter %d book pages\n", i+1);
		scanf("%d",&a->pages);
		printf("Enter %d book author\n", i+1);
		scanf("%s",&a->author);
		printf("Enter %d search id\n", i+1);
		scanf("%d",&a->search_id);
		a++;
	}
}
struct Book *SrchBook(struct Book *a)
{
	int i,id;
	printf("Enter the book search id\n");
	scanf("%d",&id);
	for(i = 0;i < NO_BOOKS;i++)
	{
		if(id == a->search_id)
		{
			return &a[i];
		}
		a++;
	}
	if(i == NO_BOOKS)
	{
		printf("Invalid Search_id, try again\n");
		return NULL;
	}
}
          
Example:[9]

            Write a C program to get the status of nth bit of a number.
          

            #include <stdio.h>
unsigned int CheckBit(unsigned int ,unsigned int );
int main()
{
    unsigned int r = 11;
    r = CheckBit(r,1);
    printf("Bit %d status is %d\n", 1, r);
    return 0;
}
unsigned int CheckBit(unsigned int a,unsigned int x)
{
    if(((a >> x) & 1) == 1)
    {
        return 1;
    }else
    {
        return 0;
    }
}
          
Example:[10]

            Write a c program function to display all node data present in a singly linked list.
          

            #include <stdio.h>
#include <stdlib.h>
struct Node
{
	int data;
	struct Node *next;
};
struct Node *head = NULL;
void AddAtBegin(int );
void Traverse();
void main()
{
	AddAtBegin(10);
	Traverse();
}
void AddAtBegin(int val)
{
	struct Node *ptr;
	if(head == NULL)
	{
		ptr = (struct Node *)malloc(sizeof(struct Node ));
		if(ptr == NULL)
		{
			printf("Req memory allocation failed\n");
		}else
		{
			ptr->data = val;
			ptr->next = NULL;
			head = ptr;
		}
	}else
	{
		ptr = (struct Node *)malloc(sizeof(struct Node ));
		if(ptr == NULL)
		{
			printf("Req memory allocation failed\n");
		}else
		{
			ptr->data = val;
			ptr->next = head;
			head = ptr;
		}
	}
}
void Traverse()
{
	struct Node *ptr = head;
	int n = 1;
	while(ptr != NULL)
	{
		printf("Node %d data %d\n", n,ptr->data);
		ptr = ptr->next;
		n++;
	}	
}
          
Example:[11]

            Write a c singly linked list program to add a node at the beginning of the list.
          

            #include <stdio.h>
#include <stdlib.h>
struct Node
{
	int data;
	struct Node *next;
};
struct Node *head = NULL;
void AddAtBegin(int );
void main()
{
	AddAtBegin(10);
}
void AddAtBegin(int val)
{
	struct Node *ptr;
	if(head == NULL)
	{
		ptr = (struct Node *)malloc(sizeof(struct Node ));
		if(ptr == NULL)
		{
			printf("Req memory allocation failed\n");
		}else
		{
			ptr->data = val;
			ptr->next = NULL;
			head = ptr;
		}
	}else
	{
		ptr = (struct Node *)malloc(sizeof(struct Node ));
		if(ptr == NULL)
		{
			printf("Req memory allocation failed\n");
		}else
		{
			ptr->data = val;
			ptr->next = head;
			head = ptr;
		}
	}
}
          
Example:[12]

            Write a c program function to add new nodes at the end of a singly linked list.
          

            #include <stdio.h>
#include <stdlib.h>
struct Node
{
	int data;
	struct Node *next;
};
struct Node *head = NULL;
void AddAtEnd(int );
void Traverse();
void main()
{
	AddAtEnd(10);
	AddAtEnd(20);
	Traverse();
	AddAtEnd(30);
	Traverse();
}
void AddAtEnd(int val)
{
	struct Node *ptr,*aptr = head,*bptr;
	if(head == NULL)
	{
		ptr = (struct Node *)malloc(sizeof(struct Node ));
		if(ptr == NULL)
		{
			printf("Req memory allocation failed\n");
		}else
		{
			ptr->data = val;
			ptr->next = NULL;
			head = ptr;
		}
	}else
	{
		ptr = (struct Node *)malloc(sizeof(struct Node ));
		if(ptr == NULL)
		{
			printf("Req memory allocation failed\n");
		}else
		{
			while(aptr != NULL)
			{
				bptr = aptr;
				aptr = aptr->next;
			}
			ptr->data = val;
			bptr->next = ptr;
			ptr->next = NULL;
		}
	}
}
void Traverse()
{
	struct Node *ptr = head;
	int n = 1;
	while(ptr != NULL)
	{
		printf("Node %d data %d\n", n,ptr->data);
		ptr = ptr->next;
		n++;
	}	
}
          
Example:[13]

            Write a c program function to add new nodes in the middle of a singly linked list.
          

            #include <stdio.h>
#include <stdlib.h>
struct Node
{
	int data;
	struct Node *next;
};
struct Node *head = NULL;
void AddAtEnd(int );
void AddAtMid(int ,int );
void Traverse();
void main()
{
	AddAtEnd(10);
	AddAtEnd(20);
	AddAtEnd(30);
	AddAtMid(25,2);
	Traverse();
}
void AddAtEnd(int val)
{
	struct Node *ptr,*aptr = head,*bptr;
	if(head == NULL)
	{
		ptr = (struct Node *)malloc(sizeof(struct Node ));
		if(ptr == NULL)
		{
			printf("Req memory allocation failed\n");
		}else
		{
			ptr->data = val;
			ptr->next = NULL;
			head = ptr;
		}
	}else
	{
		ptr = (struct Node *)malloc(sizeof(struct Node ));
		if(ptr == NULL)
		{
			printf("Req memory allocation failed\n");
		}else
		{
			while(aptr != NULL)
			{
				bptr = aptr;
				aptr = aptr->next;
			}
			ptr->data = val;
			bptr->next = ptr;
			ptr->next = NULL;
		}
	}
}
void Traverse()
{
	struct Node *ptr = head;
	int n = 1;
	while(ptr != NULL)
	{
		printf("Node %d data %d\n", n,ptr->data);
		ptr = ptr->next;
		n++;
	}	
}
void AddAtMid(int val,int pos)
{
	struct Node *aptr = head,*bptr = head,*cptr;
	int count = 0;
	if(head == NULL)
	{
		printf("List is empty\n");
	}else
	{
		while(aptr != NULL)
		{
			aptr = aptr->next;
			count++;
		}
		printf("%d\n", count);
		if(count < pos)
		{
			printf("We have less nodes than %d\n", pos);
		}else
		{
			aptr = (struct Node *)malloc(sizeof(struct Node ));
			if(aptr == NULL)
			{
				printf("Req memory allocation failed\n");
			}else
			{
				count = 0;
				while(count != pos)
				{
					cptr = bptr;
					bptr = bptr->next;
					count++;	
				}	
				aptr->data = val;
				cptr->next = aptr;
				aptr->next = bptr; 
			}
		}
	}
}
          

 Search Links


 Popular C search MCQs


 Popular C search examples