Search Here

Tuesday, November 01, 2011

Write a code of grading system using Switch---case

#include<stdio.h>
int main()
{
int a,m;
scanf("%d", &a);
m=a<=100 && a>=80?1:a<80 && a>=75?2:a<75 && a>=70?3:a<70 && a>=65?4:a<65 && a>=60?5:a<60 && a>=55?6:a<55 && a>=50?7:a<50 && a>=45?8:a<45 && a>=40?9:10;
switch(m)
{
case 1: printf("A+");
break;
case 2: printf("A");
break;
case 3: printf("A-");
break;
case 4: printf("B+");
break;
case 5: printf("B");
break;
case 6: printf("B-");
break;
case 7: printf("C+");
break;
case 8: printf("C");
break;
case 9: printf("D");
break;
default: printf("Fail");
break;
}

return 0;
}

Wednesday, March 30, 2011

Functional C programming

  • Write a function to calculate the factorial value of any integer entered through the keyboard.

    #include<stdio.h>
    #include<conio.h>
    long int factorial(int n); 
    void main()
    {       
    clrscr();
    long int n,res,x;
    scanf("%ld",&n);
    res=factorial(n);
    printf("%ld",res);
    getch();
    }
    long int factorial(int n)
    {
    int rem=1,i;
      for(i=n;i>=1;i--)
      rem=rem*i;
      return rem;
      }
  • Write a  function power(a,b)  to  calculate the value  of  a raised to b .

    #include<stdio.h>
    #include<conio.h>
    int power(int a,int b);
    void main()
    {
    clrscr();
    int i,x,sum,r,n;

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

    r=power(x,n);
    printf("%d",r);
    getch();
    }
    int power(int a,int b)
    {
    int i,res=1;
    for(i=b;i>=1;i--)
    res=a*res;
    return res;
    }
  • Write a function to calculate LCM of two numbers.

    #include<stdio.h>
    #include<conio.h>
    int lcm(int a,int b);
    void main()
    {
    clrscr();
    int a,b,i,x,y,res;
    scanf("%d%d",&x,&y);
    res=lcm(x,y);
    printf("LCM is:\n%d",res);
    getch();
    }

    int lcm(int a,int b)

    {
    int i;
    for(i=1; ;i++)
    {
    if(i%a==0&&i%b==0)
    break;
    }
    return i;
    }
  • Write a function to calculate GCD of two numbers.
    #include<stdio.h>
    #include<conio.h>
    int gcd(int a,int b);
    void main()
    {
    clrscr();
    int x,y,res;
    scanf("%d%d",&x,&y);

    res=gcd(x,y);

    printf("%d",res);

    getch();

    }
    int gcd(int a,int b)
    {
    int rem;
        while(b>0)
        {
        rem=a%b;
        a=b;
        b=rem;
          }
    return a;
    }
  • Any year is entered through the keyboard.Write a function to determine whether the year is a leap year or not.
    #include<stdio.h>
    #include<conio.h>
    int year(int y);
    void main()
    {
    clrscr();
    int y,res;
    printf("Enter the value of year?\n");
    scanf("%d",&y);
    res=year(y) ;


    getch();

    }
    int year(int y)
    {

    if(y%400==0)

    return    printf("It is leap year");
    else if(y%100==0)
    return    printf("It is not leap year");
    else if(y%4==0)
    return    printf("It is leap year");

    else

    return    printf("It is not leap year");
    }
  • A prime integer is entered through the keyboard.Write a function  to obtain the prime factors  of this number.

    #include<stdio.h>
    #include<conio.h>

    int prime (int x);

    void main()
    {
    clrscr();
    int num;
    printf("Enter The Number:");
    scanf("%d",&num);
    int p,i;
    p=num;
    for(i=2;i<=(num/2);i++)
    {  if(prime(i))
       while(p%i==0)
         { printf("%d ",i);
           p=p/i;
         }

    }

    getch();
    }

    int prime (int x)

    {  int i=2;
       while(i<=(x/2))
         { if(x%i==0)
           break;
           ++i;
         }
       if (i>(x/2))
       return 1;
       else
       return 0;

    }
  • Write a function which receives 5 integers and returns the sum, average and standard deviation of these numbers. Call this function from main() and print the results in main().
    #include<stdio.h>
    #include<conio.h>
    #include<math.h>
    int sum(int p,int q,int r,int s,int t);
    float average(float o);
    float sd(float m,float n);

    void main()

    {
    clrscr();
    int a,b,c,d,e,x;
    float y,z;
    printf("Enter the value of a:");
    scanf("%d",&a);
    printf("Enter the value of b:");
    scanf("%d",&b);
    printf("Enter the value of c:");
    scanf("%d",&c);
    printf("Enter the value of d:");
    scanf("%d",&d);
    printf("Enter the value of e:");
    scanf("%d",&e);
    x=sum(a,b,c,d,e);
    printf("\nSUM=%d",x);
    y=average(x);
    printf("\nAVERAGE=%f",y);
    z=sd(x,y);
    printf("\nSTANDARD DEVIATION=%f",z);

    getch();

    }
    int sum(int p,int q,int r,int s,int t)
    {
        return p+q+r+s+t;
    }
    float average(float o)
    {
        return o/5;
    }
    float sd(float m,float n)
    {
        return (sqrt(m-n));
    }
  • A 5 digit positive integer is entered through the keyboard write a function to calculate sum of digits of the digit number.
    Using recursion:

    #include<stdio.h>
    #include<conio.h>
    int digitsum(int n);

    void main()
    {
      clrscr();
      int x;
      printf("Enter The Number:");
      scanf("%d",&x);
      printf("The Sum Is:%d",digitsum(x));
      getch();
    }

    int digitsum(int n)

    {
      int rem,sum;
      if(n==0)
         return 0;
      rem=n%10;
      n=n/10;
      sum=rem+digitsum(n);
      return sum;
    }


    Without using recursion:
    #include<stdio.h>
    #include<conio.h>
    int digitsum(int n);

    void main()

    {
      clrscr();
      int x;
      printf("Enter The Number:");
      scanf("%d",&x);
      printf("The Sum Is:%d",digitsum(x));
      getch();
    }

    int digitsum(int n)


    {

      int rem,sum=0,i;
      for (i=1;i<=5;i++)
      { rem=n%10;
        n=n/10;
        sum=sum+rem; }
      return sum;
    }
  • A positive integer is entered through the keyboard, write a program  to obtain the prime factors recursively.
    #include<stdio.h>
    #include<conio.h>
    primefct(int x);
    void main()
    {
    clrscr();
    int x;
    printf("Enter an integer:");
    scanf("%d",&x);
    printf("the prime factors are:");
    primefct(x);
    getch();
    }

    primefct(int n)

    {
    int i;
    for(i=2;i<=n;i++)
    {
     if(n%i==0)
        {
        printf("%d ",i);
        primefct(n/i);
        break;
        }
    }
    }
  • Write a recursive function to obtain the first 25 numbers of a Fibonacci  sequence.In a Fibonacci sequences the sum of two successive terms given the third term. Following are the first few term of the Fibonacci sequence:
    1         1          2           3           5           8          13           21           34          55.........

    #include<stdio.h>
    #include<conio.h>

    long int fib(long int m);

    void main()
    {
    clrscr();
    long int i;
    for(i=1;i<=25;i++)
       printf("\n%ld\n",fib(i));
    getch();

    }

    long int fib(long int m)
    {
    if(m<=2)
       return 1;
    else
       return fib(m-1)+fib(m-2);

    }




