Monday, 27 August 2012

While loop | c program while loop | examples with source code

 While loop | c program while loop | examples with source code
 In this program we are going to learn  how to write
source code for Prime number using while loop
in function.
#include<stdio.h> //header files//
#include<conio.h>
void main()
{              //start the program prime number//
int flag=1,i,no;
clrscr();
printf("Enter any number"); // enter any number//
scanf("%d",&no);
i=2;
while(i<=no/2) //applied While loop for  cheking  condition//
{
if(no%i==0) // start number is  equals to Zero//
{
flag=0;
break; // Break the Loop//
}
i++;
}
if(flag==1) //continue//
{
printf("the number is prime number");
}
else
{
printf("th number .is not prime number");
}
getch();
} // end of program//
The number which divided by 1 and itself only.
rather than this if the number divide by any other number
then we must say it is not a prime number
output is
1).Enter any number17
Number i prime number

2).Enter any number25
Number .is not prime number.

c program Decimal to binary | convert in c

c program  Decimal to binary | convert in c
how to convert | decimal  number to binary number in  c language
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int a,i=0,n,bin=0,r;
clrscr();
printf("enter the decimal no \n");
scanf("%d",&n);
while(n != 0)
{
r=n%2;
//  a=pow(10,i);
bin=bin+(pow(10,i)*r);
i++;
n=n/2;
}
printf("binary no.=%d \n",bin);
printf("enter the binary no \n");
scanf("%d",&n);
i=0;
bin=0;
while(n != 0)
{
r=n%10;
bin=bin+(pow(2,i)*r);
i++;
n=n/10;
}
printf("decimal no.=%d",bin);

getch();
}

C Programs | Array | Malloc | sizeof

C Programs | Array | Malloc | sizeof

How to write  C Programs | Array | Malloc | sizeof

#include<stdio.h>
#include<conio.h>
void main()
{
int *a,i,n;
clrscr();
printf("enter the no of elements");
scanf("%d",&n);
a=(int *)malloc(n*sizeof (int));
printf("enter array elements");
for(i=0;i<n;i++)
{
printf("a[%d]=",i);
scanf("%d",a+i);
}
printf("array elements is\n");
for(i=0;i<n;i++)
printf("%5d",*(a+i));
getch();
}

Switch Case Statement in c language

Switch Case Statement in c language
 printf("Enter a no:\n");
scanf("%d",&n);
switch(n)
{
case 1 : printf("One");
break;
case 2 : printf("Two");
break;
case 3 : printf("Three");
break;
case 4 : printf("Four");
break;
case 5 : printf("Five");
break;
case 6 : printf("Six");
break;
case 7 : printf("Seven");
break;
case 8 : printf("Eight");
break;
case 9 : printf("Nine");
break;
default : printf("wrong no");
break;
}
getch();
}

Pattern Tutorials in c language

 Pattern Tutorials in c language
C |  Pattern Tutorials | with outputs
#include<stdio.h>
#include<conio.h>
void main()
{
int i,l,p=1;
clrscr();
for(i=1;i<=5;i++)
{
if(i%2!=0)
{
for(l=1;l<=5;l++)
printf("%4d",l*i);
}
else
{
for(l=1;l<=5;l++)
printf("%4d",l*i);
}
printf("\n");
}
getch();
}
write this code in Turbo c and see what will be output
#include<stdio.h>
#include<conio.h>
void main()
{
int q,i,j,s,t=9,p;
clrscr();
for(i=1;i<=5;i++)
{
for(q=1;q<=32;q++)
printf(" ");
for(j=i;j<5;j++)
printf(" ");
for(s=t,p=1;s<=9;s++,p++)
printf("%d",p);
printf("\n");
t-=2;
}
t=3;
for(i=4;i>=1;i--)
{
for(q=1;q<=32;q++)
printf(" ");
for(j=i;j<5;j++)
printf(" ");
for(s=t,p=1;s<=9;s++,p++)
printf("%d",p);
printf("\n");
t+=2;
}

getch();
}

