Advertisements

Search results for return

Example:[1]

            Write a c program function to calculate the circumference of a circle if the radius of the circle is passed to it.

          

            #include <stdio.h>
#define PI 3.14
float Circum(int );
int main()
{
	int rad = 32;
	float r;
	r = Circum(rad);
	printf("The area of the circle is %f\n",r);
	return 0;
}
float Circum(int r)
{
	return (2 * PI * r);
}

          
Example:[2]

            Write a c program function to compute (a+b)^2 where the value of a and b will be provided by the user.
          

            #include <stdio.h>
int get_squreof_no(int ,int );
void main()
{
	int square,int1,int2;
	printf("Enter the value of int1:");
	scanf("%d",&int1);
	printf("Enter the value of int2:");
	scanf("%d",&int2);
	square = get_squreof_no(int1,int2);
    printf("The square result is:%d\n", square);
}
int get_squreof_no(int a, int b)
{
	int r;
	r = (a*a+b*b+2*a*b);
	return r;
}

          
Example:[3]

            Write a c program function to accept 2 integers and return the result after adding them and then dividing the result by 2.
          

            #include <stdio.h>
int add(int ,int );
int main()
{
	int a = 45;
	int b = 20;
	int r;
	r = add(a,b);
	printf("The result is %d\n",r);
	return 0;
}
int add(int x,int y)
{
	int res;
	res = (x + y)/2;
	return res;
}
          
Example:[4]

            Write a c program function to calculate the area of a circle if the radius of the circle is passed to it.
          

            #include <stdio.h>
#define PI 3.14
float Area(int );
int main()
{
	int rad = 32;
	float r;
	r = Area(rad);
	printf("The area of the circle is %f\n",r);
	return 0;
}
float Area(int r)
{
	return (PI * r * r);
}
          
Example:[5]

            Write a c program function to accept the temperature in Deg Centigrade and convert it to Fahrenheit.
          

            #include <stdio.h>
float celtofarenheit(int );
int main()
{
	int c = 32;
	float r;
	r = celtofarenheit(c);
	printf("The result is %f\n",r);
	return 0;
}
float celtofarenheit(int cel)
{
	return ((cel * 1.8) + 32);
}

          
Example:[6]

            Write a c program function to accept the principal, rate of interest, period of deposit and to calculate the simple interest.
          

            #include <stdio.h>
float sinterest(int ,float ,int );
int main()
{
   int princ = 10000;
   float rate = 3.875;
   float ra = rate/100;
   int period = 5;
   float r;
   r = sinterest(princ,ra,period);
   printf("The Simple interest is %f\n",r);
   return 0;
}
float sinterest(int p,float r,int t)
{
   return (p*(1 + r * t));
}
          
Example:[7]

            Write a c program function to calculate the sum of two integers where the two integers will be provided by the user.
          

            #include <stdio.h>
int Sum(int a,int b);
void main()
{
	int int1,int2,sum;
	printf("Enter first integer\n");
	scanf("%d",&int1);
	printf("Enter second integer\n");
	scanf("%d",&int2);
	sum = Sum(int1,int2);
	printf("The sum is:%d\n",sum);
}
int Sum(int a,int b)
{
 	return a + b;
}
          
Example:[8]

            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:[9]

            Write a program for illustrating the use of inline function in c programming.
          

            #include <stdio.h>
void inline test_inline_func1(int a, int b) 
{
    printf ("a=%d and b=%d\n", a, b);
}
int inline test_inline_func2(int x) 
{
    return x*x;
}
int main() {
    int tmp;
    test_inline_func1(2,4);
    tmp = test_inline_func2(5);
    printf("square val=%d\n", tmp);
    return 0;
}
          
Example:[10]

            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:[11]

            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:[12]

            Write a C program to return the bigger number between 2 integers using conditional/ternary operator.
          

            #include <stdio.h>
#define n1 10
#define n2 20
#define MAX(A,B) ((A > B) ? A : B)
int main()
{
    int s;
    s = MAX(n1,n2);
    printf("The max number between %d and %d is %d\n",n1,n2,s);
    return 0;
}
          
Example:[13]

            Write a c program function to calculate the sum of all digits of an integer number.
          

            #include <stdio.h>
