the dice-rolling program to also print a histogram.


#include
#include

int main()
{
int i;
int d1, d2;
int a[13];

for(i = 2; i <= 12; i = i + 1)
a[i] = 0;
for(i = 0; i < 100; i = i + 1)
{
d1 = rand() % 6 + 1;
d2 = rand() % 6 + 1;
a[d1 + d2] = a[d1 + d2] + 1;
}
for(i = 2; i <= 12; i = i + 1)
{
printf("%d: %d\t", i, a[i]);
printnchars('*', a[i]);
printf("\n");
}
return 0;
}

Related Links :

Program to use square() function and use it to print the squares of the numbers 1-10.



The squaring function is quite simple:

int square(int x)
{
return x * x;
}

Here is a loop and main program to call it:

#include

int square(int);

int main()
{
int i;

for(i = 1; i <= 10; i = i + 1)
printf("%d %d\n", i, square(i));
return 0;
}

Related Links :

Program to print the first 10 Fibonacci numbers.



#include

int main()
{
int i;
int fibonacci = 1;
int prevfib = 0;
int tmp;

for(i = 1; i <= 10; i = i + 1)
{
printf("%d %d\n", i, fibonacci);
tmp = fibonacci;
fibonacci = fibonacci + prevfib;
prevfib = tmp;
}
return 0;
}

Related Links :

Program to print the first 7 positive integers and their factorials.



#include

int main()
{
int i;
int factorial = 1;

for(i = 1; i <= 7; i = i + 1)
{
factorial = factorial * i;
printf("%d %d\n", i, factorial);
}
return 0;
}

Related Links :

program to compute the average of the ten numbers



#include

int main()
{
int i;

for(i = 1; i <= 10; i = i + 1)
{
if(i % 2 == 0)
printf("%d is even\n", i);
else
printf("%d is odd\n", i);
}
return 0;
}

Related Links :

program to find out how many of the numbers from 1 to 10 are greater than 3



#include

int main()
{
int i;
int count = 0;

for(i = 1; i <= 10; i = i + 1)
{ if(i > 3)
count = count + 1;
}

printf("%d numbers were greater than 3\n", count);

return 0;
}




Related Links :

program to print a simple triangle of asterisks.


#include

