GURU NANAK DEV CO-ED POLYTECHNIC

GURU NANAK DEV CO-ED POLYTECHNIC
MAIN BULIDING

Thursday, 14 February 2013

Monday, 22 October 2012

Some C Programs for Electrical Engineeirng Third Semester Students

#include<stdio.h>
          main( )
          {
                    int mark;
                    char grade;
                    scanf (“%d %c”, &mark, &grade);
                    if (grade= =’A’)
                              {
                                        mark=mark+10;
                              }
                              printf(“%d”, mark);
                    }


#include<stdio.h>
          main ( )
          {
                    int mark;
                    scanf(“%d”, &mark);
                    if (mark>40)
                              printf(“pass”);
                    else
                              printf(“fail”);
          }

Write a program to find the sum of two numbers using function with no arguments and no return values.
          #include<stdio.h>
          main( )
    {
                    sum ( );
    }     
sum( )
    {
          int i, j, x;
          scanf(“%d %d”, &i, &j);
          x=i+j;
          printf(“sum=%d”, x);
          return;
     }



#include<stdio.h>
          main ( )
          {
                    int day;
                    printf(“Give a number between 1 and 7\n”);
                    scanf(“%d”, &day);
                    if (day = = 1)
                              printf(“Monday \n”);
          else if (day = =2)
                              printf(“Tuesday \n”);
          else if (day = =3)
                              printf(“Wednesday \n”);
else if (day = =4)
                              printf(“Thursday \n”);
else if (day = =5)
                              printf(“Friday \n”);
else if (day = =6)
                              printf(“Saturday \n”);
          else
                    printf(“Sunday \n”);
}

Write a program to find the square of n numbers using function with arguments
and no return values.
#include<stdio.h>
          main( )
              {
                    int i, n;
                    printf(“Enter the value of n \n”);
                    scanf(“%d, &n);
                    for(i=0; i<n, i++)
                              square(i);
     }
square (m);
int m;
    {
int  temp;
temp - m * m;
printf(“%d %d, m, temp);
return;
     }



#incude<stdio.h>
          main ( )
                    {
                              int a, b, c;
                              printf(“Enter three numbers”);
                              scanf(“%d  %d  %d”, &a, &b, &c);
                              if (a>b)
                                        {
                                        if (a>c)
first if....else
inside if block
 
                                                  printf(“big=%d”, a);
                                        else
printf(“big=%d”, c);}
else

                                        {
                                                  if(b>c)
second if....else
inside if block
 
printf(“big=%d”, b);
                                        else
printf(“big=%d”, c);
                              }
                             
          }



#include<stdio.h>
main ( )
          {
          int day;
          print(“Enter a number between 1 and 7\n”)
                    scanf(“%d”, &day);
                    switch (day)
                    {
                    case 1:
                    printf (“Monday \n”);
                    break;
case 2:
                    printf (“Tuesday \n”);
                    break;
case 3:
                    printf (“Wednesday \n”);
                    break;
case 4:
                    printf (“Thursday \n”);
                    break;
case 5:
                    printf (“Friday \n”);
                    break;
case 6:
                    printf (“Saturday \n”);
                    break;
case 7:
                    printf (“Sunday \n”);
                    break;
default:
                    printf (“Enter a correct number \n”);
                    break;
                    }
          }



Simple and compound interest calculation
                    Simple interest = ptr/100
                    Compound interest = p(l+r/100)n - p
                              p - principle amount
                              t - number of years
                              r - rate of interest
#include<stdio.h>
                    #include<math.h>
                    main( )
          {
                    float p, n, r, simple, compound;
                    printf(“Enter principal amount\n”);
                    scanf(“%f”, &p);
                    printf(“Enter the rate of interest \n”);
                    scanf(“%f”, &r);
printf(“Enter the time period in years\n”);
                    scanf(“%f”, &t);
                    simple = p * n * r / 100;
                    compound = p * pow (l + r/100, t) - p;
                    printf(“simple interest is\n\n%6.2f”, simple);
                    printf(“\n”);
          printf(“\n compound interest is\n\n%6.2f”, compound);
          }



Write a C program to calculate the area of the triangle, given its three sides
                    Area   = s(s-a)(s-b)(s-c)
                    s         = (a+b+c) / 2
          #include<stdio.h>
