Wednesday, 30 October 2013

How to find largest number in c | largest number examples.

How to find largest number in c | largest number examples.
Find largest number in c language without using  any loop with examples.


int maxnumber(int num1, int num2, int num3)
{
    int largestnumber;
        largestnumber=int num1; /*  Assume number 1 is largest number*/
        if (num2 > largestumber)
        {
            largetnumber=num2;   /* number 2 is largest number*/

        }
if (num3 > largestnumber)
{
largestnumber=num3;

}

return largestnumber;
}


we can do this by using for loop but here i have shown how to find out largest number using

if statement.

Sunday, 16 December 2012

c program | Sorting of given number | implementation of Insertion sort

c program | Sorting of given number  |  implementation of Insertion sort

Logic and examples to sort given number in list.
In this article we will get idea about to sort number .
user will enter the number in any order like 4,5,3,67,8, etc..
and when we include in program it will get output like 3,4,5,8,67.
before to sort given number all the entered number will save in list .
then after it will sort by ascending order like small to bigger number and will show in ordered .
this program we call as Program for implementation of Insertion sort also.

#include<stdio.h>// header file
#include<conio.h>
#define max 20

void main() // start of main
{
int i,j,n,k,t,temp,a[max];//intializing variable and temp memory
clrscr();
printf("\How many no. you want to insert into list:- ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter %d no:- ",i+1);
scanf("%d",&a[i]);
}
printf("\nUnsorted list is:-\n");// will display unsorted list
for(i=0;i<n;i++)
printf("\n%d",a[i]);
for(i=1;i<n;i++)
{
for(j=0;j<i;j++)
{
if(a[i]<a[j])// cheking first value is less than next one
{
temp=a[j];
a[j]=a[i];
for(k=i;k>j;k--)
a[k]=a[k-1];
a[k+1]=temp;
}
}
}
printf("\n\nSorted list is:-\n");// will print sorted order in a list
for(i=0;i<n;i++)
printf("\n%d",a[i]);
getch();
}


Saturday, 15 December 2012

Armstrong number example in c program with coments

Armstrong number example in c program with coments

Armstrong number in c program with source code | comments | execution line by line


In this article i have shown how to Write program for Armstrong number in c  using function with while loop.
Basically we can write simple program for Armstrong number but by using function if we are

writing then its  scope will be different.
first of all get all the idea about what is Armstrong number .
its definition then only one programmer can develop his logic.
Armstrong number is number which is equal to sum its individual number cube.
cube means multiplication of same number thrice or three times.
suppose i want to check number 153 is Armstrong or not, then answer is yes .
because , 153=(1*1*1) + (5*5*5) + (3*3*3) =1+125 + 27 =126 + 27=153.
in this line 1 and 5 and 3 are 153 's individual number .and sum of cube of all three

numbers that number.
so , i think you got the little idea about Armstrong number.
so here we are going to write Armstrong number program in c language.

#include<stdio.h>  // including header files
#include<conio.h>
 void armstrong(int n);   // calling function armstrong(int n)
void main()
{
int n;   //initializing variable n
clrscr();
printf("enter the number");
scanf("%d",&n);   //153
arm(n);    // now n=153
getch();
}
void arm (int n)
{
int r,sum=0,no;
no=n;   //no=153
while(n>0)
{
r=n%10;   //taking mod  (153%10)=15 and r=3 and n=15
sum=(r*r*r)+sum; //3*3*3 + sum=27+0=27 so sum=27
n=n/10;   // now n=15

    // after first execution


r=n%10;   //taking mod  (15%10)=1 and mod =5 and n=10
sum=(r*r*r)+sum; //5*5*5 + sum=125+27=152
n=n/10;   // now n=10

     // after second execution

r=n%10;   //taking mod  (10%10)=0 and mod =1 and n=0
sum=(r*r*r)+sum; //1*1*1 + sum=152+1=153
n=n/10;     // after last execution
}
 
if(no==sum)        // now sum=153 and no=153 both are same hence 153 is armstrong number
printf("given number is armstrrong");
else
printf("not armstrong");

}


