Showing posts with label Simple Examples. Show all posts
Showing posts with label Simple Examples. Show all posts

Friday, September 23, 2011

WAP to swap two numbers using call by reference method


/*
  Name: WAP to swap two numbers using call by reference method
  Author: Parveen Malik
  Date: 23/09/11 11:43
*/


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

void swap(int*,int*);


int main()
{
    int x,y;
    printf("Enter the value of x and y \n");
    scanf("%d%d",&x,&y);
    printf("Before swapping\nx = %d\ny = %d\n",x,y);
    swap(&x,&y);
    printf("After swapping\nx = %d\ny = %d",x,y);
    getch();
    return 0;
}


void swap (int *a, int *b)
{
     int temp;
     temp=*a;
     *a=*b;
     *b=temp;
     }


OUTPUT : 


     

WAP to swap two numbers without using third variable


/*
  Name: WAP to swap two numbers without using third variable
  Author: Parveen Malik
  Date: 23/09/11 11:34
*/
#include <stdio.h>
#include <conio.h>
 int main()
 { 
     int x,y;
     printf("Enter the value of x and y \n");
     scanf("%d%d",&x,&y);
     printf("Before swapping\nx = %d\ny = %d",x,y);
     x=x+y;
     y=x-y;
     x=x-y;
     
     printf("\nAfter swapping\nx = %d\ny = %d\n",x,y);
     getch();
             return 0;
             }

OUTPUT : 

WAP to swap two numbers using third variable


/*
  Name: WAP to swap two numbers using third variable
  Author: Parveen Malik
  Date: 23/09/11 10:50
*/


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


int main()
{
    int x,y,temp;
    printf("Enter the value of x and y \n");
    scanf("%d%d",&x,&y);
    printf("Before swapping \nx = %d\ny = %d",x,y);
    temp=x;
    x=y;
    y=temp;
    printf("\nAfter swapping \nx = %d\ny = %d",x,y);
    getch();
    return 0;
}


OUTPUT : 



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 add digits of entered number


/*
  Name: WAP to add digits of entered number
  Author: Parveen Malik
  Date: 23/09/11 09:41
*/


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


int main()
{
    long num;
    int sum=0,rem;
    printf("Enter a number \n");
    scanf("%ld",&num);
    while(num!=0)
    {
                 rem=num%10;
                 sum=sum+rem;
                 num=num/10;
                 }
                 
                 printf(" Sum of digits of entered number is : %d",sum);
                 getch();
                 return 0;
                 }


OUTPUT : 



WAP to check if it is a leap year


/*
  Name: WAP to check Leap Year
  Author: Parveen Malik
  Date: 23/09/11 09:39
*/


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


int main()
{
int year;
printf("Enter a year to check if it is a leap year \n");
scanf("%d",&year);


if(year%400==0)
printf("%d is a leap year .\n",year);
else if(year%100==0)
printf("%d is not a leap year.\n",year);
else if(year%4==0)
printf("%d is a leap year.\n",year);
else
printf("%d is not a leap year.\n",year);
getch();
return 0;
}


OUTPUT : 



Sunday, September 18, 2011

WAP to display the message "Hello World" without a Semicolon.

There are many ways to do this but i'm showing you three ways to print "Hello World" without putting any semicolon.


Solution:1

#include <stdio.h>

#include <conio.h> 
void main( )
{
if(printf("Hello World"))
{
}

getch();
}


Solution:2



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

void main( )
{
swicth(printf("Hello World"))
{
}

getch();
}





Solution:3



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

void main( )
{
while(printf("Hello World"))  /*infinite loop*/
{
}

getch();
}

WAP to print all Armstrong numbers between 1 to 1000


/*
  Name: WAP to print all Armstrong numbers between 1 to 1000
  Author: Parveen Malik
  Date: 18/09/11 10:45
*/


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


int main()
{
int no, temp, rem, sum;


printf("Armstrong numbers between 1 and 1000 are:\n");
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)
printf("\n%d", no);
}
getch( );
return 0;
}
    
OUTPUT: 



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 check whether the entered number is positive,negative or zero


/*
  Name: WAP to check whether the entered number is positive,negative or zero
  Author: Parveen Malik 
  Date: 18/09/11 09:52
*/


#include<stdio.h>
#include<conio.h>
 int main()
 {
      long num;
      printf("\n\n\nEnter any number : ");
      scanf("%ld",&num);
      if(num>0)
      printf("\n%d is a positive number ",num);
      else if(num<0)
      printf("\n%d is a negative number ",num);
      else
      printf("\n%d is zero ",num);
      main();
      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:

Monday, January 3, 2011

Program to calculate simple interest.

/*
Name: Program to find out Simple interest
Author: Parveen Malik
Date: 03/01/11 18:45
*/

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


void main()
{
float rate,time,prin; // declaring rate, time and principle
float si; // simple interest

printf("Enter the value of\n\nPrinciple : "); // taking user
scanf("%f",&prin); //principle
printf("Rate : ");
scanf("%f",&rate);
printf("Time : ");
scanf("%f",&time);


si=prin*rate*time/100; //formula of finding simple interest
printf("\nSimple Interest : %.2f",si); // printing simple interest to the console

getch();
}




[caption id="attachment_55" align="aligncenter" width="800" caption="OUTPUT:"]program to find out simple interest[/caption]



Program to calculate area of a circle

/*
Name: Program to calculate area of a circle
Author: Parveen Malik
Date: 03/01/11 19:46
*/

#include<stdio.h>
#include<stdio.h>
#define PI 3.141 // to define value of pi


void main()
{
int radius;
float area;

printf("Enter the radius of the Circle : ");
scanf("%d",&radius);

area=PI*radius*radius; // formula to find the area of a circle
printf("\nArea of the Circle is : %.2f",area);

getch();
}
OUTPUT:



[caption id="attachment_12" align="aligncenter" width="656" caption="OUTPUT"]Program to calculate area of a circle[/caption]



Sunday, January 2, 2011

Program to find out sum of any two integers.

/*
Name:  Program to find out sum of any two integers.
Author: Parveen Malik
Date: 02/01/11 16:05
*/

#include<stdio.h>
#include<conio.h>   // preprocessor section


void main()
{
int n1,n2,sum; //declare integers

printf("Enter any two integers \n");
scanf("%d%d",&n1,&n2); // for the user input

sum=n1+n2;

printf("\n\n%d + %d = %d",n1,n2,sum); // to print the sum


getch();
}


[caption id="attachment_11" align="aligncenter" width="655" caption="OUTPUT"]Program to find out sum of any two integers.[/caption]

First Program "hello world"

Here is the first program from which you can start your programming as i did. C is a general purpose programming language developed by Dennis Ritchie at Bell Laboratories(1972).
C is the one of the most popular language of all the time. C is  influenced by B, BCPL, CPL, ALGOL, Assembly, FORTRAN, PL/I and it has influenced many languages like C++, C--, C#, Objective C, BitC, D, Java, JavaScript, PHP, Perl, Pike , LPC and Python etc.
Look at the program below and try to understand it.



/*  Name: hello world
Author: Parveen Malik,,
Date: 02/01/11 15:11      // comment section
*/


#include<stdio.h> // header section
#include<conio.h>


void main() //program starts from here
{
printf("hello world"); // Prints the "hello world" on console screen

getch();

} //exit





[caption id="attachment_8" align="aligncenter" width="662" caption="OUTPUT"]First Program "hello world"[/caption]