C Language Source Codes

smother

Well-known member
  • Jan 27, 2009
    19,902
    1,483
    113
    36
    Six Feet Under
    Total of Two Subjects & their Average
    **********************************************
    # include <stdio.h>
    # include <conio.h>

    main()
    {
    int sub1,sub2,tot=0;
    float avg;

    while(sub1!=-1)
    {
    clrscr();
    printf("Enter mark 1 : ");
    scanf("%d",&sub1);
    printf("Enter mark 2 : ");
    scanf("%d",&sub2);

    tot=(sub1+sub2);
    avg=(float)tot/2;

    if(avg>=66)
    printf("\nPASS\n");
    else
    printf("\nFAIL\n");
    getch();

    }

    getch();
    return 0;
    }

    *****************************************************************

    ABCD = (AB + CD) * (AB + CD)

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

    void find (int);

    main()
    {
    int x;

    clrscr();

    for(x = 1000; x <= 9999; x++)
    find(x);

    getch();
    return 0;
    }

    void find (int y)
    {
    int tot;

    tot = (y/100) + (y%100);
    tot *= tot;

    if(tot==y)
    {
    printf("%d satisfies the condition \n",y);
    }
    }
    ****************************************************************
    Generating PRIME numbers from 1 to 1000

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

    void prime (int i);

    main()
    {
    int num;

    clrscr();
    printf("Prime numbers from 1 to 1000 \n\n");

    for(num = 2; num <= 1000; num++)
    prime(num);

    getch();
    return 0;
    }

    void prime(int xnum)
    {
    int n,rem;

    for(n=2; n <= xnum / 2; n++)
    {
    rem = xnum % n;
    if(rem == 0)
    break;
    }

    if(n>=xnum/2)
    {
    printf("%d\t", xnum);
    }
    }
    *****************************************************************
    Generating PERFECT numbers from 1 to 1000

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

    void perfect (int);

    main()
    {
    int x;

    clrscr();

    printf("Perfect numbes from 1 to 1000 are....\n");

    for(x=1; x<1000; x++)
    perfect(x);

    getch();
    return 0;
    }

    void perfect (int y)
    {
    int z, rem, halfn, tot = 0;

    halfn = y/2;
    for(z = 1; z <= halfn; z++)
    {
    rem = y % z;
    if(rem == 0)
    tot += z;
    }

    if(tot == y)
    printf("%d\t",y);
    }
    ****************************************************************
    Greatest Common Divisor (GCD)

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

    main()
    {
    int m, n, gcd, rem1, rem2;

    clrscr();

    printf("Enter first number : ");
    scanf("%d", &m);

    printf("Enter second number : ");
    scanf("%d", &n);

    if(m>=n)
    {
    gcd = n;
    }
    else
    {
    gcd = m;
    }

    rem1 = m%gcd;
    rem2 = n%gcd;

    while((rem1 != 0) || (rem2 != 0))
    {
    gcd -= 1;
    rem1 = m % gcd;
    rem2 = n % gcd;
    }

    printf("\nGCD of above numbers is %d ", gcd);

    getch();
    return 0;
    }
    ****************************************************************
    Printing Numbers in the Reverse Order of their Original Sequence

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

    # define n 10

    main()
    {
    int v[n];
    int i;

    clrscr();

    for(i = 1; i <= 10; i++)
    {
    printf("Input element %d : ",i);
    scanf("%d",&mp;v);
    }

    printf("\n\n Data in reverse order......\n");
    getch();

    for(i=10;i>=1;i--)
    {
    printf("%d \t",v);
    }

    getch();
    return 0;
    }

    ***************************************************************
    Printing a String in the Reverse Order

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

    main()
    {
    char str[80];
    int len,i;

    clrscr();

    printf("Enter you string :\n");
    gets(str);

    len = strlen(str);

    printf("\n\n");

    for(i=len; i >= 0; i--)
    printf("%c", str);

    getch();
    return 0;
    }
    ********************************************************
    Sorting and Searching Methods for Arrays

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

    # define n 10
    # define m 10

    int v[n], w[n+m], p[m];

    void input (int v[n]);
    void buble (int v[n]);
    void merge (int p[n]);
    void merges (int u[n], int p[m]);
    void direct (int v[n]);
    void sequential (int v[n]);
    void binary (int v[n]);
    void display (int v[n]);
    void quick (int v[n]);
    void quicks (int u1[n], int aleft, int aright);

    main()
    {
    int opt;

    while(opt!=9)
    {
    clrscr();
    printf("\n\t\t SELECTION MENU");
    printf("\n\t\t----------------\n");
    printf("\n\t 1. Input Data to the array v[n]");
    printf("\n\t 2. Bubble Sort");
    printf("\n\t 3. Merge Sort");
    printf("\n\t 4. Direct Insertion Sort");
    printf("\n\t 5. Quick Sort");
    printf("\n\t 6. Sequential Search");
    printf("\n\t 7. Binary Search");
    printf("\n\t 8. Display Output");
    printf("\n\t 9. Exit");
    printf("\n\nEnter option number (1-9)..... : ");
    scanf("%d",&opt);

    switch(opt)
    {
    case 1: input(v); break;
    case 2: buble(v); break;
    case 3: merge(v); break;
    case 4: direct(v); break;
    case 5: quick(v); break;
    case 6: sequential(v); break;
    case 7: binary(v); break;
    case 8: display(v); break;
    }
    }

    return 0;
    }

    // ________________________ INPUT Function _______________________________

    void input (int u[n])
    {
    int i;
    printf("\n");
    for(i=1;i<=(n+1);i++)
    {
    printf("Enter data for element %d : ",i);
    scanf("%d",&u);
    }
    }

    // ________________________ BUBLE SORT Function __________________________

    void buble (int u[n])
    {
    int i,temp,j,c=0;

    for(i=0;i<=(n-1);i++)
    {
    for(j=1;j<=(n-i);j++)
    {
    if(u[j]>=u[j+1])
    {
    temp=u[j];
    u[j]=u[j+1];
    u[j+1]=temp;
    c=1;
    }
    }
    }

    if(c!=1)
    {
    printf("\nData array is already sorted");
    }
    else
    {
    printf("\nSorted data in assending order....");
    }
    getch();
    }

    // _________________________ MERGE INPUT Function ____________________________

    void merge (int u[n])
    {
    int i;

    printf("\n\nEnter values for array p[m]\n\n");

    for(i=1;i<=n;i++)
    {
    printf("Element no %d : ", i);
    scanf("%d", &p);
    }

    printf("\n");
    buble(p);
    buble(v);
    merges(u,p);
    }

    // _________________________ MREGE SORT Function _____________________________

    void merges (int u1[n], int p1[n])
    {
    int i=1,j=1,k=1,a,b;

    while(i<=n && j<=m)
    {
    if(u1<=p1[j])
    {
    w[k] = u1;
    i++;
    }
    else
    {
    w[k] = p1[j];
    j++;
    }
    k++;
    }

    if(i==n)
    {
    for(a=j+1; a<=m; a++)
    {
    w[k] = p1[a];
    k++;
    }
    }
    else
    {
    for(a=i+1; a<=n; a++)
    {
    w[k] = u1[a];
    k++;
    }
    }

    clrscr();

    printf("Press any key for the output.......\n\n");
    getch();

    for(b=1; b<=(m+n); b++)
    {
    printf("\t%d\n",w);
    }
    getch();
    }

    // __________________________ DIRECT INSERTION SORT Function ____________

    void direct (int u[n])
    {
    int i,j,temp;

    for(i=1; i<=n; i++)
    {
    temp = u;
    j = i;

    while(j>=0 && temp<=u[j-1])
    {
    u[j] = u[j-1];
    u[j-1] = temp;
    j--;
    }
    }

    printf("\n\nSorted using Direct Insertion method.....");
    getch();
    }

    // __________________ QUICK SORT Function ____________________________

    void quick (int u[n])
    {
    int left = 1;
    int right = n;

    quicks(u,left,right);
    }

    void quicks (int u1[n], int aleft, int aright)
    {
    int i, j, temp, nleft, nright, c = 0;

    i = aleft+1;
    j = aright;

    while(i <= j)
    {
    while(u1<=u1[aleft] && i<=aright)
    {
    i++;
    }

    while(u1[j]>=u1[aleft] && j>=aleft)
    {
    j--;
    }

    if(i<=j)
    {
    temp = u1;
    u1 = u1[j];
    u1[j] = temp;
    c=1;
    }

    temp = u1[j];
    u1[j] = u1[aleft];
    u1[aleft] = temp;

    nright = j-1;
    nleft = j+1;

    if(nright>=aleft)
    {
    quicks(u1,aleft,nright);
    }

    if(nleft<=aright)
    {
    quicks(u1,nleft,aright);
    }
    }
    }

    // __________________ SEQUENTIAL SEARCH Function ___________________

    void sequential (int u[n])
    {
    int i,key,c=0;

    printf("\nEnter search key : ");
    scanf("%d",&mp;key);

    for(i=1;i<=(n+1);i++)
    {
    if(u==key)
    {
    c=1;
    break;
    }
    }

    if(c!=1)
    {
    printf("\nNo match found....\n\n");
    getch();
    }
    else
    {
    printf("\nSearch number found in array position %d\n\n",i);
    getch();
    }
    }

    // _________________________ BINARY SEARCH Function _______________________

    void binary (int u[n])
    {
    int key, mid, high, low, i=1;

    printf("\nEnter search key : ");
    scanf("%d", &key);

    high = n;
    low = i;
    mid = (high+low)/2;

    while(low<=high && u[mid]!=key)
    {
    if(u[mid]>=key)
    {
    high = mid - 1;
    }
    else
    {
    low = mid + 1;
    }

    mid = (high+low)/2;
    }

    if(key==u[mid])
    {
    printf("\nSearch key found in position %d ",low);
    }
    else
    {
    printf("\nSearch key not found ");
    }

    getch();
    }

    // _________________________ DISPLAY OUPUT Function ________________________

    void display (int u[n])
    {
    int i;

    printf("\n Display Output......\n");

    for(i = 1; i <= (n+1); i++)
    {
    printf("\n\t %d",u);
    }
    printf("\n");
    getch();
    }


    Matrix Functions

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

    # define n 2

    int u[n][n], v[n][n], w[n][n];

    void input (int u[n][n], int v[n][n]);
    void add (int u[n][n], int v[n][n]);
    void prod (int u[n][n], int v[n][n]);
    void trans (int u[n][n]);
    int print (int w[n][n]);

    main()
    {
    int opt;
    do
    {
    clrscr();
    printf("\n Playing with two 3x3 matrices....\n");
    printf("\n\t 1. Input Data");
    printf("\n\t 2. Matrix sum (W=U+V)");
    printf("\n\t 3. Matrix product (w=U*V)");
    printf("\n\t 4. Matrix transformation of U");
    printf("\n\t 5. Exit");
    printf("\n\n\t Enter the option number..... : ");
    scanf("%d", &opt);

    switch(opt)
    {
    case 1:input(u,v);break;
    case 2:add(u,v);break;
    case 3:prod(u,v);break;
    case 4:trans(u);break;
    }
    }
    while(opt!=5);

    return 0;
    }

    void input(int u1[n][n], int v1[n][n])
    {
    int i,j;
    for(i=0;i<=n;i++)
    {
    printf("\n");
    for(j=0;j<=n;j++)
    {
    printf("\t Enter data for element %d,%d in Matrix 'U' : ",i,j);
    scanf("%d", &u[j]);

    printf("\t Enter data for element %d,%d in Matrix 'V' : ",i,j);
    scanf("%d", &v[j]);
    }
    }
    }

    void add(int u1[n][n], int v1[n][n])
    {
    int i,j;
    for(i=0;i<=n;i++)
    {
    for(j=0;j<=n;j++)
    {
    w[j] = u1[j] + v1[j];
    }
    }
    print(w);
    }

    void prod(int u1[n][n], int v1[n][n])
    {
    int i,j,p,prod,tot=0;

    for(i=0;i<=n;i++)
    {
    for(j=0;j<=n;j++)
    {
    tot=0;
    for(p=0;p<=n;p++)
    {
    prod = u[j][p] * v[p][j];
    tot += prod;
    }

    w[j] = tot;
    }
    }
    print(w);
    }

    void trans(int u1[n][n])
    {
    int i,j,temp;

    for(i=0;i<=n;i++)
    {
    for(j=0;j<=n;j++)
    {
    w[j] = temp;
    temp = u[j];
    }
    }

    print(w);
    }

    int print(int w1[n][n])
    {
    int i,j;

    printf("\nThe answer is..... \n");

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

    ******!|2008.3 DCSD @ NIBM|!******
     

    kasuncs

    Well-known member
  • May 21, 2007
    3,590
    271
    83
    Example for code tag
    Code:
    /*********************************************************************
    * Cretaed by Kasun Chathuranga 2008
    *   Email:     [email protected], [email protected]
    *   Web:       http://www.kasun.tk
    *   Blog:      http://kctek.blogspot.com
    *
    **********************************************************************/
    
    #include <stdio.h>
    #include <stdlib.h>
    
    #define cpuid(in,a,b,c,d)\
    	asm("cpuid": "=a" (a), "=b" (b), "=c" (c), "=d" (d) : "a" (in));
    
    int main(int argc, char* argv[])
    {
        unsigned long maxei,eax,ebx,ecx,edx,unused;
    	int i,j;
    	char string[17];
    	cpuid(0x80000000,maxei,unused,unused,unused);
    	if(maxei >= 0x80000002)
    	{
    		string[16] = '\0';
    		printf("Your Processor is : ");
    		for(i=0x80000002;i<=0x80000004;i++)
    		{
    			cpuid(i,eax,ebx,ecx,edx);
    			for(j=0;j<4;j++)
    			{
    				string[j] = eax >> (8*j);
    				string[j+4] = ebx >> (8*j);
    				string[j+8] = ecx >> (8*j);
    				string[j+12] = edx >> (8*j);
    			}
    			printf("%s",string);
    		}
    		printf("\n");
    	}
    	system("PAUSE");
    	return 0;
    }