int SumDigit(int );
int main()
{
    int res;
    res = SumDigit(1223);
    printf("The sum of digits %d\n", res);
    return 0;
}
int SumDigit(int num)
{
    int sum = 0,rem;
    while(num > 9)
    {
        rem = num%10;
        sum += rem;
        num /= 10;
    }
    sum += num;
    return sum;
}

          
Example:[14]

            Write a c program function to accept an integer number and return lsb first digit of the number.
          

            #include <stdio.h>
int FirstDigit(int );
int main()
{
    int res;
    res = FirstDigit(1223);
    printf("The lsb first digit is %d\n", res);
    return 0;
}
int FirstDigit(int num)
{
    int rem;
    rem = num % 10;
    return rem;
}
          
Example:[15]

            Write a c program function to accept an integer number and return msb last digit of the number.
          

            #include <stdio.h>
int LastDigit(int );
int main()
{
    int res;
    res = LastDigit(56431223);
    printf("The last msb digit is %d\n", res);
    return 0;
}
int LastDigit(int num)
{
    int rem;
    while(num > 9)
    {
        rem = num%10;
        num /= 10;
    }
    rem = num%10;
    return rem;
}
          
Example:[16]

            Write a C program function to convert Decimal to Binary number system.
          

            #include <stdio.h>
long binary(long int n);
int main()
{
	long int num = 29;
	long res;
	res = binary(num);
	printf("Result in binary is %d\n",res);
	return 0;
}
long binary(long int n)
{
	long rem,a = 1,bin = 0;
	while(n > 0)
	{
		rem = n%2;
		bin = bin + rem *a;
		n /= 2;
		a *= 10;
	}
	return bin;
}

          
Example:[17]

            Write a program to demonstrate the use of static keyword in C programming.
          

            #include <stdio.h>
static int p;
int f(void) {
  static int x = 0;
  x++;
  return x;
}
int y(void) {
  int x = 0;
  x++;
  return x;
}
int main() 
{
  int j;
  printf("Static variable x:%d\n", p);
  for (j = 0; j < 5; j++) {
    printf("Value of f(): %d\n", f());
  }
  for (j = 0; j < 5; j++) {
    printf("Value of y(): %d\n", y());
  }
  return 0;
}
          
Example:[18]

            Write a program to demonstrate the use of auto/local and global variables in C programming
          

            #include <stdio.h>
int autoFunc(int );
int glbvar = 10;
int glbvar1;
int main() {
  printf("glbvar:%d,glbvar1:%d\n", glbvar, glbvar1);
  int autovar = 20;
  int res;
  printf("res is:%d\n", res);
  res = autoFunc(autovar);
  printf("res is:%d\n", res);
  return 0;
}
int autoFunc(int autovar)
{
  printf("0th block:%d\n", autovar);
  {
    auto int autovar = 30;
    printf("Ist block:%d\n", autovar);
    {
      auto int autovar = 40;
      printf("2nd block:%d\n", autovar);
    }
  }
  glbvar += autovar;
  return glbvar;
}
          
Example:[19]

            Write a program with two files ext1.c and ext2.c to demonstrate the use of extern keyword
          

            /* File ext1.c */
#include <stdio.h>
void call_fun();
int val2 = 90;
int main()
{
  call_fun();
  return 0;
}

/* File ext2.c */
#include <stdio.h>
extern int val2;
void call_fun()
{
  printf("The value of val2 is %d\n",val2);
}
          
Example:[20]

            Write a c program function to find the maximum of three given integers.
          

            #include <stdio.h>
unsigned int max;
unsigned int maxi(unsigned int num1,unsigned int num2,unsigned int num3);
int main()
{
    unsigned int max_result;
    max_result = maxi(12,24,34);
    printf("The max result is %d\n",max_result);
    return 0;
}
unsigned int maxi(unsigned int num1,unsigned int num2,unsigned int num3)
{
    max = num1;
    if(num2 > max)
    {
        max = num2;
    }
    if(num3 > max)
    {
        max = num3;
    }
    return max;
}
          
Example:[21]

            Write a C program function to print a half pyramid using *
          

            #include <stdio.h>
void printing();
int main()
{
    printing();
    return 0;
}
void printing()
{
    int i,j;
    for (i = 0;i <= 6; i++)
    {
        for (j = 1; j <= i; ++j)
        {
            printf("*");
        }
        printf("\n ");
    }
}
          
