Showing posts with label For Loop. Show all posts
Showing posts with label For Loop. Show all posts

Friday, September 23, 2011

WAP to add n numbers


/*
  Name: WAP to add n numbers
  Author: Parveen Malik
  Date: 23/09/11 10:43
*/


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


int main()
{
int n,sum=0;
int count,var;
printf("Enter the number of integers you want to add : ");
scanf("%d",&n);
printf("Enter %d numbers \n",n);
for(count=1;count<=n;count++)
{
                             scanf("%d",&var);
                             sum=sum+var;
                             }
                             printf("Sum of entered numbers = %d \n",sum);
                             getch();
                             return 0;
                             }


OUTPUT : 



WAP to find factorial of a given number without using recursion


/*
  Name: WAP to find factorial of a given number without using recursion
  Author: Parveen Malik
  Date: 23/09/11 10:21
*/


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


long factorial(int);
void main(void)
{
     int num;
     long fact=1;
     printf("Enter a number to calculate its factorial : ");
     scanf("%d",&num);
     printf("%d = %ld \n",num,factorial(num));
     getch();
     }
     long factorial(int n)
     {
          int count;
          long result=1;
          for(count=1;count<=n;count++)
          result=result*count;
          return (result);
          }


OUTPUT:



WAP to check whether entered number is prime or not



/*
  Name: WAP to check whether entered number is prime or not
  Author: Parveen Malik
  Date: 23/09/11 02:28
*/


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


main()
{
   int n, c;


   printf("Enter a number\n");
   scanf("%d", &n);


   if ( n == 2 )
      printf("Prime number.\n");
   else
   {
       for ( c = 3 ; c <= n - 1 ; c++ )
       {
           if ( n %c == 0 )
              break;
       }
       if ( c != n )
          printf("Not prime.\n");
       else
          printf("Prime number.\n");
   }
   getch();
   return 0;
}


OUTPUT : 





WAP to find first n numbers of the fibonacci series


/*
  Name: WAP to find first n numbers of the fibonacci series
  Copyright: Paul Griffiths 2000
  Author: Parveen Malik
  Date: 23/09/11 01:56
  Description: Finds the first 'n' numbers in the Fibonacci sequence.
  'n' must be supplied as the sole command line argument,
  and must be between 3 and 47.
*/


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


int ParseCmdLine(int argc, char *argv[]);


int main(int argc, char * argv[]) {
    unsigned long f1 = 1, f2 = 1;
    int temp, i = 3;
    int n = ParseCmdLine(argc, argv);


    printf("First %d numbers in the Fibonacci sequence:\n\n", n);


    printf("%11lu%11lu", f1, f2);


    for ( i = 3; i <= n; ++i ) {
        temp = f2;
        f2 += f1;
        f1 = temp;


        printf("%11lu", f2);


        if ( i % 5 == 0 )
            putchar('\n');
    }
    putchar('\n');
    return EXIT_SUCCESS;
}




/*  Returns the integer specified on the command line  */


int ParseCmdLine(int argc, char *argv[]) {
    int n;
    char * endptr;


    if ( argc < 2 ) {
        fprintf(stderr, "You must supply an argument\n");
        exit(EXIT_FAILURE);
    }
    else if ( argc > 2 ) {
        fprintf(stderr, "You must only supply one argument\n");
        exit(EXIT_FAILURE);
    }


    n = strtol(argv[1], &endptr, 0);
    if ( *endptr ) {
        fprintf(stderr, "You must supply a whole number as an argument\n");
        exit(EXIT_FAILURE);
    }


    if ( n < 3 ) {
        fprintf(stderr, "You must supply a number greater than 2\n");
        exit(EXIT_FAILURE);
    }
    else if ( n > 47 ) {
        fprintf(stderr, "You must supply a number less than 48\n");
        exit(EXIT_FAILURE);
    }


    return n;


}

Sunday, September 18, 2011

WAP to print all combinations of characters A,B,C


/*
  Name: WAP to print all combinations of characters A,B,C
  Author: Parveen Malik
  Date: 18/09/11 10:19
*/
#include<stdio.h>
#include<conio.h>


