Showing posts with label c. Show all posts
Showing posts with label c. Show all posts
Saturday, February 14, 2015
C Program to calculate Factorial of any number
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
unsigned long i,fac,n;
cout<<"Enter Number: ";
cin>>n;
for(i=1,fac=1;i<=n;++i)
{
fac=i*fac;
}
cout<<"Factorial 0f "<<n<<" is: "<<fac;
getch();
}
C program to print following trianlge using character
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr(); //to clear the screen
int i,j,k,n;
cout<<"How many lines?";
cin>>n;
for(i=0;i<n;++i)
{
cout<<"
";
for(j=0;j<i;++j)
cout<<" ";
for(k=n;k>i;--k)
cout<<"*";
}
getch(); //to stop the screen
}
Turbo C for Android How to Download and Install
If you are from India and have started learning C/C++ programming from your school days then you have definitely used Turbo C++ compiler. Still many schools, colleges and institutions in India prefer Turbo C++ for teaching students (don’t know about other countries). I do not recommend you to use Turbo C++ compiler for android because it is very outdated (about 23 years old). There is another option i.e. C4droid, it is an awesome C/C++ IDE that supports gcc compiler.


7. If everything will be done correctly then after entering last command a blue screen will open and it is Turbo C++ for Android. You can control the pointer by moving your finger on your touchscreen.
For understanding this tutorial easily I have added a video below that will help you in downloading and installing turbo c++ for android.

Also Read: Download Turbo C++ for Windows 8 for Free
So, lets come to main topic on which I am writing this article. In windows we use DosBox Emulator for running Turbo C++ for Android, in the same way we need an emulator for android platform which is known as AnDosBox. So without wasting much time lets take a look how we can use Turbo C++ for android platform.
Turbo C++ for Android - Steps to Download and Install
1. First of all download Turbo C++ for Android from link: http://sh.st/r5mBt
2. It is compressed so you need to extract it. This can be done by any compression tool like Easy Unrar. You can download it from play store for free.
3. Now after extracting you will get a folder TC and an apk file AnDosBox.
4. Install AnDosBox and move TC folder in your sd card, remember that TC folder must be in sd card not in any other subfolder.
5. Now open AnDosBox that you have already installed. It will look same as like DoxBox that is used in windows.
6. Enter below lines or commands and press enter after each line. You can get the keyboard by pressing the option button situated at left side of your device.
cd tc
cd bin
tc