Example:[22]

            Write a c program function to calculate the sum of even integers from 2 to 100.
          

            #include <stdio.h>
unsigned int evensum();
unsigned int sum;
int main()
{
    unsigned sum;
    sum = evensum();
    printf("sum is %d\n",sum);
    return 0;
}
unsigned int evensum()
{
    unsigned int i;
    for(i = 2;i <= 100;i += 2)
    {
        sum += i;
    }
    return sum;
}
          
Example:[23]

            Write a c program function to calculate the sum of odd integers from 1 to 100.
          

            #include <stdio.h>
unsigned int oddsum();
unsigned int sum;
int main()
{
    sum = oddsum();
    printf("sum is %d\n",sum);
    return 0;
}
unsigned int oddsum()
{
    unsigned int i;
    for(i = 1;i <= 100;i += 2)
    {
        sum += i;
    }
    return sum;
}
          
Example:[24]

            Write a c program function to create simple calculator using switch case.
          

            #include <stdio.h>
int main()
{
    char op;
    float num1, num2, result=0.0f;
    printf("Enter [number 1] [operator] [number 2]\n");
    scanf("%f %c %f", &num1, &op, &num2);
    switch(op)
    {
        case '+':
            result = num1 + num2;
            break;
        case '-':
            result = num1 - num2;
            break;
        case '*':
            result = num1 * num2;
            break;
        case '/':
            result = num1 / num2;
            break;
        default:
            printf("Unsupported Operator");
    }
    printf("%.2f %c %.2f = %.2f\n", num1, op, num2, result);
    return 0;
}
          
Example:[25]

            Write a c program function to check a given number is even parity(even no of 1s) or odd parity(odd no of 1s).
          

            #include <stdio.h>
int CheckParity(int );
int main()
{
	unsigned int num = 12;
	printf("Parity of %d,%s\n",num,(CheckParity(num)? "odd" : "even"));
	return 0;
}

int CheckParity(int n)
{
	int parity = 0;
	while(n)
	{
		parity = !parity;
		n = n & (n-1);
	}
	return parity;
}
          
Example:[26]

            Write a C program to create a triangle of letter A.
          

            #include <stdio.h>
int main()
{
	int i,count;
	for(i = 0;i < 15;i++)
	{
		for(count = 0;count < i;count++)
		{
			printf("%c",'A');
		}
		printf("\n");
		count = 0;
	}
	return 0;
}

          
Example:[27]

            Write a C program function  to take an integer array in array format and return the sum of all elements in the array.
          

            #include <stdio.h>
int sum_array(int a[]);
#define SIZE 6
int main()
{
   int array[SIZE]  = {2,3,4,5,6,7};
   int sum = 0;
   sum = sum_array(array);
   printf("The Sum of array elements will be:%d\n",sum);
   return 0;
}
int sum_array(int a[])
{
   int param = 0;
   int i = 0;
   for(i = 0;i <= SIZE;i++)
   {
      param += a[i];
   }
   return param;
}
          
Example:[28]

            Write a C program function  to take a character array in array format and return the last character of the array passed to it.
          

            #include <stdio.h>
#define SIZE 5
char char_count(char a[]);
int main()
{
	char arr[] = {'H','e','l','l','o'};
	char last;
	last = char_count(arr);
	printf("The last character of the array passed is %c\n",last);
	return 0;
}
char char_count(char array[])
{
	return array[SIZE-1];
}
          
Example:[29]

            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:[30]

            Write a program to accept a two dimensional integer array and return the sum of all elements in the array.
          

            #include <stdio.h>
int Sum2Arr(int a[2][3]);
int main()
{
	int res;
	int ar[2][3] = 
	{
		{1,2,3},
		{4,5,6}
	};
	res = Sum2Arr(ar);
	printf("The sum of 2d array integers:%d\n", res);
	return 0;
}
int Sum2Arr(int a[2][3])
{
	int i,j,sum = 0;
	for(i = 0;i < 2;i++)
	{
		for(j = 0;j < 3;j++)
		{
			printf("adding:%d\n", a[i][j]);
			sum += a[i][j];
		}
	}
	return sum;
}
          
Example:[31]

            Write a c program to add two 2-dimensional arrays or 2-D arrays.
          

            #include <stdio.h>