Armstrong number program is very simple program just we  should know about c logic and loop

syntax and concept about program.

C program code for swap the number | example code and comments

C program code for swap the number | example code and comments

Swapping means change value of one variable into another variable.

suppose int a=9 int b=7
After swapping b=9 and a=7 .
In this program I have shown how to do swaping of number in c program using function


void swap(int,int); // function name is swap
void main()  // opening of main  method
{
int a=10,b=20;  // initializing variable
clrscr();
swap(a,b);  // calling function
getch();
}
void swap(int x, int y)
{
int temp;  // declaring temp variable
temp=x;  // storing value of x into temp    temp=10
x=y;       // storing value of y into x            x=20
y=temp;  // storing vallue of temp which was x value into y  y=10
printf("\n a=%d\n b=%d",x,y);
}

Typecasting in C | Miscelleneous Features |Definition with example and with comments

 Typecasting in C | Miscelleneous Features |Definition with example and  with comments

What is typecasting?
Converting one datatype into another data type is known as typecasting.
for example conversion of int to float and float to  int , int to char and so on..
Basically there is two types of typecasting ..
1->Implicit typecasting
2->Explicit type casting


1:-Implicit typecasting -type casting done by system.
int a=10.8 float

now here float values is  typecasted to int now a=10 and 0.8 is ignored
float b=25
integer value is typecasted to  float b=15.0
Now let see invalid example of  typecasting.
int a ="25"
char a[]=75;
there is no typecasting when string is involved.



2;>Explicit type casting .
type casting  is done by programmer.
int a=9
int b=2

a/b=9/2=4
(float)a/b=9.0/2=4.5
a/(float)b=9/2.0=4.5 variable a and b are type casted to float . they  become 9.0 and 2.0 .
Their values continued to be 9 & 2 in rest of program.



/*How to write Program to illustrate use of typecasting*/
#include<stdio.h>
#include<conio.h>

//main function
void main()
{
float a;
int x=6,y=4;
clrscr();

//Without typecasting x/y
a=x/y;
printf("\n Value of a without typecasting =%f",a);


////After typecasting x/y to float
a=(float) x/y;
printf("\n Value of a after typecasting =%f",a);
getch();
}

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.

Tuesday, 4 December 2012

strcmp | How to check password in C with Break Statement and while

strcmp  |  How to check password in C with Break Statement and while

I don't have so much idea about its execution but at last its using strcmp to compare
original password and entered password by using f7 key you can get its idea .

#include<stdio.h> //initializing header files
#include<conio.h>
void main()
{
void pass();  // function name pass
clrscr();
pass();
printf("\n\nNow you can proceed...........\n");
getch();
}
void pass()
{
char ar[30],ch; //
int i=0;
printf("\n\nEnter password\n");
while(1)
{
ch=getch();
if(ch==13)
break;
else
{
ar[i]=ch;
i++;
printf("*");
}
}
ar[i]='\0';
if(strcmp(ar,"yourvalue")==0)
return;
else
{
printf("Invalid password try again\nPress any key to continu\n");
getch();
pass();
}
}

c program for positive (+ve)and negative(-ve) number | comments with source code example

c program for positive (+ve)and negative(-ve) number | comments with source code example

Positive number means number which is not negetive means number greater than 0 or equal to zero.
let's take example.
9 - 8=1
After subtracting 8 from 9 we will get out put =1 that is positive.
but if i will subtract 9 from 8 like 8 - 9= -1.
i will get out put like -1.
go to start -> run  -> calc .
it will open calci and then  you can calculate.or do any kind of calculation.so , in this program i am going to show  How to find out positive and negative number.
with comments in c program with source code.at last output.