#include<math.h>
void main ( )
{
          float a, b, c, s, area;
          printf(“\n Enter three sides a, b, c”);
          scanf(“%f  %f %f” &a, &b, &c);
          s = (a+b+c)/2;
          area=sqrt (s * (s-a) * (s-b) * (s-c));
          printf(“\n Area of triangle is %6.2f”, area);
}

Write a C program to convert temperature in centrigrade to fahrenheit and vice
versa.
          c to f = 1.8 x centrigrade + 32     
          f to c = (fahrenheit - 32) / 1.8
#include<stdio.h>
#include<math.h>
main( )
{
float c, f, ic, fi;
printf(“Enter temperature in centrigrade \t”);
scanf(“%f”, &c);
f = 1.8 *c+32;
printf(“Fahrenheit = %f”, f);
printf(“\n Enter the temperature in fahrenheit \t”);
scanf(“%f”, &fi);
ic = (fi - 32) / 1.8;
printf(“Centigrade = %f”, ic);
          }



Write a C program to solve the quadratic equation.
          #include<stdio.h>
          #include<math.h>
          main( )
          {
                    float a, b, c, d, root, root1, root2;
                    printft(“Enter the values of a, b and c\n”);
                    scanf(%f%f%f”, &a, &b, &c);
                    d = b * b - 4.0 * a *c;
                    if (d<0)
                    printf(“ROOTS ARE IMAGINARY\t”);
          else if ( d = = 0)
              {
                    printf(“ROOTS ARE REAL AND EQUAL \n”);
          root = -b / (2.0 * a);
          printf(“THE ROOTS ARE\n root1=root2-%7.3f”, root);
     }
else
     {
          printf(“ROOTS ARE REAL\n”);
          root 1 = (-b + sqrt(d) ) / (2.0 * a);
root 2 = (-b - sqrt(d) ) / (2.0 * a);
printf(“THE ROOTS ARE \n”);
printf(“root=%7.3f\n root2=%y.3f”, root1, roots2);
      }
          }


#include<stdio.h>
          main ( )
               {
                    int i;
                    i = 0;
          while (i<5)
              {
                    printf(“GuruNanakDevPolytechnic”);
                    i++;
               }
         }



#include<stdio.h>
          main( )
              {
                    int i;
          i =1;
          do
             {
          printf(“God Loves You”);
             }
while (i<5)
      }


#include<stdio.h>
          main()
              {
                    int i, sum;
                    sum = 0;
          for (i = 1; i<=100;  i++)
          {
               sum = sum+i;
          }
printf(“Sum=%d”, sum);
                 }



Sum and average of given N numbers
#include<stdio.h>
main( )
          {
                    int count;
                    float value, average, sum;
                    sum=count=o;
          printf(“Enter values one by one, enter negative value to terminate\n\n”);
          scanf(“%f”, &value);
          while<value>0)
                    {
                              sum += value;
                              count ++;
                              scanf(“%f”, &value);
                    }
          average = sum / (float) count;
          printf(“SUM IS \n\n %f”, average);
}


To find the factorial of a given number
#include<stdio.h>
main( )
          {
                    int i,n;
                    int fact=1;
                    scanf(“%d”,&n);
                    if(n>0)
                    {
                              for(i=1;i<=n;i++)
                              fact*=i;   
                    printf(“Factorial=%d”,fact);
}
else
printf(“Negative number”);
}