int main( void ) {
	int a[ 2 ][ 3 ] = { { 5, 6, 7 }, { 10, 20, 30 } };
	int b[ 2 ][ 3 ] = { { 1, 2, 3 }, {  3,  2,  1 } };
	int sum[ 2 ][ 3 ], row, column;

	/* First the addition */
	for( row = 0; row < 2; row++ )
		for( column = 0; column < 3; column++ )
			sum[ row ][ column ] =
					a[ row ][ column ] + b[ row ][ column ];
	/* Then print the results */
	printf( "The sum is: \n\n" );
	for( row = 0; row < 2; row++ ) {
		for( column = 0; column < 3; column++ )
			printf( "\t%d",  sum[ row ][ column ] );
		printf("\n");   /* at end of each row */
	}
	return 0;
}

          
Example:[32]

            Write a C program to count the number of characters in a string without using the c inbuilt library.
          

            #include <stdio.h>
int NoOfChars(char p[]);
int main()
{
    int no_chr;
    char str[] = "Hello How are you";
    no_chr = NoOfChars(str);
    printf("No of chars is: %d\n",no_chr);
    return 0;
}
int NoOfChars(char d[])
{
    int i,count = 0;
    for(i = 0;d[i] != '\0';i++)
    {
        count++;
    }
    return count;
}
          
Example:[33]

            Write a c program to accept a string as input and return the last character of the string as output of the function.
          

            #include <stdio.h>
char RetLastChar(char *str);
int main()
{
    char *in = "Hello";
    char res;
    res = RetLastChar(in);
    printf("The last char is %c\n",res);
    return 0;
}
char RetLastChar(char *str)
{
    int i;
    char out;
    for(i = 0;str[i] != '\0';i++)
    {
      out = str[i];
    }
    return out;
}
          
Example:[34]

            Write a c program function that reverses the string passed to it without declaring any new string within the function.
          

            #include <stdio.h>
void reverse(char a[]);
int main() {
   char str[10] = "Hello";
   reverse(str);
   printf("\nReverse string is :%s\n", str);
   return (0);
}
void reverse(char d[])
{
   int k,len = 0;
   char temp;
   int i = 0;
   for(k = 0;d[k] != '\0';k++)
   {
      len++;
   }
   while(i < (len - 1))
   {
         temp = d[i];
         d[i] = d[len-1];
         d[len-1] = temp;
         i++;
         len--;
   }
}
          
Example:[35]

            Write a program to find the number of times that a given word(i.e. a short string) occurs in a sentence (i.e. a long string!).
          

            #include <stdio.h>
#include <string.h>
unsigned int search_word(char *,char *);
int main()
{
    char *s_str = "the";
    char *l_str = "the word searching for from these sentence is the.";
    unsigned int result;
    result = search_word(s_str,l_str);
    printf("Small string is found %d times in long string\n",result);
    return 0;
}
unsigned int search_word(char *s1,char *s2)
{
    int times = 0;
    int i = 0,j = 0,k = 0;
    int size = strlen(s1);
    printf("Size of small string \" %s \" is: %d\n",s1,size);
    while(s2[j] != '\0')
    {
        if(j >= (size - 1))
        {
            if(s2[j] == s1[size-1] && s2[j-1] == s1[size-2] && s2[j-2] == s1[size-3])
            {
                times = times + 1;
            }
        }
        j++;
    }
    return times;
}
          
Example:[36]

            Write a c program function to accept a string and return the character written two times continuously in the string.
          

            #include <stdio.h>
char res_char(char *);
int main()
{
    char *str = "Hello how are you";
    char result;
    result = res_char(str);
    printf("Got result %c\n",result);
    return 0;
}
char res_char(char *f)
{
    char res;
    while(*f != '\0')
    {
        if(*f == *(f + 1))
        {
        res = *f;
        }
        f++;
    }
    return res;
}
          
Example:[37]

            Write a C program function to count frequency of each character in a string and return the maximum frequency.
          

            #include <stdio.h>