#include<stdio.h>    //header files//
#include<conio.h>   //for clrscr//
main()
{                           //main start//
int user_number;       //declaration//
clrscr();                    //clear the screen//
printf("enter any number"); //taken number from user//
scanf("%d",&number);
//condition//

if(user_number>0)  //if user number is Greater than zero//
{
printf("\n the given number  is positive");
}

if(user_number<0) //if user number is Less than zero//
{
printf("\n the given number  is negetive");
}
getch();
}               //Let see the out-put
output is
enter any number 68
number  is positive

enter any number -56
a is negative

Tuesday, 20 November 2012

C program code for reverse number using function with while loop

 C program code for reverse number using function with  while loop

how to write code to reverse given number in c program using function with comments
#include<stdio.h>  //including header files
#include<conio.h>
long int reverse(long int); // declaring function with parameter
void main()    // start main
{
 long int n;
 clrscr();
 printf("enter the value \n"); // asking for enter the value
 scanf("%ld",&n); // taking value from user
 printf("result=%ld",reverse(n));  //calling function
 getch();
 }
long int reverse(long int a)  // intialization of function
{
 int r;
 long int s=0;
 while (a != 0)  // checking number will not zero
 {
  r=a%10;    
  s=10*(s+r);
  a=a/10;
  reverse(a);  // reversing number
  }
 return (s/10); //returning reversed number
 }
c programming example  for reverse number using function with  while loop

c program factorial number using function.

c program  factorial number  using function.

Take two for loop .first for loop for one to last .
till we want to factorial and second will calculate factorial of each number.
factorial means multiplication of numbers.
example
factorial of 5 is 5*4*3*2*1=120.
so factorial of 5 is 120.
same for 6 is 6*5*4*3*2*1=720.



#include<stdio.h>
#include<conio.h>
long int fact( int*);
void main()
{
  int n;
  clrscr();
  printf("enter the value \n");
  scanf("%d",&n) ;
  printf("result=%ld",fact(&n)) ;
  getch();
  }

 long int fact(int* a)
 {
 long int f=1,sum=0;  //initialization of sum=0 and variable f =1.
 int i,j;
 for(i=1;i<=*a;i++) //execution of for loop
 {
  for(j=1;j<=i;j++) // second for loop
  {
  f=f*i;
  }
  f=i/f;
  sum=sum+f;
  }
  return sum; // will return the factorial of given number.
  }


programming examples for  factorial number  using function

how to draw concentric circles with help of while loop

how to draw concentric circles with help of while loop

concentric  circles which share same axis ,center and origin.
so sphere and disks are concentric circle we can assume.

#include<graphics.h> //graphic shows header file for design in c language.

main()
{
    int gd=DETECT,gm;
    int n,x=10;
    initgraph(&gd,&gm," ");
    printf("How many concentric circles do you want?");//asking for number of circle want to draw.
    scanf("%d",&n);
    while(n!=0)
    {
    circle(240,150,x);
    setcolor(GREEN);// setting color as green.
    x+=10;
    n--;
    }
    getch(); // end of main
    closegraph();
}
programming examples for concentric circles

Sunday, 18 November 2012

c program Addition of two number using function with comments

c program Addition of two number  using function with comments

How to add two number using function in c language.
first include header files .
 This program is simple program in c language which is showing use of function to create in c  program and call that function for adding two numbers .in simple method we can do also but , by using function it makes easier .

#include<stdio.h> // header files

void main()    //start of main
{ //opening of main
  int first_number,second_number;      //declaration of two variable later which will store the values
  clrscr();
  printf("\n Enter 2 Number:"); //asking for enter the numbers
  scanf("%d %d",&first_number,&second_number);        //taking two numbers and storing  into variable first_number and second_number.
  printf("\Addition=%d",add(first_number,second_number));   calling function add()
}   //closing of main


  int add(int x,int y) //function add
  {
    return(x+y);   //will give addition of two numbers
  } // close function




Tuesday, 13 November 2012

Swap values of two variables using call by value | output | comments

Swap values of two variables using call by value | output | comments