Array C programming

  • Write a program to search for an element from an array input from the user.

    #include<stdio.h>
    #include<conio.h>

    void main()

    {
    clrscr();
    int num[5]={2,4,6,8,10},i,a;
    printf("Enter an element:");
    scanf("%d",&a);
         for(i=0;i<5;i++)
    {
         if(num[i]==a)
         {
         printf("\nFound");
         break;
         }
    }
         if(i==5)
        printf("\nNot Found");
     getch();
     }
  • Inserting an element into a position of an array.The element and the insertion point are inputs form the user.

    #include<stdio.h>
    #include<conio.h>

    void main()

    {
    clrscr();
    int a[6]={10,2,8,5,9},p,e,i,j;
    printf("Enter the inserting point?\n");
    scanf("%d",&p);
    printf("Enter the element?\n");
    scanf("%d",&e);

    for(i=5;i>=p;i--)

        {
        j=i-1;
        a[i]=a[j];
        }
    a[p]=e;

    printf("The array is:\n");

    for(i=0;i<6;i++)
        printf(" %d,",a[i]);

    getch();

    }
  • Inserting a number /character into the proper position of a array which is sorted in ascending /descending order.
    Ascending
    #include<stdio.h>
    #include<conio.h>
    void main()
    {
    clrscr();
    int a[5]={1,2,3,4,5},i,j,temp;
    for(i=0;i<5;i++)

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

    for(i=0;i<5-1;i++)
           for(j=i+1;j<5;j++)

               if(a[i]<a[j])
             {    temp=a[i];
            a[i]=a[j];
            a[j]=temp;
            }
             for(i=0;i<5;i++)
        printf("\n%d",a[i]);


        getch();
        }

    Descending

    #include<stdio.h>
    #include<conio.h>
    void main()
    {
    clrscr();
    int a[5]={1,2,3,4,5},i,j,temp;
    for(i=0;i<5;i++)

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


           for(i=0;i<5-1;i++)
           for(j=i+1;j<5;j++)

               if(a[i]>a[j])
             {    temp=a[i];
            a[i]=a[j];
            a[j]=temp;
            }


      for(i=0;i<5;i++)
        printf("\n%d",a[i]);


        getch();
        }

  • Deleting an element from an array.

    #include<stdio.h>
    #include<conio.h>
    void main()
    {
    clrscr();
    int a[7]={2,4,8,10,12,15},i,j;
    printf("Deleting 8 from this array\n\n");
    for(i=2;i<6;i++)
        {
        j=i+1;
        a[i]=a[j];
        }

    printf("The array is ");
    for(i=0;i<5;i++)
        printf("%d,",a[i]);
    getch();
    }
  • Write a program to find out the maximum, minimum and mode of an array of numbers.

    Maximum & Minimum

    #include<stdio.h>
    #include<conio.h>

    void printarray(int ar[]);

    void main()
    {

    clrscr();

    int num[10]={23,33,6,4,54,28,24,6,34,2};

    int i,k,temp,a;

    printarray(num);

    a=10/2;
    a=(num[a]+num[a-1])/2;
    printf("\nMEDIAN of this array is %d\n",a);

    for(i=0;i<9;i++)
        for(k=i+1;k<10;k++)
        {
         if(num[i]<num[k])
            {
             temp=num[i];
             num[i]=num[k];
             num[k]=temp;
            }

        }

    printf("\nMaximum number is %d\n",num[0]);
    printf("\nMinimum number is %d",num[9]);

    getch();
    }

    void printarray(int ar[])
    {

     int i;
     for(i=0;i<10;i++)
        printf("%d ",ar[i]);

     printf("\n\n");

    }


    Mode
    #include<stdio.h>
    #include<conio.h>
    void main()
    {
    clrscr();
    int a[6]={2,2,3,5,5,6},i,j;
    printf("Mode of this array is\n");
    for(i=0;i<6;i++)
        for(j=i+1;j<6;j++)
        {
             if(a[i]==a[j])
               {
            printf(" %d,",a[i]);

               }
        }

    getch();
    }
  • Find k-th maximum and k-th minimum from an array.

    #include<stdio.h>
    #include<conio.h>
    void printarray(int ar[]);
    void main()
    {

    clrscr();

    int num[10]={23,10,3,4,54,28,30,6,34,2};

    int i,k,n,l,temp;

    printarray(num);

    for(i=0;i<9;i++)
        for(k=i+1;k<10;k++)
        {
         if(num[i]<num[k])
            {
             temp=num[i];
             num[i]=num[k];
             num[k]=temp;
            }

        }
    printarray(num);

    printf("Enter the value of K?\n");
    scanf("%d",&n);
    l=n-1;
    printf("\n%dth Maximum number is %d\n",n,num[l]);

    printf("\n\n");

    for(i=0;i<9;i++)
        for(k=i+1;k<10;k++)
        {
         if(num[i]>num[k])
            {
             temp=num[i];
             num[i]=num[k];
             num[k]=temp;
            }

        }
    printarray(num);

    printf("\n%dth Minimum number is %d",n,num[l]);

    getch();
    }

    void printarray(int ar[])
    {

     int i;
     for(i=0;i<10;i++)
        printf("%d ",ar[i]);

     printf("\n\n");
    }
  • Write  a program to delete duplicate elements from an array.

    #include<stdio.h>
    #include<conio.h>

    void main()
    {
    clrscr();
    int a[7]={2,4,6,6,8,8,10},j,i,k,l;

    printf("\nOriginal array is\n ");
    for(i=0;i<7;i++)
        printf(" %d,",a[i]);

    for(i=0;i<7;i++)
        for(j=i+1;j<7;j++)
        {
        if(a[i]==a[j])
            {
            a[i]=NULL;
            for(k=i;k<7;k++)
              {
              l=k+1;
              a[k]=a[l];
              }
            }
        }
    printf("\nAfter deleting duplicate element,the array is\n ");
    for(i=0;i<5;i++)
        printf(" %d,",a[i]);
    getch();
    }
  • Write a program to find the common characters from two arrays.

    #include<stdio.h>
    #include<conio.h>
    #include<string.h>

    void main()
    {
    clrscr();
    char str1[10],str2[10];
    int i,len,j,len1;

    gets(str1);
    gets(str2);

    len=strlen(str1);
    len1=strlen(str2);

    for(i=0;i<len;i++)
        for(j=0;j<len1;j++)
        {
        if(str1[i]==str2[j])
           {
           printf("%c",str1[i]);
           break;
           }
        }
    getch();
    }
  • Take a string as input and print characters in reverse order.Don't use any built in string function.

    #include<stdio.h>
    #include<conio.h>
    #include<string.h>


    void main()
    {
    clrscr();
    int i, k , len;

    char str[100];
    char strrv[100];


    gets(str);

    len=strlen(str);

    k=0;

    for(i=len-1;i>=0;i--)
        {
        strrv[k]=str[i];
        k++;
        }


    strrv[len]=NULL;

    printf("Original String :%s\n",str);
    printf("Rerverse string :%s\n",strrv);

    getch();

    }
  • Write a program to merge two arrays removing the duplicate elements.


    #include<stdio.h>
    #include<conio.h>

    void main()
    {
    clrscr();
    int arr1[7]={2,4,8,9},arr2[4]={4,8,10,12},i,j,l,k,a;
    for(i=0;i<4;i++)
        for(j=0;j<4;j++)
        {
        if(arr1[i]==arr2[j])
           {
            arr2[j]=NULL;
            for(k=j;k<3;k++)
            {
            l=k+1;
            arr2[k]=arr2[l];
            }
            break;
           }
        }

    a=4;
    for(i=0;i<2;i++)
        {
        arr1[a]=arr2[i];
        a=a+1;
        }
    for(i=0;i<6;i++)
    printf(" %d,",arr1[i]);

    getch();
    }
  • Take a string as input and check whether it is  a palindrome.

    #include<stdio.h>
    #include<conio.h>
    #include<string.h>

    void main()
    {
    clrscr();
    int i,k,len;

    char arr1[100];
    char arr2[100];

    gets(arr1);

    len=strlen(arr1);

    k=0;

    for(i=len-1;i>=0;i--)
        {
        arr2[k]=arr1[i];
        k++;
        }

    arr2[len]=NULL;

    if(!strcmp(arr1,arr2))
        printf("\n PALINDROME");
    else
        printf("\n NOT PALINDROME");


    getch();

    }
  • Write  a program to merge two sorted arrays.


    #include<stdio.h>
    #include<conio.h>
    void printary(int arr[]);
    void main()
    {
    clrscr();
    int a[5]={2,5,1,7,9},b[5]={3,6,8,4,10},i,num,c,j;

    printary(a);

    printary(b);
    c=5;
     for(i=0;i<5;i++)

        {
           a[c]=b[i];
           c=c+1;
        }
      printf("After merge the main array is:");
     for(i=0;i<10;i++)

      printf(" %d",a[i]);
        getch();
      }
    void printary(int arr[])
    {
    int j,i,a,temp;
    for(i=0;i<5;i++)
         for(i=0;i<5-1;i++)
           for(j=i+1;j<5;j++)

               if(arr[i]>arr[j])
             {    temp=arr[i];
            arr[i]=arr[j];
            arr[j]=temp;
            }
    for(i=0;i<5;i++)
        printf(" %d",arr[i]);

        print f("\n\n");
    }
  • Write a program to count the frequencies of each character present in a text.

    #include<stdio.h>
    #include<conio.h>
    #include<string.h>
    void sort(char a[]);

    int len,k;
    void main()
    {
    clrscr();
    char a[100];
    int i,j,count=0,count1=0,count2=0;

    gets(a);

    printf("\n");
    len=strlen(a);
    for(i=0;i<len;i++)
        if(a[i]==' ')
        {
        count++;
        k=i;
        sort(a);
        i=i-1;
        }
    if(count>0)
        printf("space= %d\n\n",count);

    count=0;
    for(i=0;i<len;i++)
        if(a[i]=='.')
        {
        count++;
        k=i;
        sort(a);
        i=i-1;
        }

    if(count>0)
        printf(".= %d\n\n",count);

    for(i=0;i<len;i++)
        if(a[i]=='    ')
        {
        count2++;
        k=i;
        sort(a);
        i=i-1;
        }

    if(count2>0)
        printf("tab= %d\n\n",count2);


    for(i=0;i<len;i++)
        if(a[i]==',')
            {
            count1++;
            k=i;
            sort(a);
            i=i-1;
            }

    if(count1>0)
        printf(",= %d\n\n",count1);

    for(i=0;i<len;i++)
    {
    count=1;
        for(j=i+1;j<len;j++)
         if(a[i]==a[j])
         {
         count++;
         k=j;
         sort(a);
         len=strlen(a);
         j=j-1;
         }

    if(count>1)
    printf("%c= %d\n",a[i],count);

    }

    getch();
    }
    void sort(char a[])
    {
    int i,j;
    for(i=k;i<len;i++)
        {
        j=i+1;
        a[i]=a[j];
        }
    len=strlen(a);
    }
  • Write a program to count the number of letters and words within a text.

    #include<stdio.h>
    #include<conio.h>
    #include<string.h>

    void main()
    {
    clrscr();
    char str[100];
    int i,len,count=0,s;
    gets(str);
    len=strlen(str);
    for(i=len-1;i>=0;i--)
        if(str[i]==' ')
        count++;
    s=count+1;
    printf("The number of words is:%d",s);
    printf("\nThe number of letters is:%d",len-count);
    getch();
    }
  • Write a program which will search for a substring withing a string.

    #include<stdio.h>
    #include<conio.h>
    #include<string.h>
    void main()
    {
    clrscr();
    char str[100],substr[100];
    int i,k,len,len1,count=0;
    gets(str);
    printf("\n");
    gets(substr);
       len=strlen(str);
       len1=strlen(substr);
    for(i=0;i<len;i++)
     for(k=0;k<len1;k++)
     {
      if(str[i]==substr[k])
      count++;
      }
    printf("\n");
    if(count==len1)
        printf("Found");
    else
        printf("Not Found");

    getch();
    }
  • Take a  n numbers as input from the user .Find out their GCD and LCM.
    GCD

    #include<stdio.h>
    #include<conio.h>
    void main()
    {
    clrscr();
    int a[100],i,rem,n,k,j;
    printf("How many number of GCD:");
    scanf("%d",&n);
    printf("Print %d numbers:\n",n);
    for(k=0;k<n;k++)
    scanf("%d",&a[k]);
    {
    for(i=0;i<n;i++)


       while(a[i+1]>0)
        {
         rem=a[0]%a[i+1];
         a[0]=a[i+1];
         a[i+1]=rem;
         }


      }
      printf("The GCD is:%d",a[0]);
    getch();
    }

    LCM
    #include<stdio.h>
    #include<conio.h>

    void main()
    {
    clrscr();
    int arr[100],a,b,n,i,rem;
    printf("How many number of LCM:");
    scanf("%d",&n);
    printf("Enter %d numbers:\n",n);
    for(i=0;i<n;i++)
        scanf("%d",&arr[i]);
    for(i=0;i<n-1;i++)
    {
        a=arr[i];
        b=arr[i+1];
        while(b>0)
        {
        rem=a%b;
        a=b;
        b=rem;
        }
        arr[i+1]=a*arr[i]/a*arr[i+1]/a;
    }
    printf("The LCM is :");
    printf("%d",arr[n-1]);
    getch();
    }
  • Write a program to Add/Subtract two different matrices, input from the user.

    ADD

    #include<stdio.h>
    #include<conio.h>
    void mat(int arr[3][3]);
    void main()
    {
    clrscr();
    int m1[3][3],m2[3][3],m3[3][3],i,j;
    printf("Enter Matrix A:\n");
    mat(m1);
    printf("Enter Matrix B:\n");
    mat(m2);
    {
    for(i=0;i<3;i++)
    {
        for(j=0;j<3;j++)

    m3[i][j]=m1[i][j]+m2[i][j];
    }
     }
     printf("After adding two matrix(A+B):\n\n");

    for(i=0;i<3;i++)
    {
        for(j=0;j<3;j++)

         printf("  %d",m3[i][j]);
         printf(" \n");
     }
      getch();
      }
      void mat(int arr[3][3])
    {
    int r,c,a;
      for(r=0;r<3;r++)
    {
          for(c=0;c<3;c++)
          scanf("%d",&arr[r][c]);
     }
    for(r=0;r<3;r++)
    {
          for(c=0;c<3;c++)
          printf("  %d",arr[r][c]);
          printf("\n\n");
      }
    }


    Subtract

    #include<stdio.h>
    #include<conio.h>
    void mat(int arr[3][3]);
    void main()
    {
    clrscr();
    int m1[3][3],m2[3][3],m3[3][3],i,j;
    printf("Enter Matrix A:\n");
    mat(m1);
    printf("Enter Matrix B:\n");
    mat(m2);
    {
    for(i=0;i<3;i++)
    {
        for(j=0;j<3;j++)

    m3[i][j]=m1[i][j]-m2[i][j];
    }
     }
     printf("After subtracting two matrix(A-B):\n\n");

    for(i=0;i<3;i++)
    {
        for(j=0;j<3;j++)

         printf("  %d",m3[i][j]);
         printf(" \n");
     }
      getch();
      }
      void mat(int arr[3][3])
    {
    int r,c,a;
      for(r=0;r<3;r++)
    {
          for(c=0;c<3;c++)
          scanf("%d",&arr[r][c]);
     }
    for(r=0;r<3;r++)
    {
          for(c=0;c<3;c++)
          printf("  %d",arr[r][c]);
          printf("\n\n");
      }
    }
  • Write a program to Multiply two matrices.

    #include<stdio.h>
    #include<conio.h>
    void mat(int arr[3][3]);
    void main()
    {
    clrscr();
    int m1[3][3],m2[3][3],m3[3][3],i,j,n,p;
    printf("Enter Matrix A:\n");
    mat(m1);
    printf("Enter Matrix B:\n");
    mat(m2);
     printf("After multiplying two matrix(A*B):\n\n");

    for(i=0;i<3;i++)
       {
        for(j=0;j<3;j++)
        {
        n=0;
        for(p=0;p<3;p++)
        n=n+m1[i][p]*m2[p][j];
        m3[i][j]=n;

          printf("  %d",m3[i][j]);
         }
         printf(" \n");
     }
      getch();
      }
      void mat(int arr[3][3])
    {
    int r,c,a;
      for(r=0;r<3;r++)
    {
          for(c=0;c<3;c++)
          scanf("%d",&arr[r][c]);
     }
    for(r=0;r<3;r++)
    {
          for(c=0;c<3;c++)
          printf("  %d",arr[r][c]);
          printf("\n\n");
      }
    }
  • Write a program to find out the Transpose of a Matrix.


    #include<stdio.h>
    #include<conio.h>
    void main()
    {
    clrscr();
    int m[3][3],i,j;
    for(i=0;i<3;i++)
    {
    for(j=0;j<3;j++)
      scanf("%d",&m[i][j]);
    }

    printf("The original matrix is:\n");

    for(i=0;i<3;i++)
    {
    for(j=0;j<3;j++)
      printf("  %d",m[i][j]);
      printf(" \n\n");
    }
    printf("After transpose, the matrix is:\n");

    for(i=0;i<3;i++)
    {
    for(j=0;j<3;j++)
      printf("  %d",m[j][i]);
      printf(" \n\n");
    }


    getch();
    }
  • Write a program to construct a  nxn magic square.


    #include<stdio.h>
    #include<conio.h>
    int matrix[100][100];
    void main()
    {
    clrscr();
    int r,c,start,num,n;
    printf("Please Enter the number of order:");
    scanf("%d",&n);
    start=n/2;
    num=2;
    matrix[0][start]=1;
    r=0;
    c=start;
    while(1)
    {
    r=r-1;
    c=c+1;
    if(r<0&&c<n)
    {
    r=n-1;
    matrix[r][c]=num;
    }
    else if(r>=0&&r<n&&c<n&&matrix[r][c]==0)
    {
    matrix[r][c]=num;
    }
    else if(r>=0&&r<n&&c>=n)
    {
    c=0;
    matrix[r][c]=num;
    }
    else if(r<0&&c>=n)
    {
    r=r+2;
    c=c-1;
    matrix[r][c]=num;
    }
    else if(r>=0&&r<n&&c>=0&&c<n&&matrix[r][c]!=0)
    {
    r=r+2;
    c=c-1;
    matrix[r][c]=num;
    }
    if(n*n==num)
    break;
    num++;
    }
    for(r=0;r<n;++r)
    {
    for(c=0;c<n;++c)
    {
    printf("%d  ",matrix[r][c]);
    }
    printf("\n\n");
    }
    getch();
    }




Looping C programming

  • x and n are input through keyboard.Write a program to compute xn, n!, nCr, nPr

    xn
    #include<stdio.h>
    #include<conio.h>
    void main()
    {
    clrscr();
    int x,n,res=1;
    int i;
    printf("Enter the value of x?\n");
    scanf("%d",&x);
    printf("Enter the value of n?\n");
    scanf("%d",&n);
    for(i=0;i<n;i++)

    res=res*x;
    printf("Answer:%d",res);

    getch();
    }

    n!
    #include<stdio.h>
    #include<conio.h>

    void main()
    {
    int n,i,
    res=1;
    clrscr();
    printf("Enter the n=");
    scanf("%d",&n);
    for(i=n;i>=1;i--)
       res=res*i;
    printf("n!=%d",res);
    getch();
    }
    nCr
    #include<stdio.h>
    #include<conio.h>
    void main()
    {
    clrscr();
    long int n,r,sum=1,i,k,p,res=1,x,rem=1,f,ncr;
    printf("Enter the value of n:");
    scanf("%ld",&n);
    printf("Enter the value of r:");
    scanf("%ld",&r);
    p=n-r;
    for(i=n;i>=1;i--)
    {
    sum=sum*i;
    }
    for(k=p;k>=1;k--)
    {
    res=res*k;
    }
    for(x=r;x>=1;x--)
    {
    rem=rem*x;
    }
    f=res*rem;
    ncr=sum/f;
    printf("\nncr=%ld",ncr);
    getch();
    }
    nPr
    #include<stdio.h>
    #include<conio.h>
    void main()
    {
    clrscr();
    long int n,r,sum=1,i,k,p,res=1,l;
    printf("Enter the value of n:");
    scanf("%ld",&n);
    printf("Enter the value of r:");
    scanf("%ld",&r);
    p=n-r;
    for(i=n;i>=1;i--)

    sum=sum*i;


    for(k=p;k>=1;k--)
    {
    res=res*k;
    }
    l=sum/res;
    printf("\nnpr=%ld",l);
    getch();
    }
  • Write a program to determine the GCD and LCM of 3 numbers.
    GCD#include<stdio.h>
    #include<conio.h>
    void main()
    {
    clrscr();
    int a,b,c,rem;
    printf("Enter the value of a?\n");
    scanf("%d",&a);
    printf("Enter the value of b?\n");
    scanf("%d",&b);
    printf("Enter the value of c?\n");
    scanf("%d",&c);
    while(b>0)
        {
        rem=a%b;
        a=b;
        b=rem;
        }
    while(c>0)
        {
        rem=a%c;
        a=c;
        c=rem;
        }
    printf("GCD is:%d",a);
    getch();
    }
    LCM
    #include<stdio.h>
    #include<conio.h>
    void main()
    {
    clrscr();
    int a,b,c,i;
    printf("Enter the value of a?\n");
    scanf("%d",&a);
    printf("Enter the value of b?\n");
    scanf("%d",&b);
    printf("Enter the value of c?\n");
    scanf("%d",&c);
    for(i=1; ;i++)
    {
    if(i%a==0&&i%b==0&&i%c==0)
    break;
    }
    printf("LCM is:\n%d",i);
    getch();
    }
  • Find out the sum of each of the following series. n is the input from user for series (4)
    1. 3+11+19+.........+1691.
    2. 7+20+33+.........(up to 100th term)
    3. 5-11+17-..........(up to 75th term)
    4. 2*7*12*...........*37



     1.
    #include<stdio.h>
    #include<conio.h>
    void main()
    {
    clrscr();
    long int x=3,sum=0;
    for(x=3;x<=1691;x=x+8)
    {
    sum=sum+x;

    }
    printf("\n\nSUM:%ld",sum);
    getch();
    }
    2.
    #include<stdio.h>
    #include<conio.h>
    void main()
    {
    clrscr();
    long int x=7,sum=0,i;
    for(i=1;i<=100;i++)
    {
    sum=sum+x;
    x=x+13;
    }
    printf("\n\nSUM:%ld",sum);
    getch();
    }
    3.
    #include<stdio.h>
    #include<conio.h>
    void main()
    {
    clrscr();
    long int x=5,sum=0,i;
    for(i=1;i<=75;i++)
    {
    sum=sum+x;
    if(x>0)
        x=(x+6)*-1;
    else
        x=(x-6)*-1;
    }
    printf("\n\nSUM:%ld",sum);
    getch();
    }.
    4.
    #include<stdio.h>
    #include<conio.h>
    void main()
    {
    clrscr();
    long int x=2,sum=1;
    for(x=2;x<=37;x=x+5)
    {
    sum=sum*x;

    }
    printf("\n\nSUM:%ld",sum);
    getch();
    }
  • Write  a program to determine all prime numbers within the range [a.......b]
    where a & b are input through keyboard.


    #include<stdio.h>
    #include<conio.h>
    void main()
    {
    clrscr();
    int num,num1,i,n,l;
    scanf("%d",&num);
    scanf("%d",&num1);
    n=2;
    l=num;
    for(i=l;i<=num1;i++)
    {
    while(n<=i/2)
        {
        if(i%n==0)
            break;
        ++n;
        }
         if(n>i/2 && i!=2)
            printf(" %d",i);
        n=2;
        }
        getch();
        }
  • Construct the following table.Here n is input from the user.
                         1             2              3          .....    n
                         2             4              6         .......   2n
                         3             6              9         .......   3n
                          .             .               .           .....     .
                          .             .               .          .......    .
                          .             .               .           .......   .
                          n           2n            3n          .......  nn

    #include<stdio.h>
    #include<conio.h>

    void main()
        {
        clrscr();

        int i,n,k;
        scanf("%d",&n);

        for(i=1;i<=n;i++)
        {
             for(k=1;k<=n;k++)

            printf("\t%d",k*i);


                printf("\n");
        }
          getch();
        }
  • Write a program to find out first n perfect number where n is the input from user.

    #include<stdio.h>
    #include<conio.h>

    void main()
    {
    clrscr();
    int i,num,sum=0;
    printf("Enter a num");
    scanf("%d",&num);
    for(i=1;i<=num/2;i++)
       if(num%i==0)
       sum=sum+i;
       if(sum==num)
       printf("The number is a perfect number");
       else
        printf("The number is not a perfect number");
       getch();
      }
  • Write a program to find first n Fibonacci number where n is the input from user.

    #include<stdio.h>
    #include<conio.h>
    void main()
    {
    clrscr();
    int n,a,b,sum,i,rem;
    a=0;
    b=1;
    printf("Enter the value of n?\n");
    scanf("%d",&n);
    printf("The fibonacci numbers are:");
    for(i=1;i<=n;i++)
        {
        sum=a+b;
        rem=sum;
        a=b;
        b=rem;
        printf("\n%d\n",sum);
        }
    getch();
    }
  • Write a program to show the following triangle/rectangle of  "*" or numbers.Take n as inpute from user to determine the number of rows of the structure.(eg. n=5)
    1.
    #include<stdio.h>
    #include<conio.h>
    void main()                                                                                   {                                                                                        clrscr();                                                                          
                                                                                   
        int i,k,n;                                                             
        scanf("%d",&n);                                                       



        int c=1;                                                                   
        int s=n-1;                                                          

        for(i=1;i<=n;i++)
        {

           
            for(k=1;k<=s;k++)
            printf(" ");
            s--;

            for(k=1;k<=c;k++)
            printf("*");

            c=c+2;
            printf("\n");
        }

        getch();
        }

    2.
    #include<stdio.h>
    #include<conio.h>

    void main()
        {
        clrscr();

        int i,k,n;
        scanf("%d",&n);
        int c=1;
        int s=n-1;
                                                                                              
        for(i=1;i<=n;i++)                                                  
        {                                                                      
            for(k=1;k<=s;k++)                                      
            printf(" ");
            s--;                                                               

            for(k=1;k<=c;k++)

           if(k==1||k==c)
               printf("*");
           else if(i==n)
               printf("*");
            else
            printf(" ");

            c=c+2;

            printf("\n");
          }
        getch();
        }
    4.
    #include<stdio.h>
    #include<conio.h>

    void main()
        {
        clrscr();                                                                   
                                                                                
        int i,k,n;                                                            
        scanf("%d",&n);                                             
                                                                                 


        int c=1;                                                            
        int s=n-1;  

        for(i=1;i<=n;i++)
        {
            for(k=1;k<=s;k++)                                        
            printf("*");


            for(k=1;k<=c;k++)
            printf("*");

            c=c;
                printf("\n");
        }

        getch();
        }
    5.
    #include<stdio.h>
    #include<conio.h>
    void main()
    {
    clrscr();
    int i,n,k;
    scanf("%d",&n);
    for(i=1;i<=n;i++)                                                            
    {
     for(k=1;k<=n;k++)
    { if(k==1||k==n)
        printf("*");
     else if(i==1||i==n)
        printf("*");
     else
        printf(" ");
           }
        printf("\n");
     }

    getch();
    }

    7.
    #include<stdio.h>
    #include<conio.h>

    void main()
        {
        clrscr();

        int i,k,n;
        scanf("%d",&n);
        int c=1;                                                                                                                      
        int s=n-1;

        for(i=1;i<=n;i++)
        {

            for(k=1;k<=s;k++)
            printf(" ");
            s--;

            for(k=1;k<=c;k++)
            printf("*");

            c=c+2;
            printf("\n");
        }
        c=c-4;
        s=1;
        for(i=1;i<=n;i++)
        {

            for(k=1;k<=s;k++)
            printf(" ");
            s++;

            for(k=1;k<=c;k++)
            printf("*");

            c=c-2;
            printf("\n");
        }

        getch();
        }

    8.
    #include<stdio.h>
    #include<conio.h>

    void main()
        {
        clrscr();

        int i,k,n;
        scanf("%d",&n);
        int c=1;
        int s=n-1;                                                                          
         for(i=1;i<=n;i++)                                                                                   
        { 
            for(k=1;k<=s;k++)
            printf(" ");
            s--;

            for(k=1;k<=c;k++)
            printf("*");

            c=c+2;
            printf("\n");
        }
        c=c-4;
        s=1;
        for(i=1;i<=n;i++)
        {

            for(k=1;k<=s;k++)
            printf(" ");
            s++;

            for(k=1;k<=c;k++)
            printf("*");

            c=c-2;
            printf("\n");
        }
      getch();
        }

    10.
    #include<stdio.h>
    #include<conio.h>
    void main()
        {
        clrscr();

        int i,k,n;
        scanf("%d",&n);



        int c=(2*n)-1;
        int s=0;


        for(i=1;i<=n;i++)
        {
                                                                                             
         for(k=1;k<=s;k++)
            printf(" ");
            s++;

            for(k=1;k<=c;k++)
            printf("*");

            c=c-2;
            printf("\n");
        }
        c=3;
        s=n-2;
        n=n-1;
        for(i=1;i<=n;i++)
        {

            for(k=1;k<=s;k++)
            printf(" ");
            s--;

            for(k=1;k<=c;k++)
            printf("*");

            c=c+2;
            printf("\n");
        }

        getch();
        }
  • Write a program to print out all Armstrong numbers between 1 and 10000.

    #include<stdio.h>
    #include<conio.h>
    void main()
    {
    clrscr();
    int num,sum=0,k,rem,i;
    for(i=1;i<=10000;i++)
    {
     num=i;
     sum=0;

          for(k=1;k<=i;k++)
        {
          rem=num%10;
          sum=sum+(rem*rem*rem);
          num=num/10;
        }

         if(sum==i)
         printf("\n%d\n",i);
         }
         getch();
      }
  • Write a program to calculate how many 5 digit numbers can be created if the following terms apply:
    1. the leftmost digit is even
    2. the second digit is odd
    3. the third digit is a non even prime
    4. the fourth and fifth are two random digits not used before in the number.




      #include<stdio.h>
      #include<conio.h>

      long int prime(long int n);

      void main()
      {
      clrscr();
      long int a,b,c,d,e,i,rem,num,count=0;
      for(i=20000;i<=99999;i++)
      {
          rem=i%10;
          e=rem;
          num=i/10;
          rem=num%10;
          d=rem;
          num=num/10;
          rem=num%10;
          c=rem;
          num=num/10;
          rem=num%10;
          b=rem;
          num=num/10;
          a=num;
          if(a%2==0&&b%2!=0&&prime(c)&&d!=a&&d!=b&&d!=c&&e!=d&&e!=a&&e!=b&&e!=c)
                 count++;

          }
      printf(" \nRESULT=%ld",count);
      getch();
      }

      long int prime (long int n)
      {       int m;
          m=2;
          while(m<=n/2)
          {
          if(n%m==0)
              break;
          ++m;
          }
          if(m>n/2&&n!=2&&n!=1&&n!=0)
              return 1;
          else
              return 0;
      }

