Showing posts with label program in c. Show all posts
Showing posts with label program in c. Show all posts

Sunday, 8 July 2012

c program to find the sum of n natural numbers(for loop,while loop)

For beginners
{I am going write the c program but before you need a c compiler.there are a lot of compilers. like  Code::Blocks, dev c++  Download dev c++ its free and install it in your computer.open  dev c++ go to file-->new-->source file.}


#include<stdio.h>
#include<conio.h>
main()
{
      int n,c,sum=0;
      printf("enter a number\n");
      scanf("%d",&n);
      for(c=1;c<=n;c++)
      {
                      sum=sum+c;
                      printf("%d+",c);
                      }
                      printf("=%d",sum);
                      getch();
                      }

or


#include<stdio.h>
#include<conio.h>
main()
{
      int n,c=1,sum=0;
      printf("enter a number\n");
      scanf("%d",&n);
      while(c<=n)
      {
                      sum=sum+c;
                      printf("%d+",c);
                      c++;
                      }
                      printf("=%d",sum);
                      getch();
                      }

For beginners
{After typing this program go to execute-->compile & run-->a window will pop-up-->give file name:(anything u like)-->save as type: c source files-->click save.}



output

output of program is
enter a number
4
1+2+3+4+=10

IS IT HELPFIL? comment.



c program multiplication two numbers using user defined function

For beginners
{I am going write the c program but before you need a c compiler.there are a lot of compilers. like  Code::Blocks, dev c++  Download dev c++ its free and install it in your computer.open  dev c++ go to file-->new-->source file.}  


#include<stdio.h>
#include<conio.h>
int mul(int a,int b);
main()
{
      int a,b,c;
      printf("enter first number\n");
      scanf("%d",&a);
      printf("enter second number\n");
      scanf("%d",&b);
      printf("%d*%d = %d",a,b,mul(a,b));
      getch();
      }
      int mul(int x,int y)
      {
          int p;
          p = x*y;
          return(p);
          }


For beginners
{After typing this program go to execute-->compile & run-->a window will pop-up-->give file name:(anything u like)-->save as type: c source files-->click save.}

IS IT HELPFIL? comment.



Friday, 6 July 2012

First c program "hello world"

I am going write the first c program but before you need a c compiler(software).there are a lot of compilers(software). like  Code::Blocks ,dev c++  Download dev c++ its free and install in your computer.open  dev c++ go to file-->new-->source file.
 start your first c program (do not type step1,step2..)

step1 #include<stdio.h>
step2 #include<conio.h>
step3 main()
step4 {
step5   printf("hello world");
step6   getch();
step7   }


After typing this program go to execute-->compile & run-->a window will pop-up-->give file name:(anything u like)-->save as type: c source files-->click save.
output

The output of this program is hello world


How?
STEP1:
In almost every c program we use #include<stdio.h>.it is used to input the file stdio.h into the program we write.
STEP2:&STEP6:
if u write a program with out #include<conio.h> and getch() after running the program will close with out waiting your click.try it(remove step1 and step2) 
STEP3:&STEP4:&STEP7
main() it tell the computer this is the main part of this program inside "{"and"}"
STEP5
printf is used to print anything on screen.eg printf("hi"); it will print hi or printf("hello"); it will print hello

 IS IT HELPFIL? comment.