How to write program to swap two numbers using call by value in c .
source code to swap two number .
void swap(int a,int b)
{
int temp;
temp=a;
a=b;
b=temp;
printf("\nValues of a and b within the function: %d %d",a,b);
}
int main()

{
int a,b;
//Accept value of a & b
printf("\nEnter value of a:");
scanf("%d",&a);
printf("\nEnter value of b:");
scanf("%d",&b);
//print values before calling swap function
printf("\nValues of a and b before calling the function: %d %d",a,b);
//Calling swap function
swap(a,b);
//print values after calling the swap function
printf("\nValues of a and b after calling the function: %d %d",a,b);
}

sscanf() function | c program | string

sscanf() function | c program | string


This function is same as scanf() function but it reads from string rather than standard input.
/*Program to illustrate the use of sscanf() function*/
Include header file
int main()
{
//declare two strings
char s1[20]="1234";
char s2[20]="345.67";
//declare int and float variables
int n;
float f;
//convert the variables into string
sscanf(s1,"%d",&n);
sscanf(s2,"%f",&f);
//print the converted strings
printf("Value of n=%d\nValue of f=%f\n",s1,s2);
}

sprintf() function | c program | source code

sprintf() function | c program | source code


c program sprintf() function use in program with source code.

This function is same as printf() function but it sends the formatted output to a string instead of screen.
So we can convert a variable data type into string using sprintf() function.

/*Program to illustrate the use of sprintf() function*/

include header file
int main()
{
//declare two strings
char s1[20];
char s2[20];

//declare int and float variables

int n=7890;

float f=2345.67;
//convert the variables into string
sprintf(s1,"%d",n);
sprintf(s2,"%0.1f",f);
//print the converted strings
printf("s1=%s,s2=%s\n",s1,s2);

}

how to swap two values in c program | call by reference in c code

how to swap two values in c program  | call by reference in c code

swap values of two variables using call by reference comments
How to write C Program to swap values of two variables using call by reference
include header files


//function to swap values
void swap(int *a,int *b)
{
int temp;
temp=*a;
*a=*b;
*b=temp;
printf("\nValues of a and b within the function: %d %d",*a,*b);
}


int main()
{
int a,b;
//Accept value of a & b
printf("\nEnter value of a:");
scanf("%d",&a);
printf("\nEnter value of b:");
scanf("%d",&b);
//print values before calling swap function
printf("\nValues of a and b before calling the function: %d %d",a,b);
//Calling swap function
swap(&a,&b);
//print values after calling the swap function

printf("\nValues of a and b after calling the function: %d %d",a,b);
}

How to write C Program to calculate L.C.M and G.C.D of two numbers using function

How to write C Program to calculate L.C.M and G.C.D of two numbers using function

Program source code
.first of all create two function first function gcd to find out gcd() and next lcm().
we need to pass two parameter  in given function .

void gcd(int,int); // created function for gcd
void lcm(int,int); // created function for lcm
int main()
{
int a,b;
printf("Enter two numbers: \t");
scanf("%d %d",&a,&b);
lcm(a,b);
gcd(a,b);
return 0;
}


//function to calculate l.c.m
LCM=Least Common Multiple or lowest common multiple

How to find  LCM of 3 and 5 by manually  .
Multiples of 3 are 3,6,9,12,15,18 etc..


and the multiples of 5 are 5,10,15,20, etc... 15 is common in both case.
so lcm of 3 and 5 is 15.

void lcm(int a,int b)
{
int m,n;
m=a;
n=b;
while(m!=n)
{
if(m
m=m+a;
else
n=n+b;
}
printf("\nL.C.M of %d & %d is %d",a,b,m);
}

//function to calculate g.c.d
GCD =Greatest common divisor , One number divides two number like
12  and 20 is divisible by 4 ,
hence gcd of 12 and 20 is 4.
void gcd(int a,int b)
{
int m,n;
m=a;
n=b;
while(m!=n)
{
if(m>n)
m=m-n;
else
n=n-m;
}
printf("\nG.C.D of %d & %d is %d",a,b,m);