Advertisements

Search results for for

Example:[1]

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

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

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

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

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

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

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

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

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

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

            Write a c function to print asterisks on screen corresponding to each integer in an integer array.

          

            #include <stdio.h>
int count[10] = {1, 3, 5, 2, 5, 3, 5, 6, 8, 10};
int main(void)
{
	int i,j;
	for (i = 0; i < sizeof(count) / sizeof(count[0]); i++)
	{
		for (j = 0; j < count[i]; j++)
		{
			printf("*");  /* Print asterisk              */
		}
		printf("\n"); /* Move to the next line       */
	}
}

          
Example:[12]

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

            Write a c program to calculate the first 20 Fibonacci numbers.
          

            #include <stdio.h>
int main( void ) {
	int i, fibonacci[ 20 ];
	fibonacci[ 0 ] = 0;
	fibonacci[ 1 ] = 1;
	for( i = 2; i < 20; i++ )
		fibonacci[ i ] = fibonacci[ i - 2 ] + fibonacci[ i - 1 ];
	for( i = 0; i < 20; i++ )
		printf( "Fibonacci[ %d ] = %f\n", i, fibonacci[ i ] );
}
          
Example:[14]

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

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

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

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

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

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

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

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

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

            #include <stdio.h>
void Swap(int *,int *);
void main()
{
    int num1 = 10, num2 = 20, res;
    printf("Before swap %d,%d,num2",num1,num2);
    res = Swap(&num1,&num2);
    printf("After swap %d,%d",num1,num2);
}
void Swap(int *a,int *b)
{
    int temp;
    temp = *a;
    *a = *b;
    *b = temp;
}

          
Example:[22]

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

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

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

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

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

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

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

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

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

            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