int main()
{
    char ch1, ch2, ch3;


for(ch1='A' ; ch1<='C' ; ++ch1)
{
            printf("\n");
for(ch2='A' ; ch2<='C' ; ++ch2)
for(ch3='A' ; ch3<='C' ; ++ch3)
printf(" %c%c%c", ch1, ch2, ch3);
}
getch( );
return 0;
}


OUTPUT: 



WAP to construct a pyramid of numbers.


/*
  Name: WAP to construct a pyramid of numbers.
  Author: Parveen Malik
  Date: 18/09/11 09:26 
*/


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


int main()
{
    int i,j,num;
    printf("Enter the value of last number to be printed : ");
    scanf("%d",&num);
    for(i=1;i<=num;i++)
    {
                       printf("\n");
                       for(j=1;j<=i;j++)
                       {
                                        printf("%3d",j);
    
                                        }
                                        }
                                        getch();
                                        return 0;
                                        }
                                        
                                         
OUTPUT:

WAP to displays the position or index in the string S where the string T begins, or -1 if S doesn't contain T.


/*
  Name: Write a C program to displays the position or index in the string S where the string T begins, or -1 if S doesn't contain T. */
/*
  Author: Parveen Malik
  Date: 18/09/11 08:58
*/


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


int main(void)
{
    char str[10],tr[10];
    int i,j,pos;
    int flag=0;
    int count=0;
    printf("Enter the main string : ");
    gets(str);
    printf("Enter the substring : ");
    gets(tr);
    
    for(i=0;str[i]!='\0';i++)
    {
                             if(str[i]==str[0])
                             {
                                               pos=i;
                                               for(j=0;tr[j]!='\0';j++)
                                               {
                                                                       if(str[i]==tr[j])
                                                                       count++;
                                                                       i++;
                                                                       }
                                               flag=1;
                                               break;
                                               }
                                               }
                                               if(flag==1&&count==strlen(tr))
                                               printf("\nPostion :%d, and T is a substring of S",pos+1);
                                               else if(flag==1&&count<strlen(tr))
                                               printf("\nPosition : %d, and T is not a substring of S",pos+1);
                                               else
                                               printf("\nIndex : -1");
                                               getch();
                                               }

OUTPUT: 

WAP to print binary triangle



/*
  Name: WAP to print binary triangle 
  Author: Parveen Malik
  Date: 18/09/11 05:40
*/


#include<stdio.h>
int main()

    int i,j,row,mod,temp=1;
char inputstr[35];  /* make longer than reasonable input will expected */
                   /* error checking should still be employed */
                   /* to clear buffer overflow, if input is too long */
                   
while ( temp==1)
{
      row=0;
      while(row<=0)
      {
                   printf("\n Enter the number of rows you want : ");
                   fflush(stdout); /* printf didn't have newline, need to flush */
                   fgets(inputstr,34,stdin);
                             /* error checking should go here for buffer overflow */
                             
       row = atoi (inputstr);
       if(row<=0)
       printf("invalid input, try again \n");
       }
      for ( i=0;i<=row-1;i++)
      {  
          mod=i%2;
          for(j=0;j<=i;j++)
          {
                           printf("%d",mod);
                           if(mod==0)
                           mod=1;
                           else 
                           mod=0;
                           }
                           printf("\n");
                          
                                                  }
                                                     }
                                                     
                                                     return 0;
                                                     }



OUTPUT: 


WAP to calculate the addition of two matrix


/*
  Name: WAP to calculate the addition of two matrics 
  Author: Parveen Malik
  Date: 18/09/11 03:31 
*/


