C-DIT DCA Reference Questions and Answers – C Programming

C Programs:-
 
String reverse in c
 
#include
#include
main()
{
   char arr[100];
    printf("Enter a string to reverse\n");
   gets(arr);
    strrev(arr);
    printf("Reverse of entered string is \n%s\n",arr);
    return 0;
}
 
 
Length of a string
 
#include
#include
main()
 
{
                    char a[100];
                    int length;
                    printf("Enter a string \n");
                    gets(a);
                    length=strlen(a);
                    printf("The length of string is\n%d\n",length);
                    return 0;
}
 
 
STRCMP
 
#include
#include
main()
 
{
                    char a[100],b[100];
                    printf("Enter first string \n");
                    gets(a);
                    printf("enter second string \n");
                    gets(b);
                    if(strcmp(a,b)==0)
                    printf("Entered strings are equal \n");
                    else
                    printf("Entered string are not equal \n");
}
 
 
Plindrome
 
#include
#include
#include
main()
{
                    int a, reverse=0, temp;
                    printf("Enter a number \n");
                    scanf("%d",&a);
                    temp=a;
                    
                    while(temp!=0)
                    {
                                         reverse=reverse*10;
                                         reverse=reverse+temp%10;
                                         temp=temp/10;
                    }
                    if(a==reverse)
                    printf("Palindrome \n%d\n",a);
                    else
                    printf("Not Palindrome \n%d\n",a);
                    return 0;
}
 
Amstrong Number 
 
#include
#include
#include
main()
{
                    int a, reverse=0, temp;
                    printf("Enter a number \n");
                    scanf("%d",&a);
                    temp=a;
                    
                    while(temp!=0)
                    {
                                         remainder = temp%10;
                                        sum = sum + remainder*remainder*remainder;
                                         temp = temp/10;  
                    }
                    if(a==reverse)
                    printf("Palindrome \n%d\n",a);
                    else
                    printf("Not Palindrome \n%d\n",a);
                    return 0;
}
                    
 
Fibonacci
 
#include
#include
#include
main()
 