7. If everything will be done correctly then after entering last command a blue screen will open and it is Turbo C++ for Android. You can control the pointer by moving your finger on your touchscreen.
For understanding this tutorial easily I have added a video below that will help you in downloading and installing turbo c++ for android.
Now just go and install it, write and run your program and share your experience with me. Feel free to ask if you are getting any problem.
Note: If you are unable to change to directory TC then move the TC folder in internal storage and try the process again.
Search Terms:
turbo c compiler for android
c compiler for android
download turbo c++ for android
c compiler for android mobile
Note: If you are unable to change to directory TC then move the TC folder in internal storage and try the process again.
Search Terms:
turbo c compiler for android
c compiler for android
download turbo c++ for android
c compiler for android mobile
Friday, February 13, 2015
C program to print the following design
#include<conio.h>
void main()
{
clrscr(); //to clear the screen
int i,j,k,n;
cout<<"How many lines?";
cin>>n;
n*=2;
for(i=0;i<n;i+=2)
{
for(j=n-2;j>i;j-=2)
cout<<" ";
for(k=0;k<=i;++k)
cout<<"*";
cout<<"
";
}
for(i=0;i<n;i+=2)
{
for(j=0;j<i;j+=2)
cout<<" ";
for(k=n-1;k>i;--k)
cout<<"*";
cout<<"
";
}
getch(); //to stop the screen
}
Thursday, February 12, 2015
C program to create a loading bar
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
void main()
{
int x=170,i,gdriver=DETECT,gmode;
initgraph(&gdriver,&gmode,"c:\tc\bgi");
settextstyle(DEFAULT_FONT,HORIZ_DIR,2);
outtextxy(170,180,"LOADING,PLEASE WAIT");
for(i=0;i<300;++i)
{
delay(30);
line(x,200,x,220);
x++;
}
getch();
closegraph();
}
Wednesday, February 11, 2015
C Program for Addition and Multiplication of Polynomial Using Arrays or Linked List
Polynomial addition, multiplication (8th degree polynomials) using arrays
#include<math.h>
#include<stdio.h>
#include<conio.h>
#define MAX 17
void init(int p[]);
void read(int p[]);
void print(int p[]);
void add(int p1[],int p2[],int p3[]);
void multiply(int p1[],int p2[],int p3[]);
/*Polynomial is stored in an array, p[i] gives coefficient of x^i .
a polynomial 3x^2 + 12x^4 will be represented as (0,0,3,0,12,0,0,....)
*/
void main()
{
int p1[MAX],p2[MAX],p3[MAX];
int option;
do
{
printf("
1 : create 1st polynomial");
printf("
2 : create 2nd polynomial");
printf("
3 : Add polynomials");
printf("
4 : Multiply polynomials");
printf("
5 : Quit");
printf("
Enter your choice :");
scanf("%d",&option);
switch(option)
{
case 1:read(p1);break;
case 2:read(p2);break;
case 3:add(p1,p2,p3);
printf("
1st polynomial -> ");
print(p1);
printf("
2nd polynomial -> ");
print(p2);
printf("
Sum = ");
print(p3);
break;
case 4:multiply(p1,p2,p3);
printf("
1st polynomial -> ");
print(p1);
printf("
2nd polynomial -> ");
print(p2);
printf("
Product = ");
print(p3);
break;
}
}while(option!=5);
}
void read(int p[])
{
int n, i, power,coeff;
init(p);
printf("
Enter number of terms :");
scanf("%d",&n);
/* read n terms */
for (i=0;i<n;i++)
{ printf("
enter a term(power coeff.)");
scanf("%d%d",&power,&coeff);
p[power]=coeff;
}
}
void print(int p[])
{
int i;
for(i=0;i<MAX;i++)
if(p[i]!=0)
printf("%dX^%d ",p[i],i);
}
void add(int p1[], int p2[], int p3[])
{
int i;
for(i=0;i<MAX;i++)
p3[i]=p1[i]+p2[i];
}
void multiply(int p1[], int p2[], int p3[])
{
int i,j;
init(p3);
for(i=0;i<MAX;i++)
for(j=0;j<MAX;j++)
p3[i+j]=p3[i+j]+p1[i]*p2[j];
}
void init(int p[])
{
int i;
for(i=0;i<MAX;i++)
p[i]=0;
}
Also Read: C Program for Addition of two Sparse Matrices
Also Read: C Program for Array Representation of Stack [Push, Pop and Display]
Polynomial addition, multiplication (8th degree polynomials) using linked list
#include<math.h>
#include<stdio.h>
#include<conio.h>
#define MAX 17
typedef struct node
{
int coeff;
struct node *next;
}node;
node * init();
void read(node *h1);
void print(node *h1);
node * add(node *h1,node *h2);
node * multiply(node *h1,node *h2);
/*Polynomial is stored in a linked list, ith node gives coefficient of x^i .
a polynomial 3x^2 + 12x^4 will be represented as (0,0,3,0,12,0,0,....)
*/
void main()
{
node *h1=NULL,*h2=NULL,*h3=NULL;
int option;
do
{
printf("
1 : create 1st polynomial");
1 : create 1st polynomial");
printf("
2 : create 2nd polynomial");
2 : create 2nd polynomial");
printf("
3 : Add polynomials");
3 : Add polynomials");
printf("
4 : Multiply polynomials");
4 : Multiply polynomials");
printf("
5 : Quit");
5 : Quit");
printf("
Enter your choice :");
Enter your choice :");
scanf("%d",&option);
switch(option)
{
case 1:h1=init();read(h1);break;
case 2:h2=init();read(h2);break;
case 3:h3=add(h1,h2);
printf("
1st polynomial -> ");
1st polynomial -> ");
print(h1);
printf("
2nd polynomial -> ");
2nd polynomial -> ");
print(h2);
printf("
Sum = ");
Sum = ");
print(h3);
break;
case 4:h3=multiply(h1,h2);
printf("
1st polynomial -> ");
1st polynomial -> ");
print(h1);
printf("
2nd polynomial -> ");
2nd polynomial -> ");
print(h2);
printf("
Product = ");
Product = ");
print(h3);
break;
}
}while(option!=5);
}
void read(node *h)
{
int n,i,j,power,coeff;
node *p;
p=init();
printf("
Enter number of terms :");
Enter number of terms :");
scanf("%d",&n);
/* read n terms */
for (i=0;i<n;i++)
{ printf("
enter a term(power coeff.)");
enter a term(power coeff.)");
scanf("%d%d",&power,&coeff);
for(p=h,j=0;j<power;j++)
p=p->next;
p->coeff=coeff;
}
}
void print(node *p)
{
int i;
for(i=0;p!=NULL;i++,p=p->next)
if(p->coeff!=0)
printf("%dX^%d ",p->coeff,i);
}
node * add(node *h1, node *h2)
{
node *h3,*p;
h3=init();
p=h3;
while(h1!=NULL)
{
h3->coeff=h1->coeff+h2->coeff;
h1=h1->next;
h2=h2->next;
h3=h3->next;
}
return(p);
}
node * multiply(node *h1, node *h2)
{
node *h3,*p,*q,*r;
int i,j,k,coeff,power;
h3=init();
for(p=h1,i=0;p!=NULL;p=p->next,i++)
for(q=h2,j=0;q!=NULL;q=q->next,j++)
{
coeff=p->coeff * q->coeff;
power=i+j;
for(r=h3,k=0;k<power;k++)
r=r->next;
r->coeff=r->coeff+coeff;
}
return(h3);
}
node * init()
{
int i;
node *h=NULL,*p;
for(i=0;i<MAX;i++)
{
p=(node*)malloc(sizeof(node));
p->next=h;
p->coeff=0;
h=p;
}
return(h);
}

Tuesday, February 10, 2015
Arrays in C 2D Array Part 5
Till now I told you about the 1D arrays but today I will discuss about the 2D arrays or two dimensional arrays in c. Array is a collection of elements with similar data type. We can make arrays with any dimension. However programmers rarely go beyond 3D arrays. I will also give you an overview on 3D arrays in the subsequent tutorials. But today lets discuss the 2D arrays briefly.
int a[3][3];
This is a 2D array with 3 rows and 3 columns. The total elements in the array are 9 (3x3).
int num[3][2] = {
{43,56},
{56,54},
{65,98}
};
This is one of the simplest way of initializing a 2D array.
int num[3][2] = {43, 56, 56, 54, 65, 98};
This method will also work but it will decrease the readability of the array too.
int arr[ ][3] = { 12, 34, 23, 45, 56, 45 } ;
int num[][2] = {
{43,56},
{56,54},
{65,98}
{87,86}
};
It is optional to provide the row dimension of a 2D array if we initialize it. Remember giving column dimension is always compulsory.
Lets take one simple program to understand 2D array in C.
Output
%2B-%2BPart%2B5%2BOutptut.jpg)
Explanation
1. In the first statement I have declared the 2D array with name student. Remember 2D array also stores elements with index 00. Elements will be stored in this way.
00 01
10 11
20 21
And so on.
2. Now by using two for loops I have stored the values inside 2D array and display it on the screen.
3. Consider carefully the printf() and scanf() function with arguments “&stud[x][0], &student[x][1]”. We are storing and accessing values inside 2D array by row wise. We can also store values and access them column wise too.
Elements of first row will be stored first, after that elements of second row will be stored. This procedure will continue until the array elements ends. Given below is the memory allocation of a 2D integer array s[4][2].
%2B-%2BPart%2B5.jpg)
2D Arrays in C
As its name suggests, 2D arrays are the arrays having 2 dimensions. Those two dimensions are generally called rows and columns. 2D arrays are also called matrix.Declaration of 2D Array
A two dimensional array can be declared in following way.int a[3][3];
This is a 2D array with 3 rows and 3 columns. The total elements in the array are 9 (3x3).
Initialization of 2D Array
Similar to 1D arrays, a 2D array can also be initialize at the time of its declaration. Given below are some of the initialization methods that are used very frequently.int num[3][2] = {
{43,56},
{56,54},
{65,98}
};
This is one of the simplest way of initializing a 2D array.
int num[3][2] = {43, 56, 56, 54, 65, 98};
This method will also work but it will decrease the readability of the array too.
int arr[ ][3] = { 12, 34, 23, 45, 56, 45 } ;
int num[][2] = {
{43,56},
{56,54},
{65,98}
{87,86}
};
It is optional to provide the row dimension of a 2D array if we initialize it. Remember giving column dimension is always compulsory.
Lets take one simple program to understand 2D array in C.
#include<stdio.h>
int main()
{
int student[6][2];
int x;
for(x=0;x<6;x++)
{
printf(" Enter roll no. and marks
");
scanf("%d %d",&student[x][0],&student[x][1]);
}
for(x=0;x<6;x++)
printf("
%d %d",student[x][0],student[x][1]);
return 0;
}
Output
%2B-%2BPart%2B5%2BOutptut.jpg)
1. In the first statement I have declared the 2D array with name student. Remember 2D array also stores elements with index 00. Elements will be stored in this way.
00 01
10 11
20 21
And so on.
2. Now by using two for loops I have stored the values inside 2D array and display it on the screen.
3. Consider carefully the printf() and scanf() function with arguments “&stud[x][0], &student[x][1]”. We are storing and accessing values inside 2D array by row wise. We can also store values and access them column wise too.
Memory Allocation of 2D Array
2D arrays also stores its values similar to 1D arrays. They also store elements in contiguous memory locations.Elements of first row will be stored first, after that elements of second row will be stored. This procedure will continue until the array elements ends. Given below is the memory allocation of a 2D integer array s[4][2].
%2B-%2BPart%2B5.jpg)
GCC Compiler Download Code Blocks 12 11 a Free C C IDE

- Download Now -
Also Read: Download Turbo C++ for windows xp for free
Also Read: Download Turbo C++ for windows 7 for free
Source: http://www.codeblocks.org/
Subscribe to:
Posts (Atom)