Tuesday, 8 March 2016



WAP for simple calculator using switch case


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace switch_case
{
    class Program
    {
        static void Main(string[] args)
        {
          int  a, b, ch, add, div, mul, sub;
            Console.Write("Enter the 1st no=");
            a = Convert.ToInt32(Console.ReadLine());
            Console.Write("Enter the 2nd no=");
            b = Convert.ToInt32(Console.ReadLine());
      
            Console.WriteLine("1.Addition");
            Console.WriteLine("2.Subtraction");
            Console.WriteLine("3.Multiplication");
            Console.WriteLine("4.Division");
                Console.Write("Enter your choice");
                ch = Convert.ToInt32(Console.ReadLine());
                switch (ch)
                {
                    case 1:
                        add = a + b;
                        Console.WriteLine(add);
                        break;
                    case 2:
                        sub = a - b;
                        Console.WriteLine(sub);
                        break;
                    case 3:
                        mul = a * b;
                        Console.WriteLine(mul);
                        break;
                    case 4:
                        div = a / b;
                        Console.WriteLine(div);
                    break;
                    default :
                        Console.WriteLine("wrong choice");
                        break;
                }
                Console.ReadLine();
        }
    }
}


OUTPUT:-



 

WAP for GCD of two numbers.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace vaishu
{
    class Program
    {
        static int gcd(int a, int b)
        {
            int temp;
            if (b == 0)
                return a;
            else
                while (b != 0)
                {
                    temp = b;
                    b = a % b;
                    a = temp;
                }
            return a;
        }



        static void Main(string[] args)
        {
            int n1, n2, gcd1;
            Console.WriteLine("Enter first number" );
            n1 = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("Enter second number");
            n2 = Convert.ToInt32(Console.ReadLine());
            gcd1 = gcd(n1, n2);
            Console.WriteLine("gcd= " + gcd1);
            Console.ReadLine();
        }
    }
}




Output:
    
     



WAP for fibonacci series

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace fib
{
    class Program
    {
     
        static void Main(string[] args)
        {
            int f1 = 0, f2 = 1, f3 = 0;
            int n,i;
            Console.Write("Enter the limit= ");
            n = int.Parse(Console.ReadLine());

            Console.WriteLine(f1);
            Console.WriteLine(f2);
            for (i =3; i <= n; i++)
            {
                f3 = f1 + f2;
                f1 = f2;
                f2 = f3;
              
                Console.WriteLine(f3);

            }

 
                Console.ReadLine();
             
        }
    }
}



Output:

 

WAP for number is armstrong


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace arm
{
    class Program
    {
        static void Main(string[] args)
        {
            int sum, temp, no, rem;
            Console.WriteLine("Armstrong no between 1 to 1000=");
            for (no = 1; no <= 1000; no++)
            {
                temp = no;
                sum = 0;

                while (temp > 0)
                {
                    rem = temp % 10;
                    sum = sum + (rem * rem * rem);
                    temp = temp / 10;
                }
                if (no == sum)
                {
                    Console.WriteLine("   " + no);
                  
                }

            }
            Console.ReadLine();
        }
    }
}



Output:

 


WAP to swap without using third variable.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace temporary
{
    class Program
    {
        static void Main(string[] args)
        {
            int a, b;
            Console.WriteLine("Before Swapping:- ");
            Console.Write("\nEnter the value of a= ");
            a=Convert.ToInt32(Console.ReadLine());
            Console.Write("\nEnter the value of b= ");
            b=Convert.ToInt32(Console.ReadLine());
            a = a + b;
            b = a - b;
            a = a - b;


            Console.WriteLine(" \n After Swapping:-\n");
            Console.WriteLine("\nvalue of a= "+a);
            Console.WriteLine("\nvalue of b= " + b);
            Console.ReadLine();

        }
    }
}

OUTPUT:-


WAP to access and mutate named properties.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication6
{
    class Example
    {
        int _number;
        public int Number
       {
            get
            {
                 return this._number;
             }
            set
            {
                this._number=value;
            }
        }
    }
     class Program
    {
        static void Main(string[] args)
        {
            Example example=new Example();
            example.Number=5;//set()
            Console.WriteLine("number="+example.Number);
            Console.ReadLine();
        }

    }
}

OUTPUT:-

WAP for command line arguments


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

main(int arg, char *ar[])
{
  int n,i;
  clrscr();
  for(i=0;i<arg;i++)
  printf("argv[%d] = %s\n",i,ar[i]);
  getch();
}



WAP for copy file using command line arguments


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

main(int argc, char *argv[])
{
  FILE *source,*dest;
  int c;
  clrscr();
  if(argc!=3)
  {
   printf("\nWrong no of arguments ");
   exit(1);
  }
  if((source=fopen(argv[1],"r"))==NULL)
  {
   printf("\nCan't open file ");
   exit(1);
  }
  if((dest=fopen(argv[2],"w"))==NULL)
  {
   printf("\nCan't open file ");
   exit(1);
  }
  while((c=fgetc(source))!=EOF)
  fputc(c,dest);

  fclose(source);
  fclose(dest);
  printf("\ndone");
  getch();
}


WAP to check how many arguments are supplied


#include <stdio.h>
#include <conio.h>
int main( int argc, char *argv[] )  {

   if( argc == 2 ) {
      printf("\nThe argument supplied are %s\n", argv[1]);
   }
   else if( argc > 2 ) {
      printf("\nToo many arguments have supplied");
   }
   else {
      printf("\nOnly one argument is expected");
   }
   getch();
   return 0;
}


WAP to give basic information


#include <stdio.h> 
#include <conio.h>    
void main(int argc, char *argv[] )  { 
     
printf("My Program Name Is: %s\n", argv[0]); 
      
     if(argc < 2){ 
          printf("\nNo argument are passed"); 
     } 
     else{ 
          printf("First argument: %s\n", argv[1]); 
     } 
getch();
    } 

Monday, 29 February 2016

WAP for matrix multiplication

#include<stdio.h>
#include<conio.h>
#define row1 3
#define col1 3
#define row2 3
#define col2 3
main()
{
 int m1[row1][col1],m2[row2][col2],m3[row1][col2];
 int i,j,k;
 clrscr();
 printf("\nEnter matrix m1[%d][%d] row wise",row1,col1);
 for(i=0;i<row1;i++)
   for(j=0;j<col1;j++)
      scanf("%d",&m1[i][j]);
 printf("\nEnter matrix m2[%d][%d] row wise",row2,col2);
  for(i=0;i<row1;i++)
   for(j=0;j<col1;j++)
      scanf("%d",&m2[i][j]);

 printf("\n Matrix 1\n");
 for(i=0;i<row1;i++){
   for(j=0;j<col1;j++){
      printf("%4d",m1[i][j]);
      }
   printf("\n");
 }

 printf("\n Matrix 2\n");
 for(i=0;i<row1;i++){
   for(j=0;j<col1;j++){
      printf("%4d",m2[i][j]);
      }
   printf("\n");
 }

       /* Multiplication Logic  */
  for(i=0;i<row1;i++)
  {
   for(j=0;j<col2;j++)
   {
    m3[i][j]=0;
    for(k=0;k<col1;k++)
    {
     m3[i][j]+=m1[i][k]*m2[k][j];
    }
   }
  }
 printf("\n Resultant Matrix\n");
 for(i=0;i<row1;i++){
   for(j=0;j<col1;j++){
      printf("%4d",m3[i][j]);
      }
   printf("\n");
 }

 getch();
}