{
                    int n, first=0,second=1, next, c;
                    printf("Enter the number of terms \n");
                    scanf("%d",&n);
                    printf("First %d terms of fibonocci numbers are: \n",n);
                    for(c=0;c<n;c++)
                    {
                                         if(c<=1)
                                         {
                                         next=c;          
                                         }
                                         else
                                         {
                                         next=first+second;
                                         first=second;
                                         second=next;  
                                         }
                                         printf("%d\n",next);
                    }
                    return 0;
 
LeapYear
 
#include
main()
{
                    int y;
                    printf("Enter a year \n");
                    scanf("%d",&y);
                    if(y%400==0)
                                         printf("%d This is leap year\n",y);
                    else if(y%100==0)
                                         printf("%d is not a leap year\n",y);
                    else if(y%4==0)
                                         printf("%d This is leap year\n",y);
                    else
                                         printf("%d is not a leap year\n",y);
                    return 0;
}
                    
 
STRCPY
#include
#include
main()
{
                    char s[50], d[50];
                    printf("enter sourse string\n");
                    scanf("%s",&s);
                    strcpy(d,s);
                    printf("Sourse String is %s\n",s);
                    printf("Destination string is %s\n",d);
                    return 0;
}                   
 
 
Prime or Not
 
#include
#include
#include
main()
{
                    int n,c=2;
                    printf("Enter a number to check prime or not\n\n");
                    scanf("%d",&n);
                    for(c=2;c<=n-1;c++)
                    {
                                         if(n%c==0)
                                         {
                                                             printf("%d is not a prime number\n\n",n);
                                                             break;
                                         }
                    }
                    if(n==c)
                    {
                                         printf("%d is prime number\n\n", n);
                    }
                    return 0;
}
 
 
Sum of Two Matrix
 
#include
#include
#include
main()
                    {
                    int r,c,a,b,first[10][10], second[10][10], sum[10][10];
                    printf("Enter the number of Rows and Columns\n");
                    scanf("%d%d",&r,&c);
                                         printf("Enter the elements of first matrix\n");
                    for(a=0;a<r;a++)
                    for(b=0;b<c;b++)
                    scanf("%d",&first[a][b]);
                    printf("Enter the elements of second matrix\n");
                    for(a=0;a<r;a++)
                    for(b=0;b<c;b++)
                    scanf("%d",&second[a][b]);
                                        for(a=0;a<r;a++)
                    for(b=0;b<c;b++)
                    sum[a][b]=first[a][b]+second[a][b];
                                         printf("Sum of matrix is\n");
                    for(a=0;a<r;a++)
                    {
                                         for(b=0;b<c;b++)
                                         printf("%d\t",sum[a][b]);
                                         printf("\n");
                    }
                    getch();
                    return 0;
}
 
 
Transpose   
#include
#include
#include
main()
 
{
                    int m, n, c, d, matrix[10][10], transpose[10][10];
                        printf("Enter the number of rows and columns of matrix ");
                       scanf("%d%d",&m,&n);
                       printf("Enter the elements of matrix \n");
                        for( c = 0 ; c < m ; c++ )
                       {
                          for( d = 0 ; d < n ; d++ )
                          {
                             scanf("%d",&matrix[c][d]);      }
                       }
                        for( c = 0 ; c < m ; c++ )
                       {
                          for( d = 0 ; d < n ; d++ )
                          {
                             transpose[d][c] = matrix[c][d];
                          }
                       }
                        printf("Transpose of entered matrix :-\n");
                        for( c = 0 ; c < n ; c++ )
                       {
                          for( d = 0 ; d < m ; d++ )
                          {             
                             printf("%d\t",transpose[c][d]);
                          }  
                          printf("\n");  }   
                    return 0;
}
 
 
Matix multiply
 
#include
 main()
{
   int m, n, p, q, c, d, k, sum = 0;
   int first[10][10], second[10][10], multiply[10][10];
    printf("Enter the number of rows and columns of first matrix\n");
   scanf("%d%d",&m,&n);
   printf("Enter the elements of first matrix\n");
   for (  c = 0 ; c < m ; c++ )
      for ( d = 0 ; d < n ; d++ )
         scanf("%d",&first[c][d]);
    printf("Enter the number of rows and columns of second matrix\n");
   scanf("%d%d",&p,&q);
    if ( n != p )
      printf("Matrices with entered orders can't be multiplied with each other.\n");
   else
   {
      printf("Enter the elements of second matrix\n");
 
      for ( c = 0 ; c < p ; c++ )
         for ( d = 0 ; d < q ; d++ )
            scanf("%d",&second[c][d]);
       for ( c = 0 ; c < m ; c++ )
      {
         for ( d = 0 ; d < n ; d++ )
         {
            for ( k = 0 ; k < p ; k++ )
            {
               sum = sum + first[c][k]*second[k][d];
            }
 
            multiply[c][d] = sum;
            sum = 0;
         }
      }
       printf("Product of entered matrices:-\n");
 
      for ( c = 0 ; c < m ; c++ )
      {
         for ( d = 0 ; d < q ; d++ )
            printf("%d\t",multiply[c][d]);
 
         printf("\n");
      }
   }
   return 0;
}
 
 
 
Sort String 
#include
#include
main()
{
      char string[100], ch, *pointer, *result, *temp;
      int c, length;
       printf("Enter a string\n");
      gets(string);
      length = strlen(string);
      temp = result = (char*)malloc(length+1);
       pointer = string;
       for ( ch = 'a' ; ch <= 'z' ; ch++ )
      {
          for ( c = 0 ; c < length ; c++ )
          {
              if ( *pointer == ch )
              {
                 *result = *pointer;
                 result++;
              }
              pointer++;
          }
          pointer = string;
      }
      *result = '';
      result = temp;
      strcpy(string, result);
      free(result);      
      printf("%s\n", string);
       return 0;
}
 
 
Remove space 
#include
#include
#include
main()
{
   char string[100], *blank, *start;
   int length, c = 0, d = 0;
    printf("Enter a string\n");
   gets(string);
   length = strlen(string);
    blank = string;
    start = (char*)malloc(length+1);
    if ( start == NULL )
      exit(EXIT_FAILURE);
    while(*(blank+c))
   {
      if ( *(blank+c) == SPACE && *(blank+c+1) == SPACE )
      {}
      else
      {
         *(start+d) = *(blank+c);
                             d++;
      }
      c++;
   }
   *(start+d)='';
    printf("%s\n", start);
    free(start);     
    return 0;
}
 
 
 C Programs:-
 
String reverse in c
 
#include
#include
main()
{
   char arr[100];
    printf("Enter a string to reverse\n");
   gets(arr);
    strrev(arr);
    printf("Reverse of entered string is \n%s\n",arr);
    return 0;
}
 
 
Length of a string
 
#include
#include
main()
 
{
                    char a[100];
                    int length;
                    printf("Enter a string \n");
                    gets(a);
                    length=strlen(a);
                    printf("The length of string is\n%d\n",length);
                    return 0;
}
 
 
STRCMP
 
#include
#include
main()
 
{
                    char a[100],b[100];
                    printf("Enter first string \n");
                    gets(a);
                    printf("enter second string \n");
                    gets(b);
                    if(strcmp(a,b)==0)
                    printf("Entered strings are equal \n");
                    else
                    printf("Entered string are not equal \n");
}
 
 
Plindrome
 
#include
#include
#include
main()
{
                    int a, reverse=0, temp;
                    printf("Enter a number \n");
                    scanf("%d",&a);
                    temp=a;
                    
                    while(temp!=0)
                    {
                                         reverse=reverse*10;
                                         reverse=reverse+temp%10;
                                         temp=temp/10;
                    }
                    if(a==reverse)
                    printf("Palindrome \n%d\n",a);
                    else
                    printf("Not Palindrome \n%d\n",a);
                    return 0;
}
 
Amstrong Number 
 
#include
#include
#include
main()
{
                    int a, reverse=0, temp;
                    printf("Enter a number \n");
                    scanf("%d",&a);
                    temp=a;
                    
                    while(temp!=0)
                    {
                                         remainder = temp%10;
                                        sum = sum + remainder*remainder*remainder;
                                         temp = temp/10;  
                    }
                    if(a==reverse)
                    printf("Palindrome \n%d\n",a);
                    else
                    printf("Not Palindrome \n%d\n",a);
                    return 0;
}
                    
 
Fibonacci
 
#include
#include
#include
main()
 
{
                    int n, first=0,second=1, next, c;
                    printf("Enter the number of terms \n");
                    scanf("%d",&n);
                    printf("First %d terms of fibonocci numbers are: \n",n);
                    for(c=0;c<n;c++)
                    {
                                         if(c<=1)
                                         {
                                         next=c;          
                                         }
                                         else
                                         {
                                         next=first+second;
                                         first=second;
                                         second=next;  
                                         }
                                         printf("%d\n",next);
                    }
                    return 0;
 
LeapYear
 
#include
main()
{
                    int y;
                    printf("Enter a year \n");
                    scanf("%d",&y);
                    if(y%400==0)
                                         printf("%d This is leap year\n",y);
                    else if(y%100==0)
                                         printf("%d is not a leap year\n",y);
                    else if(y%4==0)
                                         printf("%d This is leap year\n",y);
                    else
                                         printf("%d is not a leap year\n",y);
                    return 0;
}
                    
 
STRCPY
#include
#include
main()
{
                    char s[50], d[50];
                    printf("enter sourse string\n");
                    scanf("%s",&s);
                    strcpy(d,s);
                    printf("Sourse String is %s\n",s);
                    printf("Destination string is %s\n",d);
                    return 0;
}                   
 
 
Prime or Not
 
#include
#include
#include
main()
{
                    int n,c=2;
                    printf("Enter a number to check prime or not\n\n");
                    scanf("%d",&n);
                    for(c=2;c<=n-1;c++)
                    {
                                         if(n%c==0)
                                         {
                                                             printf("%d is not a prime number\n\n",n);
                                                             break;
                                         }
                    }
                    if(n==c)
                    {
                                         printf("%d is prime number\n\n", n);
                    }
                    return 0;
}
 
 
Sum of Two Matrix
 
#include
#include
#include
main()
                    {
                    int r,c,a,b,first[10][10], second[10][10], sum[10][10];
                    printf("Enter the number of Rows and Columns\n");
                    scanf("%d%d",&r,&c);
                                         printf("Enter the elements of first matrix\n");
                    for(a=0;a<r;a++)
                    for(b=0;b<c;b++)
                    scanf("%d",&first[a][b]);
                    printf("Enter the elements of second matrix\n");
                    for(a=0;a<r;a++)
                    for(b=0;b<c;b++)
                    scanf("%d",&second[a][b]);
                                        for(a=0;a<r;a++)
                    for(b=0;b<c;b++)
                    sum[a][b]=first[a][b]+second[a][b];
                                         printf("Sum of matrix is\n");
                    for(a=0;a<r;a++)
                    {
                                         for(b=0;b<c;b++)
                                         printf("%d\t",sum[a][b]);
                                         printf("\n");
                    }
                    getch();
                    return 0;
}
 
 
Transpose   
#include
#include
#include
main()
 
{
                    int m, n, c, d, matrix[10][10], transpose[10][10];
                        printf("Enter the number of rows and columns of matrix ");
                       scanf("%d%d",&m,&n);
                       printf("Enter the elements of matrix \n");
                        for( c = 0 ; c < m ; c++ )
                       {
                          for( d = 0 ; d < n ; d++ )
                          {
                             scanf("%d",&matrix[c][d]);      }
                       }
                        for( c = 0 ; c < m ; c++ )
                       {
                          for( d = 0 ; d < n ; d++ )
                          {
                             transpose[d][c] = matrix[c][d];
                          }
                       }
                        printf("Transpose of entered matrix :-\n");
                        for( c = 0 ; c < n ; c++ )
                       {
                          for( d = 0 ; d < m ; d++ )
                          {             
                             printf("%d\t",transpose[c][d]);
                          }  
                          printf("\n");  }   
                    return 0;
}
 
 
Matix multiply
 
#include
 main()
{
   int m, n, p, q, c, d, k, sum = 0;
   int first[10][10], second[10][10], multiply[10][10];
    printf("Enter the number of rows and columns of first matrix\n");
   scanf("%d%d",&m,&n);
   printf("Enter the elements of first matrix\n");
   for (  c = 0 ; c < m ; c++ )
      for ( d = 0 ; d < n ; d++ )
         scanf("%d",&first[c][d]);
    printf("Enter the number of rows and columns of second matrix\n");
   scanf("%d%d",&p,&q);
    if ( n != p )
      printf("Matrices with entered orders can't be multiplied with each other.\n");
   else
   {
      printf("Enter the elements of second matrix\n");
 
      for ( c = 0 ; c < p ; c++ )
         for ( d = 0 ; d < q ; d++ )
            scanf("%d",&second[c][d]);
       for ( c = 0 ; c < m ; c++ )
      {
         for ( d = 0 ; d < n ; d++ )
         {
            for ( k = 0 ; k < p ; k++ )
            {
               sum = sum + first[c][k]*second[k][d];
            }
 
            multiply[c][d] = sum;
            sum = 0;
         }
      }
       printf("Product of entered matrices:-\n");
 
      for ( c = 0 ; c < m ; c++ )
      {
         for ( d = 0 ; d < q ; d++ )
            printf("%d\t",multiply[c][d]);
 
         printf("\n");
      }
   }
   return 0;
}
 
 
 
Sort String 
#include
#include
main()
{
      char string[100], ch, *pointer, *result, *temp;
      int c, length;
       printf("Enter a string\n");
      gets(string);
      length = strlen(string);
      temp = result = (char*)malloc(length+1);
       pointer = string;
       for ( ch = 'a' ; ch <= 'z' ; ch++ )
      {
          for ( c = 0 ; c < length ; c++ )
          {
              if ( *pointer == ch )
              {
                 *result = *pointer;
                 result++;
              }
              pointer++;
          }
          pointer = string;
      }
      *result = '';
      result = temp;
      strcpy(string, result);
      free(result);      
      printf("%s\n", string);
       return 0;
}
 
 
Remove space 
#include
#include
#include
main()
{
   char string[100], *blank, *start;
   int length, c = 0, d = 0;
    printf("Enter a string\n");
   gets(string);
   length = strlen(string);
    blank = string;
    start = (char*)malloc(length+1);
    if ( start == NULL )
      exit(EXIT_FAILURE);
    while(*(blank+c))
   {
      if ( *(blank+c) == SPACE && *(blank+c+1) == SPACE )
      {}
      else
      {
         *(start+d) = *(blank+c);
                             d++;
      }
      c++;
   }
   *(start+d)='';
    printf("%s\n", start);
    free(start);     
    return 0;
}
 
 
 

About keralawebdesigner

Web and Graphic Designer

Posted on December 4, 2011, in CDIT DCA Model Questions and Answers. Bookmark the permalink. Leave a comment.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: