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();

        }

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  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();
    }

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 .

    #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);

    }