Walk Lightly on this PLANET and yet leave such a FOOTPRINT that cannot be erased for thousands of Years..!!!
Visit Codstech for Cyber Security related Posts !

Visitors

Showing posts with label C-Simple Programs. Show all posts
Showing posts with label C-Simple Programs. Show all posts

Saturday, September 17, 2011

C-43 : Sum of 1 to 100 numbers

Refer  Find Sum , Sum of n even numbers using user defined fun... 


1st Method :

#include<stdio.h>
#include<conio.h>
void main()
            {
            clrscr();
            int i,sum=0;
            for(i=1;i<101;i++)
                        sum=sum+i;
            printf("The sum of the numbers from 1 to %d is %d\n",i-1,sum);
            getch();
            }

2nd Method :

#include<stdio.h>
#include<conio.h>
void main()
            {
            clrscr();
            int i,sum=0;
            for(i=1;i<101;sum+=i++)
            ;
            printf("The sum of the numbers from 1 to %d is %d\n",i-1,sum);
            getch();
            }


3rd Method :

#include<stdio.h>
#include<conio.h>
void main()
            {
            clrscr();
            int i,sum=0;
            for(i=1;i<101 && (sum+=i++);)
            ;
            printf("The sum of the numbers from 1 to %d is %d\n",i-1,sum);
            getch();
            }



Output : 


The sum of the numbers from 1 to 100 is 5050
=================================================================
Back to Home   &  C

Thursday, September 1, 2011

C-42 : Read values from keyboard for calculations

Read values from keyboard for calculations . And see how scanf() works.

Refer Simple Calculations also.



#include<stdio.h>
#include<conio.h>
main()
     {
      clrscr();
      int x,y,z;
      printf("Enter a value of x:");
      scanf("%d",&x);
      printf("Enter a value for y:");
      scanf("%d",&y);
      z=x*y;
      printf("z=x*y=%d\n",z);
      getch();
      return 0;
     }


Output :

Enter a value of x:
3Enter a value of y: 4
z=x*y=12
=========================================================================
Back to Home   &  C


C-41: Simple Calculations

Elementary operations with small integers:

Refer Find Sum also.



#include<stdio.h>
#include<conio.h>
main()
      {
      clrscr();
      printf("\n\t\n\t");
      printf("x=%d,y=%d\n",x,y);
      z=x-y;
      printf("z=x-y=%d\n",z);
      z=x*y;
      printf("z=x*y=%d\n",z);
      z=x/y;
      printf("z=x/y=%d\n",z);
      z=y/x;
      printf("z=y/x=%d\n",z);
      z=x%y;
      printf("z=x%%y=%d\n",z);
      z=y%x;
      printf("z=y%%x=%d\n",z);
      getch();
      }



Output :x=5,y=7
z=x-y=-2

z=x*y=35
z=x/y=0
z=y/x=1
z=x%y=5
z=y%x=2
=========================================================================
Back to Home   &  C


Wednesday, August 31, 2011

C-39 : Example of Escape Sequence (\n)

\n is the example of  Escape Sequence.
It's used to print the newline character.
Note that ,\n doesn't appear in the output.
Place \n wherever you want to insert a new line in the output.

See the examples below :
(Eg:-1) :

#include<stdio.h>
#include<conio.h>
void main()
{
            clrscr();
            printf("This\nString\nwill\nbe\nprinted\nin\n8\nlines.\n");
            getch();
}

Output:
This
String
will
be
printed
in
8
lines
===========================================================================
(Eg :-2) :
#include<stdio.h>
#include<conio.h>
main()
            {
            clrscr();
            printf("This is the First Line of Output.");
            printf("But is this the Second\nline of Output?");
            getch();
            }

Output:
This is the First Line of Output. But is this the Second
line of Output?


===========================================================================
(Eg :-3) : 




#include<stdio.h>
#include<conio.h>
main()
            {
            clrscr();
            printf("In how many lines will the output\nof this program be printed?");
            getch();
            }



Output:
In how many lines will the output
of this program be printed?
===========================================================================
(Eg:-4) :


What will be the output of the program ?

#include<stdio.h>
#include<conio.h>
main()
      {
      clrscr();
      printf("\"\\n\", the teacher said,\"is used to  ");
      printf("insert a new line in a C string.\"");
      printf("\n\" I C,I C\",said the blind student.\n");
      getch();
      return 0;
      }

Output:


" \n", the teacher said, "is used to  insert a new line in a C string".
"I C,I C", said the blind student.

(An important point to remember :  ""(Double Quote) character is not an Escape Sequence ).
.


=========================================================================
Back to Home   &  C

C-38: Example for Nested IF

Example to demonstrate Nested IF :

Refer Find largest from given numbers also.



#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();

            printf("First Section\n");
            {
                 printf("Second Section\n");
                 {
                        printf("Third section\n");
                        {
                                    printf("Fourth Section\n");
                        }
                 }
            }
            getch();
}



Output:

First Section
Second Section
Third Section
Fourth Section
=========================================================================
Back to Home   &  C

C-37 : My first C Program

My first C Program ; To display a message


#include<stdio.h>
#include<conio.h>
void main()
            {
            clrscr();
            printf("My first Program!! \n");
            getch();
            }


Output :

My first Program!!




Refer To display a message also.

Note :
While using code block ....See the input code below :
(use file --> new empty file )
 

(No need to  use "clrscr" if u are using code block like software)
=========================================================================
Back to Home   &  C

Friday, February 11, 2011

C16 : Array Declaration

Back to Home   &  C


//Program to declare an Array and initialize value to it

#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int a[5]={20,30,10,40,60};
int i;
for(i=0;i<5;i++)
 {
 printf("\n%d\n",a[i]);
 }
getch();
}
---------------------------------------------------------
Output
20
30
10
40
60
===============================================
Back to Home   &  C

C15 : Print numbers that is divisible by 7

Back to Home   &  C


//Program to print numbers that is completely divisible by 7 between 1 and n,
also count total number,and find its sum

#include<stdio.h>
#include<conio.h>
void main()
 {
 clrscr();
 int i=0,ct=0,sum=0,n;
 printf("Enter limit value\n");
 scanf("%d",&n);
 printf("The numbers are...\n");
 while(i<=n)
 {
 if(i%7==0)
 {
 printf("%d\n",i);
 ct++;
 sum=sum+i;
 }
 i++;
 }
 printf("sum=%d\n",sum);
 printf("count=%d",ct);
 getch();
 }
-----------------------------------------------------------------------------------
Output
Enter Limit
50
Numbers are...
0
7
14
21
28
35
42
49
sum=196
count=8
====================================
Back to Home   &  C