Posts

Showing posts from August, 2011

Pyramid Pattern no. 8 using c code

Image
Pyramid Pattern no. 8 using c code /* To print Pyramid Pattern shown below */ #include<stdio.h> int main() { int i,j,n; printf("Enter no. of lines: "); scanf( "%d",&n); //for loop for upper pyramid //for loop for number of lines for(i=1;i<=n;i++) { //for loop for spaces for(j=1;j<=n-i;j++) { printf(" "); } //for loop for stars in first half for(j=1;j<=i;j++) { printf("* "); } //for loop for stars in second half for(j=1;j<i;j++) { printf("* "); } printf("\n"); } //for loop for lower pyramid //for loop for number of lines for(i=1;i<=n-1;i++) { //for loop for spaces for(j=1;j<=i;j++) { printf(" "); } //for loop for stars in first half for(j=1;j<=n-i;j++) { printf("* "); } //for loop for stars in second half for(j=1;j<n-i;j++) { printf("* "); } printf("\n"); } return 0; }

SORT NAMES IN ALPHABETICAL ORDER USING POINTERS

SORT NAMES IN ALPHABETICAL ORDER USING POINTERS #include <stdio.h> #include <stdlib.h> #include <string.h> #define N 5 #define MAX_STRLEN 256 #define MAX_NAMELEN 128 typedef struct {   char *first;   char *last; } Name; void sort(Name *a, int n); Name *nameArray; int main(int argc, char *argv[]) {   int i;   char line[MAX_STRLEN],*p;   nameArray = (Name *)calloc(N,sizeof(Name));   printf("\nEnter %d names (First Last), one per line:\n",N);   for (i = 0; i < N; i++) {     printf("> ");     fgets(line,MAX_STRLEN,stdin);     *(strchr(line,'\n')) = '\0';     if (strlen(line) > 0) {       /*        * Could use some more logic here to verify input.        * This will work for : First Last        */       p = strchr(line,' '); *p++ = '\0';       nameArray[i].first = (char *)malloc(strlen(line));       nameArray[i].last = (char *)malloc(strlen(p));       strcpy(nameArray[i].fir

GENERATE AND PRINT FIRST N FIBONACCI NUMBERS

Image
c program to generate, print fibonacci series This program generates Fibonacci series without recursion. You can print as many number of terms of series as desired. C code #include<stdio.h> #include<conio.h>   main ( ) { int n , first = 0 , second = 1 , next , c ;   printf ( "Enter the number of terms " ) ; scanf ( "%d" ,& n ) ;   printf ( "First %d terms of fibonacci series 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 ) ; }   getch ( ) ; return 0 ; } Output:

SORT LIST OF NAMES IN ALPHABETICAL ORDER

C Program to sort the list of names of persons in alphabetical order using Bubble sort technique #include<stdio.h> #include<string.h> void main(void) { int i,k,n; char*name[50],*temp=""; printf("\nEnter number of persons:"); scanf("%d",&n); for(i=0;i<n;i++) gets(name[i]); for(k=0;k<(n-1);k++) { for(i=0;i<(n-k-1);i++) { if(strcmp(name[i],name[i+1]>0) { strcpy(temp,name[i]); strcpy(name[i],name[i+1]); strcpy(name[i+1],temp); } } } printf("\nSorted List....\n"); for(i=0;i<n;++i) puts(name[i]); }

L.C.M and G.C.D of two numbers using function

Image
How to write C Program to calculate L.C.M and G.C.D of two numbers using function #include<stdio.h> void gcd(int,int); void lcm(int,int); int main() {     int a,b;     printf("Enter two numbers: \t");     scanf("%d %d",&a,&b);     lcm(a,b);     gcd(a,b);     return 0; } //function to calculate l.c.m void lcm(int a,int b) {     int m,n;     m=a;     n=b;     while(m!=n)     {         if(m<n)             m=m+a;         else             n=n+b;     }     printf("\nL.C.M of %d  &  %d is %d",a,b,m); } //function to calculate g.c.d void gcd(int a,int b) {     int m,n;     m=a;     n=b;     while(m!=n)     {         if(m>n)             m=m-n;         else             n=n-m;     }     printf("\nG.C.D of %d  &  %d is %d",a,b,m); }

c pro gram for matrix multiplication