int freq_func(char *);
int main()
{
	char *s = "Here is the string that needs to be verified for the frequency of each character";
	int max_freq;
	max_freq = freq_func(s);
	printf("Max Frequency is:%d\n",max_freq);
	return 0;
}
int freq_func(char *r)
{
	int i =0,j = 0,res1 = 0,res2 = 0;
	while(r[j] != '\0')
	{
		while(r[i] != '\0')
		{
			if(r[j] == r[i])
			{
				res1 = res1 + 1;
			}
			i++;
		}
		if(res1 > res2)
		{
			res2 = res1;
		}
		i = 0;
		res1 = 0;
		j++;
	}
	return res2;
}
          
Example:[38]

            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:[39]

            Write a c program function to calculate the number of words present in a string passed to it.
          

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

          
Example:[40]

            Write a c program function to swap two integers using function pointers.
          

            #include <stdio.h>
void (*fptr)(int * ,int * );
void function(int * ,int * );
int main()
{
	int num1 = 20,num2 = 30;
	printf("Before:%d,%d\n", num1,num2);
	fptr = function;
	(*fptr)(&num1,&num2);
	printf("After:%d,%d\n", num1,num2);
	return 0;
}
void function(int *a,int *b)
{
	int copy;
	copy = *a;
	*a = *b;
	*b = copy;
}
          
Example:[41]

            Write a c program function to reverse the string passed to it using pointers and without using the c inbuilt library.
          

            #include <stdio.h>
#include <stdlib.h>
void RevString(char *,char *);
int main()
{
   char *str = "Hello";
   char *res;
   res = (char *)malloc(10);
   RevString(str,res);
   printf("Reversed string is %s\n",res);
   return 0;
}
void RevString(char *s,char *r)
{
   int i,j,k,len = 0;
   for(i = 0;*(s+i) != '\0';i++)
   {
      len++;
   }
   for(j = 0,k = len-1;*(s+j) != '\0',k > -1;j++,k--)
   {
      *(r+j) = *(s+k);
   }
}
          
Example:[42]

            Write a c program function to return the same string after reversing it using pointers
          

            #include <stdio.h> 
#include <string.h> 
void reverseString(char* str) 
{ 
    int l, i; 
    char *begin_ptr, *end_ptr, ch; 
    l = strlen(str); 
    begin_ptr = str; 
    end_ptr = str;  
    for (i = 0; i < l - 1; i++) 
    {
        end_ptr++; 
    } 
    for (i = 0; i < l / 2; i++) 
    { 
        ch = *end_ptr; 
        *end_ptr = *begin_ptr; 
        *begin_ptr = ch; 
        begin_ptr++; 
        end_ptr--; 
    } 
} 
int main() 
{ 
    char str[10] = "Hello"; 
    reverseString(str); 
    printf("Reverse of the string: %s\n", str); 
    return 0; 
} 

          
Example:[43]

            Write a C program function to return the 5th element of an array using pointer to a function.
          

            #include <stdio.h>
#define MEMBER_NO 5
int *pfun(int *,int );
int main(){
	int *ptr;
	int ar[9] = {11,22,33,44,55,66,77,88,99};
	ptr = pfun(ar,MEMBER_NO-1);
	printf("Arrray ar address: %p,ptr address:%p,*ptr value: %d",ar,ptr,*ptr);
	return 0;
}
int *pfun(int *a,int n){
	a = a + n;
	return a;
}
          
Example:[44]

            Write a c program to accept a string as input and return the number of words present in it using pointers.

          

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

          
Example:[45]

            Write a program to swap the 4bytes of an integer. If input is provided as 0x12345678 output is generated as 0x78563412.
          

            #include <stdio.h>
union out
{
   struct
   {
      unsigned char ll;
      unsigned char lh;
      unsigned char hl;
      unsigned char hh;
   }in;
   unsigned int b;
};
int main()
{
   union out aa;
   aa.b = 0x12345678;
   printf("Before swapping:%x\n", aa.b);
   unsigned char ll = aa.in.ll;
   unsigned char lh = aa.in.lh;
   aa.in.ll = aa.in.hh;
   aa.in.hh = ll;
   aa.in.lh = aa.in.hl;
   aa.in.hl = lh;
   printf("After swapping:%x\n", aa.b);
   return 0;
}
          
Example:[46]

            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:[47]

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

            #include <stdio.h>
#define NO_BOOKS 3
struct Book
{
	int pages;
	char author[10];
	int search_id;
};
void EnterBooks(struct Book *);
int main()
{
	struct Book b[NO_BOOKS];
	EnterBooks(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++;
	}
}
          
