Saturday, 8 December 2012

How to develop logic in c programming language with programs

How to develop logic in c programming language with programs

C Language  is First  Programming Language.
I Can say it is base of all Programming Language.
To develop the code in Java ,c# , VB , c++ ,Or any Language we need C.
I am not saying that only C is language where you can apply  your all logic but if you will apply then it
will be good.
If  you want to develop logic in c language then you have to learn Simple Programs ..
i.e .
Even - Odd Number.
Prime - Perfect Number.
Armstrong Number.
series of perfect and prime number.
By playing with loops you can improve logic in good way ..
Like
For loop ,
If - Else,
while loop..
goto statement
switch statement

C language gives you idea about ,How to write program using  Array ,Pointers, Structure.
Main thing is that you should know how to declare the variables and  how to initialize them..
How to create a  function and how to call the function which you have already created.
While writing the program , writing comments is also nice idea.

By Using Turbo C Just Use F7 key to check step by step execution .

Pyramid pattern in c program | to print pyramid | comments with for loop

Pyramid pattern in c program | to print pyramid | comments with for loop

In this program we are going to learn how to print  triangle in pyramid  pattern in c program using two for loop.so ,Basically there are two types of pyramid
1->Floyd triangle
2->Pascal triangle.

structure for pyramid

                                t
                               t  t
                             t t  t  t
                            t  t  t  t  t
void main ()// source code
{

int i,j,n;
clrscr();
printf("Enter number of lines\n");
scanf("%d",&n);
for(i=1;i<=n;i++)//loop for number of lines
{
 for(j=0;j<40-i;j++) //for printing spaces
{
printf(" ");
}
 for(j=i;j<=2*i-1;j++)//for printing trinagle on left half
 {
 printf("%d",j%10);
}
for(j=2*i-2;j>=i;j--)//for printing triangle on right half
{
printf("%d",j%10);
}
printf("\n");
}
getch();
}

another way also is there

Another pattern  with source code
* * * * *
* * * *
* * *
* *
*
*/

#include<stdio.h>
int main()
{
int i,j,n;

printf("Enter no. of lines: ");
scanf( "%d",&n);


//for loop for number of lines
for(i=n;i>=1;i--)
{

//for printing stars
for(j=1;j<=i;j++)
 {
printf("* ");
 }
 printf("\n");
}
return 0;
}

so , basically we need two for loops .first for loop for printing number of lines .
How many lines do you need .
then after print one simple star we need to give space for that we need third for loop.
After that triagle for left half and right half.

goto statement in c program | simple goto example in c

goto statement in c program | simple goto example  in c

In this article we are going to learn how to work with goto statement in c programming language.
Basically goto is jump statement which means we can go one point or functionality to another functionality  in same function or within function. for  that we use goto statement.

#include<stdio.h> // including header files
#include<conio.h>
void main()
{
int n;
clrscr();
printf("Enter the no");
scanf("%d",&n);
if(n % 2==0)
goto even;  // will goto even
else
goto odd;
even:printf("even"); // will print if number is even
odd:printf("odd");
getch();

Addition of two numbers in c program with source code.

Addition of two numbers in c program with source code.


In this article we are going to learn how to add two number entered by user in c programming language using function.
create function  and initialize that function and call that function by passing parameter you want to add.

#include<stdio.h>
void main()
{
  int a,b;
  clrscr();
  printf("\n Enter 2 Number:"); // user will enter two number one by one
  scanf("%d %d",&a,&b); // number will store in variable a and b.
  printf("\Addition=%d",add(a,b)); //calling  function add
}
  int add(int x,int y)  // in function add passed two parameter x and y.
  {
    return(x+y);  // adding two number
  }
return will  give output.

Thursday, 6 December 2012

Palindrome program | logic | examples source code | c++

Palindrome  program | logic | examples source code | c++

After reversing if  you are getting same  number it means you are reversing palindrome number.
to reverse the all digit and get same number is symbol of palindrome number.
Lets take examples
121  after reverse it  we will get 121. so we can say 121 is palindrome number.same is with 999=999.
12=21 not palindrome number .Have you seen mirror same functionality is working here.
But here is digit not your  face.so we are going to learn about palindrome number and execution.
we  can reverse string also like did=reverse(did)=did but i dont know about  words just think about digits .ok i think there is enough explanation now time is to do something real .
Let's concentrate to source code in c program.sorry about c language we are going to learn c++.everything is same in c just little about scanf and printf

How to write c++ palindrom number Program with source code


int pali(int);  // function name pali
void main()
{
int n,a;
clrscr();
cout<<"\n enter the number=";
cin>>n;  // accepting number from user
a=palindrom(n);
if(n==a)  // cheking after reversing number is same or not
cout<<"\n number is palindrom";  // yes it is
else
cout<<"\n number is not palindrom";  //no  it is not
getch();
}
int palindrom(int x) //
{
int r,sum=0;
while(x!=0) // till number will there
{
r=x%10; taking mode so we can get at least one number

sum=sum*10+r;   //addition
x/=10; //division
}
return(sum);//function calling
}

in  c or c++ do execution with your own .execution will give you correct logic and method to understand program logic.