Conditional C programming

  • Three numbers are input through keyboard.Write a program to find out the maximum and minimum of those 3 numbers.

    #include<stdio.h>
    #include<conio.h>
    void main()
    {
    clrscr();
    int a,b,c;
    printf("Enter value of a?\n");
    scanf("%d",&a);
    printf("Enter value of b?\n");
    scanf("%d",&b);
    printf("Enter value of c?\n");
    scanf("%d",&c);

    if(a>b&&a>c)
        printf("\n Max a");
    if(b>a&&b>c)
        printf("\n Max b");
    if(c>a&&c>b)
        printf("\n Max c");
    if(a<b&&a<c)
        printf("\n\n Min a");
    if(b<a&&b<c)
        printf("\n\n Min b");
    if(c<a&&c<b)
        printf("\n\n Min c");
    getch();
    }
  • Take  a year as input and determine whether  it is leap year.

    #include<stdio.h>
    #include<conio.h>
    void main()
    {
    clrscr();
    int year;
    printf("Enter a year to check for a leap year:");

    scanf("%d",&year);

    if((year%4==0 && year%100!=0)||(year%400==0))

         {
         printf("The entered year is a leap year.");
          }
    else
        {
         printf("The entered year is not a leap year.");
         }
    getch();
         }
  • If  cost price and selling price of an  item is input  through the  keyboard, write a program to determine  whether the seller has made profit  or incurred loss.Also determine  how much profit  he made or loss he incurred.
    #include<stdio.h>
    #include<conio.h>
    void main()
    {
    clrscr();
    int x,y,sum;
    printf("Enter cost price x?\n");
    scanf("%d",&x);
    printf("Enter selling price y?\n");
    scanf("%d",&y);

    if(x<y)
        {
        printf("The seller has made profit");
        sum=y-x;
        printf("\nThe profit is:%d",sum);
        }
    else
        {
        printf("The seller has incurred loss");
        sum=x-y;
        printf("\nThe loss is:%d",sum);
        }
    getch();
    }
  • Any  integer  is  input  through  keyboard. Write a program to find out  whether it is an odd number  or  even number.
    #include<stdio.h>
    #include<conio.h>
    void main()
    {
    clrscr();
    int num;
    printf("Enter a num:");
    scanf("%d",&num);

    if(num%2==0)
         printf("The occured number is even.");
    else
         printf("The occured number is odd.");
    getch();
    }
  • A  5 digit number is entered through the keyboard. Write a program to obtain the reverse number and  to determine whether  the  original  numbers are  equal  or  not.
    #include<stdio.h>
    #include<conio.h>
    void main()
    {
    clrscr();
    long int num,num1,rem,numb=0;

    printf("Enter the 5 digit number ?\n");
    scanf("%ld",&num);
    rem=num%10;
    numb=numb+(10000*rem);
    num1=num/10;
    rem=num1%10;
    numb=numb+(1000*rem);
    num1=num1/10;
    rem=num1%10;
    numb=numb+(100*rem);
    num1=num1/10;
    rem=num1%10;
    numb=numb+(10*rem);
    num1=num1/10;
    numb=numb+num1;
    printf("\nThe reverse number of this number is:%ld",numb);
    numb;
    if(numb==num)
        printf("\n\nThe numbers are equal");
    else
        printf("\n\nThe numbers are not equal");
    getch();
    }
  • Aust  grading  policy  is:
    1. 80%  marks or above  is A+
    2. 75% to 79% marks is A
    3. 70% to 74% marks is A-
    4. 65% to 69% marks is B+
    5. 60% to 64% marks is B
    6. 55% to 59% marks is B-
    7. 50% to 54% marks is C+
    8. 45% to 49% marks is C
    9. 40% to 44% marks is D
    10. Below 40% is F

      Write  aprogram which will take an input from user and calculate the grade of a student according to Aust  grading policy based on that input.
       
      #include<stdio.h>
      #include<conio.h>
      void main()
      {
      clrscr();
      int x;
      printf("Enter a mark:");
      scanf("%d",&x);
      if(x>=80)
      printf("The grade is:A+");
      else if(x>=75&&x<=79)
      printf("The grade is:A");
      else if(x>=70&&x<=74)
      printf("The grade is:A-");
      else if(x>=65&&x<=69)
      printf("The grade is:B+");
      else if(x>=60&&x<=64)
      printf("The grade is:B");
      else if(x>=55&&x<=59)
      printf("The grade is:B-");
      else if(x>=50&&x<=54)
      printf("The grade is:c+");
      else if(x>=45&&x<=49)
      printf("The grade is:c");
      else if(x>=40&&x<=44)
      printf("The grade is:c");
      else if(x<40)
      printf("The grade is:F");
      getch();
      }
      • A certain grade of steel is graded according to the following conditions:
        1.Hardness must be greater than 60
        2.Certain content must be less than 0.7
        3.Tensile strength must be greater than 5000

        The grades are as follow:

        Grade is 10 if all three conditions are met
        Grade is 9 if condition 1 and 2 are met
        Grade is 8 if condition 2 and 3 are met
        Grade is 7 if condition 1 and 3 are met
        Grade is 6 if only one condition is met
        Grade is 5 if one of the conditions are met
         Write a program which will require the user   give values of hardness, carbon content and tensile  strength  of  the steel   under consideration and output   the  grade  of the steel.

        #include<stdio.h>
        #include<conio.h>
        void main()
        {
        clrscr();

        int x,z;
        float y;
        printf("Enter the value of hardness?\n");
        scanf("%d",&x);
        printf("carbon content?\n");
        scanf("%f",&y);
        printf("Enter tensile strength?\n");
        scanf("%d",&z);
        if (x>60&&y<0.7&&z>5000)
            printf("Grade is 10");
        else if(x>60&&y<0.7)
            printf("Grade is 9");
        else if(y<0.7&&z>5000)
            printf("Grade is 8");
        else if(x>60&&z>5000)
            printf("Grade is 7");
        else if(x>60||y<0.7||z>5000)
            printf("Grade is 6");
        else
            printf("Grade is 5");
        getch();
        }




