#include
#include
int main()
{
char string[80]; // Has 79 usable elements
int pos, num_chars;
cout << "Enter a string for the character array: ";
cin.get(string,80,'\n');
cout << "How many characters do you want to extract? ";
cin >> num_chars;
for (pos = 0; pos < num_chars; pos++)
cout << string[pos];
cout << '\n';
return 0;
}
C++ Strpose Function example.
C++ Program to Find Even Odd number
#include
int main()
{
int your_number;
cout << "Enter a whole number: ";
cin >> your_number;
if (your_number % 2 == 0)
cout << "\nYour number is even\n";
if (your_number % 2 != 0)
{
cout << "Your number is odd.\n";
}
cout << "That's all!\n";
return 0;
}
C++ Program to Print ASCII Value of Given Char
#include
#include
int main()
{
int one_char;
cout << "\nEnter a character: ";
one_char = getch();
cout << "\nThe character you entered was "
<< (char) one_char << '\n';
cout << " Its ASCII value is "<< (int) one_char << '\n';
return 0;
}
draw a interesting picture using line() in C++ Graphics
#include
#include
#include
void main(void)
{
int driver = DETECT,mode;
int x[10],y[10];
int x_center = 360, y_center = 180, rad = 100;
int i,j;
initgraph(&driver,&mode,"c:\\tc\\bgi");
for ( i = 0; i < 10; i++ )
{
x[i] = x_center + rad * cos(36*i*3.14159/180);
y[i] = y_center + rad * sin(36*i*3.14159/180);
}
for ( i = 0; i < 10; i++ )
for ( j = 0; j < 10; j++ )
line(x[i],y[i],x[j],y[j]);
getch(); /* press any key return to TEXT mode */
closegraph();
}
C++ program to sort a range of numbers with Insertion and Quicksort, check their sorting time and prompt the result on the screen
#include
#include
#include
#include
#include
#include
#include
/* define variable */
const int max=29000;
int list[max];
FILE *fp;
clock_t start,end;
char any1[8];
/* Insertion sort module */
void insertion(int min1,int max1)
{
int a,b,v;
for(a=min1;a<=max1;a++)
{
v = list[a];
b = a;
do
{
list[b] = list[b-1];
b = b - 1;
} while(list[b-1] > v);
list[b] = v;
}
}
/* sort partitioning element */
void sorthree(int x,int y,int z)
{
int temp;
if (list[x] > list[y])
{
temp = list[x];
list[x] = list[y];
list[y] = temp;
}
if (list[z] < list[x])
{
temp = list[x];
list[x] = list[z];
list[z] = temp;
temp = list[y];
list[y] = list[z];
list[z] = temp;
}
if ((list[z] > list[x]) && (list[z] < list[y]))
{
temp = list[y];
list[y] = list[z];
list[z] = temp;
}
}
/* Quicksort module */
void quicksort(int min2,int max2)
{
int m,v,t,i,j,q;
if ((max2-min2) > 9)
{
m = (max2-min2+1)/2;
sorthree(min2,m,max2);
max2=max2-1;
q = list[m];
list[m] = list[max2];
list[max2] = q;
v = list[max2];
i = min2+1;
j = max2-1;
do
{
do
{
i=i+1;
} while (list[i] < v);
do
{
j=j-1;
} while (list[j] > v);
t = list[i];
list[i] = list[j];
list[j] = t;
} while (i<=j);
list[j]=list[i];
list[i]=list[max2];
list[max2]=t;
quicksort(min2,i-1);
quicksort(i+1,max2);
}
else
insertion(m,max2);
}
/* main program */
void main()
{
int i,j,k,min,max1;
char any2[8];
clrscr();
cout << "Enter a file name to store data :";
cin >> any1; /* input data file name on */
cout << '\n' << "Generating file...waits\n\n";/* screen */
fp = fopen(any1,"w");
for(j=0;j<=max/200;j++)
{
for(i=0;i<=200;i++) /* write random values to file */
{
k = rand();
fprintf(fp,"%d\n",k);
}
}
fclose(fp);
fp = fopen(any1,"r");
i = 0;
while(fscanf(fp,"%d\n",&k) != EOF)
{
list[i] = k; /* read values from file and assign to an array */
i = i + 1;
}
fclose(fp);
min = 0;
max1 = max;
max1=max1-1;
cout << "Sorting with Quicksort... waits" << '\n';
start = clock();
quicksort(min,max1); /* sort an unsorted list with quicksort */
end=clock();
float result = (end-start)/CLK_TCK;
cout << "The time needs to sort " << max
<< " numbers in Quciksort is : " << result << " second(s)" << "\n\n";
cout << "Enter an output file for Quicksort : ";
cin >> any2;
fp = fopen(any2,"w");
for(i=0;i<=max;i++)
{ /* write the output from quicksort and put them */
k = list[i]; /*to a file */
fprintf(fp,"%d\n",k);
}
fclose(fp);
fp = fopen(any1,"r");
i = 0;
while(fscanf(fp,"%d\n",&k) != EOF)
{
list[i] = k;
i = i + 1;
}
fclose(fp);
cout << "\nSorting with Insertion Sort... waits" << '\n';
start = clock();
insertion(0,max); /* sort an unsorted list with insertion sort */
end=clock();
result = (end-start)/CLK_TCK;
cout << "The time needs to sort " << max
<< " numbers in Insertion is : " << result << " second(s)" << "\n\n";
cout << "Sort an already sorted array again with Quicksort..." << '\n';
min = 0;
max1 = max;
max1=max1-1;
start = clock();
quicksort(min,max1); /* sort an already sort list with quicksort */
end=clock();
result = (end-start)/CLK_TCK;
cout << "The time needs to sort " << max
<< " numbers in Quicksort is : " << result << " second(s)" << "\n";
cout << "Sort an already sorted array again with Insertion sort..." << '\n';
start = clock();
insertion(0,max); /* sort an already list with insertion sort */
end=clock();
result = (end-start)/CLK_TCK;
cout << "The time needs to sort " << max
<< " numbers in Insertion sort is : " << result << " second(s)" << '\n';
}
C Program to create Random Numbers (CPP)
#include
#include
#include
#include
int main(void)
{
int i,j;
for(j=0;j<150;j++)
{
// randomize();
for(i=0;i<200;i++)
printf("%d\n", rand() % MAXINT);
}
return 0;
}
C Program to find Given Character is vowel Or consonant by using switch case
C Program to find Given Character is vowel Or consonant
/* wap to determine whether the entered chracter is consonant or vowel*/
#include
main()
{
char ch;
clrscr();
printf("\n Enter any character:");
scanf("%c",&ch);
switch(tolower(ch))
{
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
printf("\n The character is vowel!");
break;
default :
printf("\n The chracter is consonant!");
}
getch();
}
C Assignment to calculate total percentage in c by using structures
C Assignment to calculate total percentage in c by using structures
#include
#include
struct student
{
char name[20];
int age;
int RollNo;
int marks[6];
int total;
float per;
};
void setmarks(struct student *);
void calculate(struct student *);
void display(struct student *);
void main()
{
struct student s1={"xyz",23,131},*ptr;
clrscr();
ptr=&s1;
printf("\n Student Name=%s",ptr->name);
printf("\n student Age=%d",ptr->age);
printf("\n student roll number=%d",ptr->RollNo);
setmarks(&s1);
calculate(&s1);
display(&s1);
getch();
}
void setmarks(struct student * m)
{
int i;
printf("\n Enter marks getting in 6 subjects.........\n");
m->total=0;
for(i=0;i<6;i++)
{
scanf("%d",&m->marks[i]);
m->total=m->total+m->marks[i];
}
}
void calculate(struct student * p)
{
p->per=p->total/600.0*100;
}
void display(struct student * d)
{
printf("\n student Name=%s",d->name);
printf("\n student age=%d",d->age);
printf("\n student roll no=%d",d->RollNo);
printf("\n student total markse=%d",d->total);
printf("\n student percentage=%f",d->per);
}
Example Of stringlen Function in C
#include
#include
#include
void main()
{
int len;
char name[15]="www.lernc.blogspot.com";
clrscr();
len=strlen(name);
printf("\n the length of name is %d",len);
getch();
}
C Program to create a Star in C
main()
{
int i,j,k;
clrscr();
for(i=1;i<=3;i++)
{
for(j=1;j<=3;j++)
{
for(k=1;k<=3;k++)
{
if(i==j||j==k||i==k)
{
continue;
}
else
{
printf("\n%d%d%d",i,j,k);
}
}
}
}
getch();
}
Selection Sorting of Given Array in c
Selection Sorting of Given Array in c
#include
#include
void main()
{
int num[10],i,j,temp;
clrscr();
printf("\n Enter 10 values of an array...........\n");
for(i=0;i<10;i++)
scanf("%d",&num[i]);
printf("\n array before sort .........\n");
for(i=0;i<10;i++)
printf("%d\t",num[i]);
for(i=0;i<9;i++)
{
for(j=i+1;j<10;j++)
{
if(num[i]<=num[j])
{
temp=num[i];
num[i]=num[j];
num[j]=temp;
}
}
}
printf("\n array after sort .........\n");
for(i=0;i<=10;i++)
printf("%d\t",num[i]);
getch();
}
C Program to print Address of Variable by using Pointers
C Program to print Address of Variable by using Pointers
Its very Basic Program for assignments of Collages.
Its very Basic Program for assignments of Collages.
#include
#include
void main()
{
int a;
float b;
clrscr();
printf("\n Address of a=%u",&a);
printf("\n Address of b=%u",&b);
getch();
}
C Program to find Given Character id Vowel or Not
C Program to find Given Character id Vowel or Not
#include
#include
main()
{
char ch;
clrscr();
printf("enter any character");
scanf("%c",&ch);
if(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u')
{
printf("%c is an vowel",ch);
}
else
{
printf("%c is not a vowel ",ch);
}
getch();
}
Basic C Program to Print Matrix by using 2 D arrays
#include
#include
void main()
{
int matrix1[2][3]={10,20,30,40,50,60},i,j;
clrscr();
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
printf("%5d",matrix1[i][j]);
printf("\n");
}
getch();
}
C Program to find sum of given series by using Function
#include
#include
long int sum_sqr(int);
main()
{
int n;
clrscr();
printf("\n Enter any number : ");
scanf("%d",&n);
printf("\n Sum of Series is %ld",sum_sqr(n));
printf("\n\n Press any key to exit. . .");
getch();
}
long int sum_sqr(int m)
{
if(m<=1)
return 1;
else
return
(m*m+sum_sqr(m-1));
}
Shell Sort Demo in C Programming
Shell Sort Demo in C Programming
#include
#include
#include
void shell(char *items, int count)
{
register int i, j, gap, k;
char x, a[5];
a[0]=9; a[1]=5; a[2]=3; a[3]=2; a[4]=1;
for(k=0; k < 5; k++)
{
gap = a[k];
for(i=gap; i < count; ++i)
{
x = items[i];
for(j=i-gap; (x < items[j])
&& (j >= 0); j=j-gap)
items[j+gap] = items[j];
items[j+gap] = x;
}
}
}
int main(void)
{
char s[255];
printf("Enter a string:");
gets(s);
shell(s, strlen(s));
printf("The sorted string is: %s.\n", s);
return 0;
}