#include<stdio.h>
#include<conio.h>
int main(void)
{
     float a[10][10],b[10][10],c[10][10];
     int i,j,m,n;
     printf("Enter the size of Matrices as m,n : ");
     scanf("%d%d",&m,&n);
     printf("Enter %d elements of Matrix A row-wise\n",m*n);
     for(i=0;i<m;i++)
     {
                     for(j=0;j<n;j++)
                     {
                                     scanf("%f",&a[i][j]);
                                     }
                                     }
     printf("Enter the %d elements of Matrix B row-wise\n",m*n);
     for(i=0;i<m;i++)
     {
                     for(j=0;j<n;j++)
                     {
                                     scanf("%f",&b[i][j]);
                                     }
                                     }
     /* now add A Matrix + B Matrix */
     
     for(i=0;i<m;i++)
     {
                     for(j=0;j<n;j++)
                     {
                                     c[i][j]=a[i][j]+b[i][j];
                                     }
                                     }
     /* to print the result */
     
     printf("\n Addition of Matrix A + Matrix B : \n\n");
  for (i=0;i<m;i++)
{
      for(j=0;j<n;j++)
      {
                      printf("%3.2f\t",c[i][j]);
                      }
                      printf("\n");
                      }
                      getch();
                      return 0;
                      }
     
OUTPUT : 



WAP to find the smallest of 10 numbers using for loop


/*
  Name: WAP to find the smallest of 10 numbers using for loop 
  Author: Parveen Malik
 Date : 18/09/11 03:21


*/


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


int main(void)
{
    int a[15],min,i;
    printf("Enter any ten numbers \n");
    for(i=0;i<10;i++)
    {
                     scanf("%d",&a[i]);
                     if(i==0)
                      min=a[i];
                     
    if( a[i]<min)
    min=a[i];
}
printf("\nMinimum : %d",min);


getch();
return 0;
}

OUTPUT : 


WAP to find the largest of 10 numbers using for loop


/*
  Name: WAP to find the largest of 10 numbers using for loop 
  Author: Parveen Malik
  Date: 18/09/11 02:58 
*/


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


int main(void)
{
    int a[15],max,i;
    printf("Enter any ten numbers \n");
    for(i=0;i<10;i++)
    {
                     scanf("%d",&a[i]);
                     if(i==0)
                      max=a[i];
                     
    if( a[i]>max)
    max=a[i];
}
printf("\nMaximum : %d",max);


getch();
return 0;
}


OUTPUT : 



WAP to print square star pattern


/*
  Name: WAP to print square star pattern 
  Author: Parveen Malik
  Date: 18/09/11 02:12
*/


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


static void print(int length, int width)
{
        int j;
        int i;


        length--;
        width--;
        for(j = 0; j <= width; j++) {
                for(i = 0; i <= length; i++) {
                        if(!i || !j || j == width || i == length)
                                putchar('s');
                        else
                                putchar('*');
                }
                putchar('\n');
        }
}


int main(void)
{
        print(6, 4);
        getch();
        return 0;
}

OUTPUT : 

WAP to multiply two matrix


/*
  Name: WAP to Multiply two Matrix
  Author: Parveen Malik
  Date: 18/09/11 01:50 
*/


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


int main()
{
    float a[10][10], b[10][10],c[10][10];
    int m,n,p,q,i,j,k;
    printf("Enter the size of Matrix A as m,n : ");
    scanf("%d%d",&m,&n);
    printf("Enter the size of Matrix B as p,q : ");
    scanf("%d%d",&p,&q);
    
    if(n!=p)
    {
            printf("\n Matrix Multipication AxB is not feasible\n");
            exit(1);
            }
            printf("\n Enter the %d elements of Matrix A row-wise \n",m*n);
            
            for(i=0;i<m;i++)
            {
                            for(j=0;j<n;j++)
                            {
                                            scanf("%f",&a[i][j]);
                                            }
                                            }
            printf("\n Enter the %d elements of Matrix B row-wise \n",p*q);
            for(i=0;i<p;i++)
            {
                            for(j=0;j<q;j++)
                            {
                                            scanf("%f",&b[i][j]);
                                            }
                                            }
            for(i=0;i<m;i++)
            {
                            for(j=0;j<q;j++)
                            {
                                            c[i][j]=0;
                                            for(k=0;k<n;k++)
                                            {
            c[i][j]+=a[i][k]*b[k][j];
                                            }
                                            }
                                            }
    printf("\n Product AxB is : \n\n\n");
    for(i=0;i<m;i++)
{
    for(j=0;j<q;j++)
    {
                    
            printf("%5.2f\t",c[i][j]);
            }
            printf("\n");
            }
            
            getch();
            return 0;
            }
            
            OUTPUT :