C | File Handling programs | Free Source Code | comments

 C | File Handling programs | Free Source Code | comments
C | File Handling  programs | Free Source Code | comments write the simple program for
file handling
#include<stdio.h>//header files//
void main()     
{                       ///start of main//

FILE *f1,*f2,*f3;     //pointers//
char c;
clrscr();                  // clear the screen//

printf("input text");
f1=fopen("cmb.txt","w");   //open file//
f2=fopen("cmb01.txt","w");
f3=fopen("cmb02.txt","a");
while((c=getchar()) != EOF)
putc(c,f1);                   //copy from one to another//
f1=fopen("cmb.txt","r");
while((c=getc(f1)) !=EOF)
printf("%c",c);                    
fclose(f1);
while((c=getchar()) != EOF) // use while loop for condition//
putc(c,f2);
fclose(f2);
f2=fopen("cmb01.txt","r");
while((c=getc(f2)) !=EOF)   //till not end of file//
printf("%c",c);                  // print all;l the data//

fclose(f1);                 //close the file//
fclose(f2);

file using file program in c

 file using file program in c
with the help of pointer this program shows to open abc.txt
file using file program in c
#include<stdio.h>      // HEADER FILES//
#include<conio.h>
main()                  //main start//
{
FILE * fp;                                 
char ch;
clrscr();
fp=fopen("abc.txt","r");
if(fp==NULL)            //if file is null//
{
printf("error");
getch();
}
while(1)
{
ch=fgetc(fp);
if(ch==EOF)
break;
printf("%c",ch);
}
fclose(fp);
getch();
}

C command line program code using argv using for loopExample

 C command line program code using argv using for loopExample
 C command line program code using argv  using for loop 
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main(int argc,char*argv[])
{
 int i,p=1;
// clrscr();
 if(argc!=3)
 {
 printf("\nerror");
 exit(1);
 }
 for(i=1;i<=atoi(argv[2]);i++);
  {
  p=p*atoi(argv[1]);
  printf("%d",p);

  }
  getch();
 }
examples for C command line program code using argv  using for loop

C command line program code using argv Example

 C command line program code using argv Example
c command line program code using argv
#include<stdio.h>
void main( int arge,char *argv[])
{
 FILE * fp;
 int i;
 char word[15];
 fp=fopen(argv[1],"w");
 printf("\nNo.of argument in command line=%d\n\n",arge);
 for(i=2;i<arge;i++)
 fprintf(fp,"%s",argv[i]);
 fclose(fp);
 printf("contents of %s file\n\n",argv[1]);
 fp=fopen(argv[1],"r");
 for(i=2;i<arge;i++);
{
 fscanf(fp,"%s",word);
 printf("%s",word);
 }
 fclose(fp);
 printf("\n\n");
 for(i=0;i<arge;i++)
 printf("%*s \n",i*5,argv[i]);
 }
Example for:-c command line program code using argv

C file programs using array command line

 C file programs using array command line
C file programs  using array command line
#include<stdio.h>
#include<conio.h>
void main(int argc,char*argv[])
{
 FILE *filep;
 int i;
 clrscr();
 printf("\nNumber of argument on command line =%d\n",argc);
 if(!argc<2)
 {
 printf("Arguments on the command line:\n");
 for(i=0;i<=argc;i++)
 printf("argv[%d]=%s\n",i,argv[i]);
 }
 else
 {
  printf("\nNo parameter on the command line");
  }
  getch();
 }
programming code for C file programs  using array command line

C command line program code using argv if -else loop

 C command line program code using argv if -else loop
C command line program code using argv if -else loop
#include<stdio.h>
#include<conio.h>
void main(int argc,char*argv[])
{
 FILE *fp;
 char s[100];
 clrscr();
 printf("\nNumber of argument on command line =%d\n",argc);
 if(argc<2)
 {
 printf("forget the file name\n");
  }
 else
 {
  printf("\nNo parameter on the command line");
  }
  getch();
 }
Examples for C command line program code using argv if -else loop

C program code | file handling with fopen using while loop

 C program code | file handling with fopen using while loop
C program code     | file handling  with fopen using while loop
#include<stdio.h>
void main()
{
 FILE *f1,*f2,*f3;
 char c;
 clrscr();
 printf("input text");
 f1=fopen("cmb.txt","w");
 f2=fopen("cmb01.txt","w");
 f3=fopen("cmb02.txt","a");
 while((c=getchar()) != EOF)
 putc(c,f1);
 f1=fopen("cmb.txt","r");
 while((c=getc(f1)) !=EOF)
 printf("%c",c);
 fclose(f1);
 while((c=getchar()) != EOF)
 putc(c,f2);
 fclose(f2);
 f2=fopen("cmb01.txt","r");
 while((c=getc(f2)) !=EOF)
 printf("%c",c);
 fclose(f1);
 fclose(f2);

 }
programming example for C program code     | file handling  with fopen using while loop

C program for file code ,fputs(),fflush,strcpy,strcmp using for loop sort alphabetically Float datatype in c , Example

C program for file code ,fputs(),fflush,strcpy,strcmp using for loop sort alphabetically Float datatype in c , Example
   c program for file code ,fputs(),fflush,strcpy,strcmp using for loop
#include<stdio.h>
void main()
{
 FILE *f,*q;
 char any;
 char nm[20],t[20];
 char m[20][20];
 int i,j,n;
 clrscr();
 f=fopen("e:\names.txt","w");
 do
 {
  fflush(stdin);
  printf("\n Enter a name");
  gets(nm);
  fputs(nm,f);
  fputs("\n",f);
  fflush(stdin);
  any=getchar();
  }while(any=='y');
  fclose(f);
  f=fopen("e:\names.txt","r");
  q=fopen("e:\sorted.txt","w");
  n=0;
  while(!feof(f))
  {
   fgets(nm,20,f);
   if(feof(f))
   break;
   strcpy(m[n],nm);
   n++;
   }
   fclose(f);
   printf("\n list of students");
   for(j=0;j<1;j++)
   puts(m[j]);
   for(i=0;i<n-1;i++)
   {
   for(j=i+1;j<n;j++)
   {
    if(strcmp(m[i],m[j]>0) )
    {
     strcpy(t,m[i]);
     strcpy(m[i],m[j]);
     strcpy(m[j],t);
     }
     }
     }
     printf("\n sorted alphabetically...\n");
     for(j=0;j<n;j++)
     {
      puts(m[j]);
      fputs(m[j],q);
      }
      fclose(q);
      getch();
      }

Heap Sort for sorting array in ascending Order

 Heap Sort for sorting array in ascending Order
Program for sorting an array in ascending order using heap sort
#include<conio.h>
#include<stdio.h>
void create(int[]);
void down_adjust(int[],int);
void main()
{
int heap[30],n,i,last,temp;
printf("\nEnter no. of elements :");
scanf("%d",&n);
printf("\nEnter the data to be sort :");
for(i=1;i<=n;i++)
scanf("%d",&heap[i]);
//create a heap
heap[0]=n;
create(heap);
//sorting
while(heap[0] > 1)
{
//swap heap[1] and heap[last]
last=heap[0];
temp=heap[1];
heap[1]=heap[last];
heap[last]=temp;
heap[0]--;
down_adjust(heap,1);
}
//print sorted data
printf("\nsorted data is :");
for(i=1;i<=n;i++)
printf("%d\t",heap[i]);
}
void create(int heap[])
{
int i,n;n=heap[0]; //no. of elements
for(i=n/2;i>=1;i--)
down_adjust(heap,i);
}
void down_adjust(int heap[],int i)
{
int j,temp,n,flag=1;
n=heap[0];
while(2*i<=n && flag==1)
{
j=2*i;  //j points to left child
if(j+1<=n && heap[j+1] > heap[j])
j=j+1;
if(heap[i] > heap[j])
flag=0;
else
{
temp=heap[i];
heap[i]=heap[j];
heap[j]=temp;
i=j;
}
}
}

sorted data is :20      67      96*/