Sunday, May 5, 2013

Simple Example ...

Examples:

\\1.The hello program

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


\\2.Adding two intergers

#include<stdio.h>
#include<conio.h>
void main()
{
int a, b,c;
printf("enter two no.");
scanf("%d%d",&a,&b);
c=a+b;
printf("SUM is =%d",c);
getch();
}


\\3.  Print out powers of 2: 1, 2, 4, 8, .. up to 2^N


#include <stdio.h>
#include<conio.h>
#define N 16

void main()

{  int n;           /* The current exponent */ 


int val = 1;     /* The current power of 2  */ 

printf("\t  n  \t    2^n\n");


printf("\t================\n");

   
 for (n=0; n<=N; n++)

{
 printf("\t%3d \t %6d\n", n, val);    

 val = 2*val;
 


getch();
}




\\4.Read a positive number N. Then read N integers and print them out together with their sum.

#include <stdio.h>
#include<conio.h>
void main()
{
int n;       /* The number of numbers to be read */ 

int sum;     /* The sum of numbers already read  */
  int current; /* The number just read             */ 
int lcv;     /* Loop control variable, it counts the number of numbers already read */ 

printf("Enter a positive number n > ");

scanf("%d",&n); /* We should check that n is really positive*/

sum = 0;

for (lcv=0; lcv < n; lcv++)
{

   printf("\nEnter an integer > ");

    scanf("%d",&current);    /*    printf("\nThe number was %d\n", current); */
    sum = sum + current;  } 
   printf("The sum is %d\n", sum);

getch();
}

No comments:

Post a Comment