Example:[48]

            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:[49]

            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 arrays.

          

            #include <stdio.h>
#define NO_BOOKS 3
struct Book
{
	int pages;
	char author[10];
	int search_id;
};
void EnterBooks(struct Book *);
void DisplayBooks(struct Book *);
int main()
{
	struct Book b[NO_BOOKS];
	EnterBooks(b);
	DisplayBooks(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 DisplayBooks(struct Book a[])
{
	int i;
	for(i = 0;i < NO_BOOKS;i++)
	{
		printf("Book %d pages %d\n",i+1,a[i].pages);
		printf("Book %d author %s\n",i+1,a[i].author);
		printf("Book %d book search id %d\n",i+1,a[i].search_id);
	}
}
          
Example:[50]

            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 arrays.

          

            #include <stdio.h>
#define NO_BOOKS 3
struct Book
{
	int pages;
	char author[10];
	int search_id;
};
void EnterBooks(struct Book *);
struct Book *SrchBook(struct Book a[]);
int main()
{
	struct Book b[NO_BOOKS];
	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);
	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[i].search_id)
		{
			return &a[i];
		}
	}
	if(i == NO_BOOKS)
	{
		printf("Invalid Search_id, try again\n");
		return NULL;
	}
}
          
Example:[51]

            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:[52]

            Write a C program function  to swap the nibbles in a byte.
          

            #include <stdio.h>
unsigned char function(unsigned char );
int main()
{
	unsigned char pass = 0x12;
	printf("Input byte is: %x\n", pass);
	unsigned char res;
	res = function(pass);
	printf("The result is %x\n",res); 
}
unsigned char function(unsigned char d)
{
	return ((d << 4) | (d >> 4));
}

          
Example:[53]

            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:[54]

            Write a C program to set the 0th to 3rd bits of an integer.
          

            #include <stdio.h>
unsigned int SetBits(unsigned int ,unsigned int ,unsigned int);
int main()
{
    unsigned int r = 0;
    printf("Original %d\n", r);
    r = SetBits(r,0,3);
    printf("Result setbits is %d\n", r);
    return 0;
}
unsigned int SetBits(unsigned int a,unsigned int start,unsigned int end)
{
    int i;
    for(i = start;i <= end;i++)
    {
        a |= (1 << i);
    }
    return a;
}
          
Example:[55]

            Write a c program to clear the second bit of an integer from LSB.
          

            #include <stdio.h>
unsigned int ClearBit(unsigned int ,unsigned int );
unsigned int sum;
int main()
{
    unsigned int r = 11;
    printf("Original %d\n", r);
    r = ClearBit(r,1);
    printf("After clearing bit %d, result is %d\n", 1, r);
    return 0;
}
unsigned int ClearBit(unsigned int a,unsigned int x)
{
    return (a &= ~(1 << x));
}
          
Example:[56]

            Write a c program to find the current day of the week using enum.
          

            #include <stdio.h>
int CheckDay();
enum week
{
    sun=1,mon,tue,wed,thu,fri,sat
};
int main()
{
    int s;
    s = CheckDay();
    printf("Today is %dth day of the week\n",s);
    return 0;
}
int CheckDay()
{
    enum week today;
    today = wed;
    return today;
}
          
Example:[57]

            Write a c program to toggle the second bit of an integer from LSB.
          

            #include <stdio.h>
unsigned int ToggleBit(unsigned int ,unsigned int );
unsigned int sum;
int main()
{
    unsigned int r = 11;
    printf("Original %d\n", r);
    r = ToggleBit(r,1);
    printf("After toggling bit %d, result is %d\n", 1, r);
    return 0;
}
unsigned int ToggleBit(unsigned int a,unsigned int x)
{
    return (a ^= (1 << x));
}
          
Example:[58]

            Write a c program function to set bit 1 for a given number.
          

            #include <stdio.h>
unsigned int SetBit(unsigned int ,unsigned int );
unsigned int sum;
int main()
{
    unsigned int r = 9;
    printf("Original %d\n", r);
    r = SetBit(r,1);
    printf("After setting bit %d, result is %d\n", 1, r);
    return 0;
}
unsigned int SetBit(unsigned int a,unsigned int x)
{
    return (a |= 1 << x);
}
          

 Search Links


 Popular C search MCQs


 Popular C search examples