Preliminary C programming

  • Write a program to print "Hellow World".

    #include<stdio.h>
    #include<conio.h>
    void main()
    {
    clrscr();
    printf("Hellow World");
    getch();
    }
  • The length and height of a rectangle and radius of a circle are input through the keyboard.Write a program to find the area & perimeter of the rectangle and circumference of the circle.
    #include<stdio.h>
    #include<conio.h>

    void main()
    {
     clrscr();
     int length,height,area,per,rad,cir;
     printf("Enter a length? \n");
     scanf("%d",&length);
     printf("Enter a height? \n");
     scanf("%d",&height);
     printf("Enter a radius?\n");
     scanf("%d",&rad);

     area=length*height;

     per=2*(length+height);
     cir=2*3.1416*rad;
     printf("The perimiter is: %d\n",per);

     printf("The area is: %d\n",area);
     printf("The circumference is:%d",cir);

     getch();
     }
  • Rahim's basic salary is inpute through the keyboard.His house rent allowarnce is 30% of his basic salary and medical allowance is 5% of basic salary.He gets extra 1000 tk as technical allowance.Write a program to calculate his gross salary and print the result.

    #include<stdio.h>
    #include<conio.h>
    void main()
    {
    clrscr();
    int b,t,g;
    float h,m;
    printf("input rahim's basic salary:\n");
    scanf("%d",&b);
    h=(b*30)/100;
    m=(b*5)/100;
    t=1000;
    g=(b)+(h)+(m)+(t);
    printf("Rahim's gross salary is:%d",g);
    getch();
    }
  • Two anumbers  are input  through the keyboard into two locations A and B. Write a program to interchange the contents of A and B.

    #include<stdio.h>
    #include<conio.h>
    void main()
    {
    clrscr();
    int a,b,temp;
    printf("Enter the value of A:");
    scanf("%d",&a);
    printf("Enter the value of B:");
    scanf("%d",&b);
    temp=a;
    a=b;
    b=temp;

    printf("\n\nAfter interchange the value of A is:%d",a);
    printf("\n\nAfter interchange the value of B is:%d",b);
    getch();
    }
  • If a 5 digit number is input  through the keyboard, Write a program to calculate  and print  the sum of its digits.

    #include<stdio.h>
    #include<conio.h>
    void main()
    {
    clrscr();
    int rem,sum=0;
    int num;
    printf("A 5 digit number is?\n");
    scanf("%d",&num);
    rem=num%10;
    sum=rem+sum;
    num=num/10;
    rem=num%10;
    sum=rem+sum;
    num=num/10;
    rem=num%10;
    sum=rem+sum;
    num=num/10;
    rem=num%10;
    sum=sum+rem;
    num=num/10;
    sum=sum+num;
    printf("\nThe sum of the digits of this number is:%d",sum);
    getch();
    }
  • If  a 5 digit number is input  through the keyboard, Write a program to reverse the number.

    #include<stdio.h>
    #include<conio.h>
    void main()
    {
    clrscr();
    long int rem,numb=0;
    long int num;
    printf("Enter the 5 digit number ?\n");
    scanf("%ld",&num);
    rem=num%10;
    numb=numb+(10000*rem);
    num=num/10;
    rem=num%10;
    numb=numb+(1000*rem);
    num=num/10;
    rem=num%10;
    numb=numb+(100*rem);
    num=num/10;
    rem=num%10;
    numb=numb+(10*rem);
    num=num/10;
    numb=numb+num;
    printf("The reverse number of this number is:%ld",numb);
    getch();
    }
  • If  a 4 digit number is input  through the keyboard, Write a program to obtain the sum of the first and last digit of this number.
    #include<stdio.h>
    #include<conio.h>
    void main()
    {
    clrscr();
    int rem,sum=0;
    int num;
    printf("A 4 digit number is?\n");
    scanf("%d",&num);
    rem=num%10;
    sum=rem+sum;
    num=num/10;
    rem=num%10;
    num=num/10;
    rem=num%10;
    num=num/10;
    rem=num%10;
    sum=sum+rem;
    printf("\nThe sum of the first and last digit of this number is:%d",sum);
    getch();
    }