Posts

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 Las...

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; ...

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]);    ...

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