Write a program to find the smallest of N numbers.
          #include<stdio.h>
          main( )
                    {
                              int i,n,a[50],small;
                    printf(“Give the value of n”);
                    scanf(“%d”, &n);
          for(i=0;i<n;i++)                                 /*i<n because
          scanf(“%d”,&a[i]);              array starts from 0th location */
small=a[0];
          for(i=1;i<n;i++)
                    {
                    if(a[i]<small)
                              small=a[i];
                    }
          printf(“/n small=%d”, small);
}
Write a program to arrange N given numbers in ascending order.
          #include<stdio.h>
          main( )
                    {
                              int i,n,a[50],temp;
                    printf(“Give the value of n”);
                    scanf(“%d”, &n);
          for(i=0;i<n;i++) /*i<n because
                    array starts from 0th location */
                    scanf(“%d”,&a[i]);
          for(i=0;i<n-1;i++)
                    {
                    for(j=i+1;j<n;j++)
                         {
                    if(a[i]>a[j])   
                        {
                              temp=a[i];
                              a[i][=a[j];
                              a[j]=temp;
                          }
        }
printf(“printing the list:\n”);
for(i=0;i<n;i++)
          printf(“%d”, a[i]);
}

          Write a program to find the smallest number and its position in a given one
dimensional array.
#include<stdio.h>
          main( )
                    {
                              int i,n,a,small,position;
                    int a[100];
                    printf(“Give the value of n”);
          scanf(“%d”, &n);
for(i=0;i<n;i++)
          scanf(“%d”,&a[i]);
small=a[0];
position=0;
for(i=1;i<n;i++)
          {
          if(a[i]<=small)
                    {
                    small=a[i];
                    position=i;
                    }
printf(“the smallest elements is : %d      \n”, small);
printf(“the position is : %d  /n”, position);
}




          Write a program to calculate the mean and standard deviation for 10 numbers.
          Formula


 
          Mean  =   S



 
          SD  =       S            -        S

#include<stdio.h>
#include<math.h>
main( )
          {
                    int i,n;
          float a[50],mean,sd;
          float sumsq=0.0, sum=0.0;
          printf(“Give the numbers of         elments”);
          scanf(“%d, &n);
          for(i=0;i<n;i++)
                    {
                              scanf(“%d”,&a[i]);
                              sum+=a[i];                        //sum=sum+a[i]
                    }
          mean=sum/float(n);
          for(i=0;i<n;i++)
                    {
                              sumsq=sumsq+a[i]*a[i];
                    }
                    sd=sqrt(sumsq/float(n)-mean*mean);
                    printf(“Mean is:%f”/mean);
printf(“Standard deviation is: %f’, sd);
}



Write a program to read a (nxm) matrix. (nxm means n rows and m colums)
          #include<stdio.h>
          main( )
                    {
                              int a[50][50], i, j, m, n;
                              printf(“\n Enter the order of matrix”);
                    scanf(“%d%d”, &n, &m);
                              printf(“\n Enter the matrix value”);
          for (i=0;i<n;i++)
          for (j=0;j<m;j++)
                    scanf(“%d”, &a[i][j]);
}


Write a program to print a (nxm) matrix
          #incude<stdio.h>
          main( )
                    {
                              int a[50][50], i, j, m, n;
                    scanf(“%d%d”, &n, &m);
          /* reading the matrix */
          for (i=0;i<n;i++)
          for (j=0;j<m;j++)
                    scanf(“%d”, &a[i] [j]);
          /*  printing the matrix */
          for (i=0;i<n;i++)
          for (j=0;j<m;j++)
                    printf(%d”, a[i][j]);
}
Write a program to reverse a string
          #include<stdio.h>
          main( )
          {
                    char s1[10],  s2[10];
                    int 1=0, i;
                    while(s1[ù] != ‘\0’)
               ù++;
          for (i=0; i<ù; i++)
          s2 [ù-i-1] = s1[i];
s2[i] = ‘\0’;
printf(“the reversed string is \n”);
puts(s2);       }
Write a program to read a matrix and print its transpose

                                                  1  2  3  4
          Suppose A              =
5  6  7  8

1   5
2   6
          Transpose of A       =        3   7
                                                  4   8  

          /* transpose of a given matrix */
#incude<stdio.h>
          main( )
                    {
                              int a[20][20], i, j;
                              printf(“\Give the values of n and m”);
                    scanf(“%d%d”, &n, &m);
                              printf(“\n Enter the matrix”);
          for (i=0;i<n;i++)
          for (j=0;j<m;j++)
                    scanf(“%d”, &a[i][j]);
                    for(j=0;j<m;j++
          {
          for (i=0;i<n;i++)
                    printf(“%5d”, a[i][j]);
                    printf(“\n”);
}
          }



Write a program to print all the diagonal elements in a two dimensional array
a[20][20].
          The diagonal elements are a[0][0], a[1][1], a[2][2] etc.
          /* program to print diagonal elements */
#incude<stdio.h>
          main( )
                    {
                              int a[20][20], i, j;
          for (i=0;i<20;i++)
          for (j=0;j<20;j++)
                    scanf(“%d”, &a[i][j]);
for (i=0;i<20;i++)
                    printf(“%5d”, a[i][j]);
}

          Write a program to find the sum of the diagonal elements of a given 20x20
matrix.
          /* program to add the diagonal elements */
#incude<stdio.h>
          main( )
          {
          int a[20][20], i, j;
          int sum=0;
for (i=0;i<20;i++)
for (j=0;j<20;j++)
scanf(“%d”, &a[i][j]);
for (i=0;i<20;i++)
          sum=sum+a[i][i]; /* sum+=a[i][i];*/
printf(“sum of the diagonal=%d”,sum);
}



Write a program to sum all the elements of nxm matrix sum = a[0][0] + a[0][1]
+ a[0][2] + .......+ a[1][0] + a[1][1] + a[1][2] + ......
#incude<stdio.h>
          main( )
                    {
                              int a[20][20], n, m, i, j;
                    int sum = 0;
                              printf(“\Give the values of n and m”);
                    scanf(“%d%d”, &n, &m);
          for (i=0;i<n;i++)
          for (j=0;j<n;j++)
                    scanf(“%d”, &a[i][j]);
          for (i=0;i<n;i++)
                    for(j=0;j<m;j++
                    sum=sum+a[i][j];
printf(“Sum of the elements = %d”, sum)
}

Write a program to find the length of the given string
          #include<stdio.h>
          main( )
          {
          int  ù  = 0;
          char a[20];
          printf(“Give the string”);
          gets(a);
          while(a[ù]!=’\0’)
   ù ++;
          printf(“the length of the give string:%d   \n”, ù );
}




/* program to add two matrices*/
          #include<stdio.h>
          main( )
          {
          int a[10] [10], int b[10] [10], int c[10] [10];
          int i, j, n, m, p, q;
          printf(“give the row and column of A matrix \n”);
          scanf(“%d%d”, &n,&m);
          printf(“give the row and column of B matrix \n”);
          scanf(“%d%d”, &p,&q);
          / *checking the order of the matrix */
          if(n= =p)&&(m = = q)
          {
                    printf(“matrices can be added \n”);
                    for (i=0; i<n; i++)
                    for (j=0; j<m; j++)
                    scanf(“%d”, &a[i][j]);
          for (i=0; i<q; i++)
                    for (j=0; j<p; j++)
                    scanf(“%d”, &b[i][j]);
          for (i=0; i<n; i++)
                    for (j=0; j<m; j++)
                    c[i][j] = a[i][j] + b[i][j];
          for (i=0;i<n;i++)
          {
          for (j=0;i<m;j++)
          printf(‘%d”, c[i][j]);
          printf(“\n”);
}
}
else
printf(“matrixs cannot be added \n”);
          }




Write a program to find out the largest and smallest number in a given matrix.
          /* program to find the largest and smallest */
          #include<stdio.h>
          main( )
          {
          int a[10] [10], i, j, small, large;
          printf(“give the values of n and m  \n”);
          scanf(“%d%d”, &n,&m);
          for (i=0; i<n; i++)
for (j=0;j<m;j++)
          scanf(“%d”, &a[i][j]);
          small=a[0][0];
for (i=0;i<n;i++)
          for(j=0;j<m;j++)
          if(small>a[i][j])
          small= a[i][j]
large= a[0][0];
for (i=0;i<n;i++)
          for(j=0;j<m;j++)
          if(large<a[i][j])
          large=a[i][j];
printf(“smallest number is %d\t”, small);
printf(“largest number is %d\t”, large);
}




/* program to multiply two matrices*/
          #include<stdio.h>
          main( )
{
          int a[10] [10], b[10] [10], [10] [10];
int i, j, n, m, p, q;
printf(“give the row and column of matrix A\n”);
          scanf(“%d%d”, &n,&m);
printf(“give the row and column of matrix B\n”);
          scanf(“%d%d”, &p,&q);
          if(m= = p)
          {
          printf(“Matrices can be multiplied”);
          for (i=0; i<n; i++)
for (j=0;j<m;j++)
          scanf(“%d”, &a[i][j]);
for (i=0;i<p;i++)
          for(j=0;j<q;j++)
          scanf(“%d”, &b[i][j]);
for (i=0;i<n;i++)
          for(j=0;j<q;j++)
          {
          c[i][j]=0
          for (k=0;k<m;k++)
                    c[i][j]=c[i][j])+a[i][k]* b[k][j];
          }
          for(i=0;i<n;j++)
          {
          for (j=0;j<q;j++)
printf(“%5d”, c[i][j]);
printf(“\n”);
}
          }
else
          printf(“The matrices cannot be multiplied\n”);
}



  1. # include <stdio.h>  
  2. # include <conio.h>  
  3. void main()   
  4. {   
  5.  int x, n, count = 1, pow = 1 ;   
  6.  clrscr() ;   
  7.  printf("Enter the value of x : ") ;   
  8.  scanf("%d", &x) ;   
  9.  printf("\nEnter the value of n : ") ;   
  10.  scanf("%d", &n) ;   
  11.  while(count <= n)   
  12.  {   
  13.   pow = pow * x ;   
  14.   count ++ ;   
  15.  }   
  16.  printf("\nThe power of %d^%d is : %d", x, n, pow) ;   
  17.  getch() ;   
  18. }   
           Write a C program to calculate pow(x,n)
21.  Below solution divides the problem into subproblems of size y/2 and call the subproblems recursively.

#include<stdio.h>

/* Function to calculate x raised to the power y */
int power(int x, unsigned int y)
{
    if( y == 0)
        return 1;
    else if (y%2 == 0)
        return power(x, y/2)*power(x, y/2);
    else
        return x*power(x, y/2)*power(x, y/2);

}

/* Program to test function power */
int main()
{
    int x = 2;
    unsigned int y = 3;

    printf("%d", power(x, y));
    getchar();
    return 0;
}
22.  Time Complexity: O(n)
Space Complexity: O(1)
Algorithmic Paradigm: Divide and conquer.
23.  Above function can be optimized to O(logn) by calculating power(x, y/2) only once and storing it.
/* Function to calculate x raised to the power y in O(logn)*/
int power(int x, unsigned int y)
{
    int temp;
    if( y == 0)
        return 1;
    temp = power(x, y/2);
    if (y%2 == 0)
        return temp*temp;
    else
        return x*temp*temp;
}
Time Complexity of optimized solution: O(logn)
Let us extend the pow function to work for negative y and float x.
/* Program to test function power */
#include<stdio.h>
#include<math.h>
int main()
{
    double x=2;
    double y=3;
    double p;
    p=(pow(x,y));
    printf("%f",p);
    getchar();
   
}




#include<stdio.h>

void main()
 {
    int sum,n;
    fact x,y;

  printf("Enter the exponential(E) value");
  scanf("%f ",y);

  printf("Enter the value for x");
  scanf("%f",x);

  printf("No. of times x is to be multiplied");
  scanf("%d",n);

  while(e=n)
   {
     sum=x^n;  
     printf("The result of expression is =%d",sum);
     printf("Check this out%d");
   }
}
 
Is This Program Easily Understandable ?   
30 Yes
46 No



 

  Re: Program to find the value of e raised to power x using while loop


#include<stdio.h>
void main()
{
     int i=1,e,x,s,p=1;
    printf("Enter e and x :");
     scanf("%d%d",&e,&x);
     while(i<=x)
     {
         p=p*e;
            i++;
     }
      
     printf("power is %d ",p);
}

here pow(e,x) is calculated using this program.
 
1




 

  Re: Program to find the value of e raised to power x using while loop


#include<stdio.h>
#include<math.h>
main()
{
int i,j,n,x,fact=1;
float c,t,sum=1;
printf("enter the value of x and n);
scanf("%d%d",&x,&n);
while(i<n-1)
{
c=pow(x,i);
for(j=1;j<i;j++)
fact=fact*j;
t=c/fact;
sum=sum+t;
}i=i+1;
printf("the sum of series is %f",sum);
getch();
}
 


 

 Re: Program to find the value of e raised to power x using while loop

include<stdio.h>
#include<math.h>
main()
{
int i=1,e,x,c;
printf("enter the value of x and e);
scanf("%d%d",&x,&e);
while(i<=e)
{
c=pow(x,e);
i=i+1;
}
printf("the sum of series is %f",c);
getch();
}
 
Is This Answer Correct ?   
5 Yes
15 No


 

  Re: Program to find the value of e raised to power x using while loop


#include<stdio.h>
#include<conio.h>
#define e 2.718281828

int main()
{
   double ex=e;
   int x,i=0;
   printf("\nenter the power : ");
   scanf("%d",&x);
   while(i++!=x-1)
   {
      ex*=e;
   }
   printf("\nthe result is : %f",ex);  
   getch();
   return 0;
}

/*i hope u get the logic.....TR dash*/
note:--->run in dev C++ / TC