int main()
{
int i, j;

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

Related Links :

program to print the numbers from 1 to 10 and their squares.



#include

int main()
{
int i;


for(i = 1; i <= 10; i = i + 1)
{
printf("%d %d\n", i, i * i);
}
return 0;
}

Related Links :

Write a function to determine whether the year is a leap year or not.


#include
main()
{
int leap_year(year);
int year, lp;
printf("Enter the year:");
scanf ("%d", &year);
lp=leap_year(year);
if (lp)
{
printf("\nThe entered year is a leap year.");
}
else
{
printf("\nThe entered year is not a leap year.");
}
}

leap_year(int y)
{
int lp;
if (y%4==0)
{
lp=1;
}
else
lp=0;
return(lp);
}

Related Links :

Program to convert any given year into its roman equivalent by Function.



#include
main()
{
int year;
int convert (int year);

{

printf("Note:Enter a four year digit year.\n\n");
printf("Enter the year that you wanna convert to Roman: " );
scanf ("%d", &year);
if (year> 1999)

{
printf("Invalid Year.Please enter again.\n\n");
}
}

convert(year);
}


convert(int year)
{
int i;
printf("\nYear converted to Roman:");

i=(year/1000); //thousands place
if(i==1)
{
printf("m");
}
i=((year/100)%10); //hundreds place
switch (i)
{
case 1:
printf("c");

break;

case 2:
printf("cc");

break;

case 3:
printf("ccc");

break;

case 4:
printf("cd");

break;

case 5:
printf("d");

break;

case 6:
printf("dc");

break;

case 7:
printf("dcc");

break;

case 8:
printf("dccc");

break;

case 9:
printf("dcccc"); //this part you may think is wrong..9 -> cm

break; //but i have taken a hint from the example in the question.

}



i=((year/10)%10); //tens place

switch(i)
{
case 1:
printf("x");

break;

case 2:
printf("xx");

break;

case 3:
printf("xxx");

break;

case 4:
printf("xl");

break;

case 5:
printf("l");

break;

case 6:
printf("lx");

break;

case 7:
printf("lxx");

break;

case 8:
printf("lxxx");

break;

case 9:
printf("lxxxx"); //had it not been for this example, it would have been xc
break;
}



i=year%10; //ones place

switch(i)
{
case 1:
printf("i");
break;

case 2:
printf("ii");
break;

case 3:
printf("iii");
break;

case 4:
printf("iv");
break;

case 5:
printf("v");
break;

case 6:
printf("vi");
break;

case 7:
printf("vii");
break;

case 8:
printf("viii");
break;

case 9:
printf("ix");
break;
}
printf ("\n\n");
return 0;
}




Related Links :

Program to find A to the power of B using Functions

Program to find A to the power of B using Functions



#include
main()
{
int power (a,b);
int a, b, result;
printf("Enter the value of a and b:");
scanf ("%d %d", &a, &b);
result=power(a,b);
printf("%d raised to %d is %d", a, b, result);
}

power (int a, int b)
{
int calculation=1, calc;
for (calc=1; calc <=b; calc++) { calculation=calculation*a; continue; } return(calculation); }

Related Links :

Program to find Prime Factors using Functions.

Program to find Prime Factors using Functions.



#include
main()
{
int number;
int prime(int number);
int primefactor(int number);
printf("Enter the number whose prime factors are to be calculated:");
scanf ("%d", &number);
primefactor(number);
}

//The following is the function that detects a Prime number.

prime(int num)
{
int i, ifprime;
for (i=2; i<=num-1; i++)
{
if (num%i==0)
{
ifprime=0;
}
else
ifprime=1;
}
return
(ifprime);
} //The following function prints the prime factors of a number.

primefactor(int num)
{
int factor,
ifprime;
for (factor=2; factor<=num;)
{ prime(factor);
//so that the factors are only prime and nothing else.
if (ifprime)
{ if (num%factor==0) //diving by all the prime numbers less than the number itself.
{
printf("%d ", factor);
num=num/factor; continue;
}
else
{ factor++;//this cannot be made a part of the for loop
}
}
}
return 0;
}




Related Links :

Program to Find Area and Circumference of a Circle by using Pointers

Program calculating the Area and Circumference of a Circle
by using Pointers.





#include
main()
{
int radius;
float area, perimeter;
printf("\nEnter radius of a circle:");
scanf ("%d", &radius);
areaperi (radius, &area, &perimeter);
printf("Area=%f", area);
printf("\nPerimeter=%f", perimeter);
}


areaperi(int r, float *a, float *p)
{
*a=3.14*r*r;
*p=2*3.14*r;
}




Related Links :

Program to find Product of Two Numbers using Function by Returns a Float

Program to find Product of Two Numbers using Function by Returns a Float.




#include
main()
{

int i;
float j, prod;
float product (int x, float y);

printf("Enter the i(int) and j(float):");
scanf ("%d %f", &i, &j);
prod = product(i,j);
printf("Product:%f", prod);

}


product (int x, float y)
{
float product;
product = x*y;
return (product);
}



Related Links :

Program to show Calculation of Sum, Average and Standard Deviation using Functions and Pointers.

Program to show Calculation of Sum, Average and Standard Deviation using Functions and Pointers.


#include
#include
int calc (float a, float b, float c, float d, float e, float *sum, float *avg,float *sd);
int main()
{
float a, b, c, d, e, sum=0.0, avg=0.0;
float sd=0.0;
printf("Enter Five Numbers:");
scanf("%f %f %f %f %f",&a,&b,&c,&d,&e);
calc (a, b, c, d, e, ∑, &avg, &sd);
printf("\nSum=%f", sum);
printf("\nAverage=%f", avg);
printf("\nStandard Deviation=%f\n", sd);
getchar();
return 0;
}
calc (float a, float b, float c, float d, float e, float *sum, float *avg, float *sd)
{
float Calc=0.0;
*sum = a+b+c+d+e;
*avg = *sum / 5.0;
Calc += ( a - *avg) * ( a - *avg);
Calc += ( b - *avg) * ( b - *avg);
Calc += ( c - *avg) * ( c - *avg);
Calc += ( d - *avg) * ( d - *avg);
Calc += ( e - *avg) * ( e - *avg);
*sd = sqrt((double)Calc/5.0);
}



Related Links :

C Program to Calculate Factorial of a number using Recursion Function


#include
main()
{
int a, fact;
printf("\nEnter any number: ");
scanf ("%d", &a);
fact=rec (a);
printf("\nFactorial Value = %d", fact);
}
rec (int x)
{
int f;
if (x==1)
return (1);
else
f=x*rec(x-1);
return (f);
}

Related Links :

Use of the strlen string in C++

#include"iostream.h"
#include"conio.h"
#include"string.h"
void main()
{
clrscr();
char str[5];
int length;
cout<<"enter the string:";
cin>>str;
length=strlen(str);
cout<<"the length of the string is:";
cout<<" "<
getch();
}

Related Links :

PROGRAM FOR A SELECTION SORT

// PROGRAM FOR A SELECTION SORT;
#include "iostream.h"
#include "conio.h"
class selection
{
int i,data[100],size,j,t;
public:
void get();
void sort();
void show();
};
void selection::get()
{
cout<<"\n\nENTER THE SIZE OF ARRAY:"; cin>>size;//from keybord
for(i=0;i<=size-1;i++) { cout<<"\n\nenter el:"; cin>>data[i];//from keybord
}
}
void selection::sort()
{
for(i=0;i<=size-2;i++) for(j=i+1;j<=size-1;j++) { if(data[i]>data[j])
{
t=data[i];
data[i]=data[j];
data[j]=t;
}
}
}
void selection::show()
{
cout<<"\n\n THE SERIES IS :"; for(i=0;i<=size-1;i++) { cout<<" "<<<"\n\nAFTER SORTING :"; s.show(); getch(); } //*/*/*/*/*/*/*/*/*/*/*/*/*//**/*/*/*/*/*/*/*/***/*/*/*/*/*/* /* EXPLANATION FOR THE 'FOR LOOP'; i.e for(i=0;i<=size-2;i++) // for how many iterations for(j=i+1;j<=size-1;j++) if(data[i]>data[j])
{
t=data[i];
data[i]=data[j];
data[j]=t;
}


THIS IS EXPLAIN AS GIVEN BELOW.....
let us take example that the series is
55 22 44 11 33
HERE ONE THING IS NOTABLE THAT THE INDEX OF
'55' is '0'
'22' is '1'
'44' is '2'
'11' is '3'
'33' is '4'
when i=0; 'j' will be 1 and we know that in case
of two loop the second loop will execute first.
now the size of the series is 5 therefore 'j' will
goes from 1 to 4. In the series here 'i' is 55 and
'j' is 22 44 11 33. Second for loop execute first . i.e is
it check data[55]>data[22] . if it is true then will
interchange otherwise as it is .Again when loop round
i.e when 'j' become 2 it check again data[22]>data[44],
because 'i' becomes 22 as the condition was true .
Here as the condition is false , there will be no
change .Again loop round and 'j' become 3.It check
data[22]>data[11],as it is true there will a change.
Now at last when 'j' becomes 4 it check data[22]>data[33],
it is false therefore here will be no change.
Now when j becomes 5 secomd loop
will exit as the condition is false for 'for loop';
Now 'i' becomes 1 and 'j' will be 2. Here in second execution
'j' will goes from 2 to 4; NOW THE SERIES IS 11 55 44 22 33
As the index of 55 is 1 therefore here 'i' will be 55 and
'j'would be 44 22 33 .In this csse data[55]>data[44],
as the condition is true there will be change .Now the series
is 11 44 55 22 33 .Next data[44]>data[22] ,it is true i.e is
the series is 11 22 55 44 33 .Now data[22]>data[33] as it
false ,therefore there will no change . Now it exit from
second loop again. Now 'i' will be 55 and 'j' are 44 33.
It check data[55]>data[44] it is true therefore series is
11 22 44 55 33.Loop round and check data[44]>data[33]
it is true .Now the series is 11 22 33 55 44.Again it will
exit from second loop. Now 'i'is 3 in last round and the
55 is at third index therefore 'i' will be 55 and 'j' becomes
44.now it check data[55]>data[44]. As it is true there will
be change ,the series is 11 22 33 44 55. It will exit from
second loop .Now 'i' is 4 BUT condition is(i=0;i<=size-2;i++) therefore it is false and execution will stop here.THE GIVEN SERIES IS SORTED SERIES . */

Related Links :

PROGRAM FOR INESERTION SORT

//PROGRAM FOR INESERTION SORT;
#include
#include
class insert
{
int A[100],n,i,j,t,k;
public:
void get();
void show();
void sort();
};
void insert::get()
{
cout<<"ENTER THE SIZE OF ARRAY:";
cin>>n;
for(i=0;i<=n-1;i++)
{
cout<<"\n\nenter el:\t";
cin>>A[i];
}
}
void insert::show()
{
cout<<"\n\nthe series is:";
for(i=0;i<=n-1;i++)
cout< }
void insert::sort()
{
for(i=0;i<=n-2;i++) //for iteration's
{
if(A[i]>A[i+1])
{
t=A[i+1];
for(j=0;j<=i;j++)
{
if(t {
for(k=i+1;k>=j+1;k--)
{
A[k]=A[k-1];
}
A[j]=t;
break;
}
}
}
}
}
void main()
{
clrscr();
insert s;
s.get();
s.show();
cout<<"\n\nAFTER SORTING:";
s.sort();
s.show();
getch();
}

Related Links :

Program to cnvert infix to postfix

//program to cnvert infix to postfix
#include"iostream.h"
#include"conio.h"
#include"string.h"
#include"ctype.h"
class infix
{
int stack[10],target[10],top;
char *s,*t;
public:
void setexpr(char *str);
void push(int c);
void pop();
void convert();
void priority(char x);
} ;
void infix::setexpr(char *str)
{
s=str;
}
void infix::push(int c)
{
if(top==10)
{
cout<<"stack is full:";

}
else
{
top++;
stack[top]=c;
}
}
void infix::pop()
{
if(top==-1)
{
cout<<"stack is empty:";

}
else
{
int item=stack[top];
top--;
}
}
void infix::convert()
{
while(*s)
{
if(*s==' '||*s=='\t')
{
s++;
continue;
}
if(isalpha(*s) || isdigit(*s))
{
while(isalpha(*s) || isdigit(*s))
{
*t=*s;
s++;
t++;
}
}
if(*s=='(')
{
push(*s);
s++;
}
if(*s=='+'||*s=='-'||*s=='*'||*s=='/'||*s=='$'
||*s=='%')
{
if(top!=-1)
{
opr=pop();
if(opr!='(')
{
while(priority(opr) >= priority(*s))
{
*t=opr;
t++;
opr=pop();
}
push(opr);
push(*s);
s++;
}
else
{
push(*t);
t++;
}
}
}
void infix::priority(char x)
{
if(x=='+'||x=='-')
{
return 1;
}
if(x=='$')
{
return 3;
}
if(x=='/'|| x=='*')
{
return 2;
}
else
{
return 0;
}
}

void infix::show()
{
cout<
}
void main()
{
clrscr();
char arr[10];
cout<<"enter the infix experesion:";
cin>>arr;
infix i;
i.setexpr(arr);
i.convert();
i.show();
getch();
}

Related Links :

Program to cnvert infix to postfix

//program to cnvert infix to postfix
#include"iostream.h"
#include"conio.h"
#include"string.h"
#include"ctype.h"
class infix
{
int stack[10],target[10],top;
char *s,*t;
public:
void setexpr(char *str);
void push(int c);
void pop();
void convert();
void priority(char x);
} ;
void infix::setexpr(char *str)
{
s=str;
}
void infix::push(int c)
{
if(top==10)
{
cout<<"stack is full:";

}
else
{
top++;
stack[top]=c;
}
}
void infix::pop()
{
if(top==-1)
{
cout<<"stack is empty:";

}
else
{
int item=stack[top];
top--;
}
}
void infix::convert()
{
while(*s)
{
if(*s==' '||*s=='\t')
{
s++;
continue;
}
if(isalpha(*s) || isdigit(*s))
{
while(isalpha(*s) || isdigit(*s))
{
*t=*s;
s++;
t++;
}
}
if(*s=='(')
{
push(*s);
s++;
}
if(*s=='+'||*s=='-'||*s=='*'||*s=='/'||*s=='$'
||*s=='%')
{
if(top!=-1)
{
opr=pop();
if(opr!='(')
{
while(priority(opr) >= priority(*s))
{
*t=opr;
t++;
opr=pop();
}
push(opr);
push(*s);
s++;
}
else
{
push(*t);
t++;
}
}
}
void infix::priority(char x)
{
if(x=='+'||x=='-')
{
return 1;
}
if(x=='$')
{
return 3;
}
if(x=='/'|| x=='*')
{
return 2;
}
else
{
return 0;
}
}

void infix::show()
{
cout<
}
void main()
{
clrscr();
char arr[10];
cout<<"enter the infix experesion:";
cin>>arr;
infix i;
i.setexpr(arr);
i.convert();
i.show();
getch();
}

Related Links :

Program for inserstion sort using while loop

//program for inserstion sort using while loop;

#include "iostream.h"
#include "conio.h"
class anand
{
int a[10],n,i,j,temp;
public:
void getdata();
void showdata();
void calculate();
};
void anand::getdata()
{
cout<<"ENTER THE SIZE OF ARRAY:";
cin>>n;

for(i=0;i<=n-1;i++)
{
cout<<"\n\nENTER EL:"; cin>>a[i];

}
}
void anand::showdata()
{
cout<<"\nAFTER SORTING THE SERIES IS:"<<" \n\n";

for(i=0;i<=n-1;i++)
cout<<" "<
=0) //inner loop
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
j--;
}

else
break;
} // end of inner while loop
I++;
}
}

void main()
{
clrscr();
anand x;
x.getdata();
x.calculate();
x.showdata();
getch();
}

Related Links :

Program for generating a sequence

#include "stdio.h"
#include "conio.h"

void main()
{
int n,f=0;
clrscr();
printf("\n\n\n\t\t\t Program for generating a sequence");
printf("\n\t\t\t *********************************");
printf("\n\n\n\t\t\t Enter any no. = ");
scanf("%d",&n);
if(n<1)
{
printf("\n\n\t\t\t Error !");
exit(0);
}
else
{
while(n!=1)
{
if(n%2==1)
{
n=n*3+1;
f++;
}
else
{
n=n/2;
f++;
}

printf("\n\n\t\t\t Next value %d",n);
}

printf("\n\n\t\t\t Final value %d, Operation performed %d",n,f);

}
getch();

}

Related Links :


If you face any Problem in viewing code such as Incomplete "For Loops" or "Incorrect greater than or smaller" than equal to signs then please collect from My Web Site CLICK HERE


More Useful Topics...

 

History Of C..

In the beginning was Charles Babbage and his Analytical Engine, a machine
he built in 1822 that could be programmed to carry out different computations.
Move forward more than 100 years, where the U.S. government in
1942 used concepts from Babbage’s engine to create the ENIAC, the first
modern computer.
Meanwhile, over at the AT&T Bell Labs, in 1972 Dennis Ritchie was working
with two languages: B (for Bell) and BCPL (Basic Combined Programming
Language). Inspired by Pascal, Mr. Ritchie developed the C programming
language.

My 1st Program...


#include
#include
void main ()
{
clrscr ();
printf ("\n\n\n\n");
printf ("\t\t\t*******Pankaj *******\n");
printf ("\t\t\t********************************\n");
printf ("\t\t\t\"Life is Good...\"\n");
printf ("\t\t\t********************************");
getch ();
}

Next Step...


#include
#include

void main ()
{
clrscr ();
printf ("\n\n\n\n\n\n\n\n");
printf ("\t\t\t --------------------------- \n\n");

printf ("\t\t\t | IGCT, Info Computers, INDIA | \n\n");
printf ("\t\t\t --------------------------- ");

getch ();

}

Hits!!!