c program to matrix multiplication #include <stdio.h> int   main (){    int  a[5][5],b[5][5],c[5][5];    int  i,j,k,sum=0,m,n,o,p;      printf( "\nEnter the row and column of first matrix" );    scanf( "%d %d" ,&m,&n);      printf( "\nEnter the row and column of second matrix" );    scanf( "%d %d" ,&o,&p);      if (n!=o){         printf( "Matrix mutiplication is not possible" );        printf( "\nColumn of first matrix must be same as row of second matrix" );    }    else {         printf( "\nEnter the First matrix->" );        for (i=0;i<m;i++)         for (j=0;j<n;j++)               scanf( "%d" ,&a[i][j]);              printf( "\nEnter the Second matrix->" );        for (i=0;i<o;i++)         for (j=0;j<p;j++)               scanf( "%d" ,&b[i][j]);              printf( "\nThe First matrix is\n" );        for (i=0;i<m;i++){   

LOOPS IN C

Loops in C language For Loop -Most Useful Loops for ( variable initialization; condition; variable inc/dec ) {   Code to execute while the condition is true } While Loop - Simple Loops while ( condition ) { Code to be executed if the condition is true } Do While Loops - It is useful when we want to loop at least once do { } while ( condition );

C PROGRAM TO SORT LIST OF NAMES IN ALPHABETICAL ORDER

C Program to sort the list of names of persons in alphabetical order using Bubble sort technique #include<stdio.h> #include<string.h> void main(void) { int i,k,n; char*name[50],*temp=""; printf("\nEnter number of persons:"); scanf("%d",&n); for(i=0;i<n;i++) gets(name[i]); for(k=0;k<(n-1);k++) { for(i=0;i<(n-k-1);i++) { if(strcmp(name[i],name[i+1]>0) { strcpy(temp,name[i]); strcpy(name[i],name[i+1]); strcpy(name[i+1],temp); } } } printf("\nSorted List....\n"); for(i=0;i<n;++i) puts(name[i]); }

PROGRAM TO SORT ARRAY ELEMENTS (BUBBLE SORT)

C++ PROGRAM TO SORT ARRAY ELEMENTS  (BUBBLE SORT #include<conio.h> #include<iostream.h> void main() { clrscr(); int a[100],i,n,j,temp; cout<<"How many element: "; cin>>n; cout<<"Enter the elements of array: "<<endl; for(i=0;i<n;i++) cin>>a[i]; cout<<"The elements of array Before Sorting: "<<endl; //Coding by: Mohamed safeer //WWW.4CSSSM.BLOGSPOT.COM for(i=0;i<n;i++) cout<<a[i]<<" "; cout<<endl; for(i=0;i<n;i++) { for(j=0;j<n-1-i;j++) { if(a[j]>a[j+1]) { temp=a[j]; a[j]=a[j+1]; a[j+1]=temp; } } } cout<<"Elements of array After Sorting: "<<endl; for(i=0;i<n;i++) cout<<a[i]<<" "; getch(); }

program to find sum of array elements

C++ PROGRAM TO FIND SUM OF ARRAY ELEMENTS  #include<iostream.h> #include<conio.h> void main() { clrscr(); int ar[10],n,s=0; cout<<"Enter size of the array: "; cin>>n; cout<<"Enter array element: "<<endl; for(int i=0;i<n;i++) { cout<<"Enter element "<<i<<": "; cin>>ar[i]; } //Coding by: mohamedsafeer //http://4CSSSM.blogspot.com cout<<"Elements of array: "<<endl; for(i=0;i<n;i++) cout<<"Element at index number "<<i<<" is: "<<ar[i]<<endl; ///For adding all elements/// for(i=0;i<n;i++) s=s+ar[i]; cout<<"Sum of elements: "<<s; getch(); }

C++ PROGRAM TO FIND TRANSPOSE OF A MATRIX

c++ program to find transpose of matrix #include<iostreame.h> void main()      {     int m[100][100],r,c,i,j;     clrscr();    cout<<"How many ROW and COLUM";     cin>>r>>c;     cout<<"Enter the Matrix";     for(i=0;i<r;i++)     {       for(j=0;j<c;j++)       {         cin>>m[i][j];       }     }    cout<<"The transpose is";     for(i=0;i<r;i++)     {       for(j=0;j<c;j++)       {         cout<<m[j][i]);       }       cout<<"\n";     }     getch();      }