Tuesday, 2 June 2015

Arithmetic operations on structures using functions

#include <stdio.h>
struct complex {
       int a;
       int b;
 }t1,t2;
void read(struct complex *);
void write(struct complex);
void add(struct complex,struct complex);
void sub(struct complex,struct complex);
void mul(struct complex,struct complex);
int main() {
    read(&t1);
    read(&t2);
    write(t1);
    write(t2);
    add(t1,t2); 
    sub(t1,t2);
    mul(t1,t2);
}
void read(struct complex *z) {
     printf("Enter the real part:");
     scanf("%d",&z->a);
     printf("Enter the imazinary part:");
     scanf("%d",&z->b);
}
void write(struct complex z) {
     printf("The complex number is :");
   if(z.b<0)
     printf("%d - i%d\n",z.a,(-z.b));
   else
       printf("%d +i%d\n",z.a,z.b);
}
void add(struct complex x,struct complex y) {
     struct complex z;
     z.a = t1.a + t2.a;
     z.b = t2.b + t2.b;
     printf("Sum of two complex numbers:");
     write(z);
}

void sub(struct complex x,struct complex y) {
     struct complex z;
     z.a = t1.a - t2.a;
     z.b = t2.b - t2.b;
     printf("Subtracting of two complex numbers:");
     write(z);
}
 void mul(struct complex x,struct complex y) {
     struct complex z;
     z.a = t1.a * t2.a - t1.b * t2.b;
     z.b = t1.a * t2.b + t1.b * t2.a;
printf("Multiplication of two complex numbers:");
     write(z);
}
 
I/O of  a program
 
Enter the real part:4
Enter the imazinary part:6
Enter the real part:4
Enter the imazinary part:7
The complex number is :4 +i6
The complex number is :4 +i7
Sum of two complex numbers:The complex number is :8 +i14
Subtracting of two complex numbers:The complex number is :0 +i0
Multiplication of two complex numbers:The complex number is :-26 +i52
 
 

Saturday, 4 April 2015

Reverse pyramid pattern program in c

#include<stdio.h>
main()
{
int i,j,k=1,m,l;
printf("Enter the height of the pyramid");
scanf("%d",&l);
m=0;
for(i=1;i<=l;i++)
{
    for(j=2*l-1;j>=i;j--)
      {
          if(k==0)
       {
 if((j%2==0) && j<=2*l-m && (j-m)>=i-m)
 printf("%c",'*');
 else
 printf(" ");
 }
           else if(k==1)
 {
 if(j%2!=0 && j<=2*l-m && (j-m)>=i-m)
 printf("%c",'*');
 else
 printf(" ");
 }
         }
printf("\n");
if(k==1)
k=0;
else
k=1;
m=m+1;
}
}

I/O of Program

Enter the height of the pyramid 7                                                                                                                                                
* * * * * * *                                                                                                                                                                   
 * * * * * *                                                                                                                                                                    
  * * * * *                                                                                                                                                                     
   * * * *                                                                                                                                                                      
    * * *                                                                                                                                                                       
     * *                                                                                                                                                                        
      *   

Thursday, 2 April 2015

Efficient C Programming Code To Create Pyramid and Pattern

//only two for loop are used to create pyramid and patterns. Time complexity of the following //program is O(n^2).

 #include<stdio.h>
main()
{
int i,j,k=1,m,l;
printf("Enter the height of the pyramid");
scanf("%d",&l);
m=l;
for(i=1;i<=l;i++)
{
    for(j=1;j<l+i;j++)
      {
          if(k==0)
      {
if((j%2==0) && j>=m && (j-m)<2*i)
printf("%c",'*');
else
printf(" ");
}
           else if(k==1)
{
if(j%2!=0 && j>=m && (j-m)<2*i)
printf("%c",'*');
else
printf(" ");
}
         }
printf("\n");
if(k==1)
k=0;
else
k=1;
m=m-1;
}
}

I/O of a program

Enter the height of the pyramid 7

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