c++ programs examples for beginners

c++ programs examples for beginners

c++ programs examples for beginners

c++ programs examples for beginners is collection of complete c++ programs which can be very useful for beginners to ready to execute and learn.

//Written by Adarsh Patel

//reverse string with the use of pointer
#include <stdio.h>
#include <conio.h>
#include <string.h>
void main()
{
clrscr();
char *str,*dup_str;
char temp;
int i,j;
dup_str=str;

printf(“\nEnter String : “);
gets(str);

printf(“\nCurrent String is : “);
puts(str);

printf(“\nAddress of Str is : %u”,str);
printf(“\nAddress of str_dup is : %u”,dup_str);
printf(“\nStrlen is : %d\n”,strlen(str));
for(i=0,j=strlen(str)-1;i<=strlen(str)/2;i++,j–)
{
temp = *(str+i);
*(str+i) = *(str+j);
*(str+j) = temp;
}

str=dup_str;
printf(“\nNew String is : \n”);
for(;*str!=NULL;str++)
printf(“%c”,*str);

getch();
}

//Written by Adarsh Patel

//check weather string is palindrom or not, with the use of pointer
#include <stdio.h>
#include <conio.h>
#include <string.h>
void main()
{
clrscr();
char *str,*dup_str;
char temp;
int i,j,flag;
dup_str=str;

printf(“\nEnter String : “);
gets(str);

printf(“\nCurrent String is : “);
puts(str);

printf(“\nAddress of Str is : %u”,str);
printf(“\nAddress of str_dup is : %u”,dup_str);
printf(“\nStrlen is : %d\n”,strlen(str));

flag=1;
for(i=0,j=strlen(str)-1;i<=strlen(str)/2;i++,j–)
{
if(*(str+i) == *(str+j))
flag=1;
else
{
flag=0;
break;
}
}

if(flag==1)
printf(“\nGiven String is Palindrom”);
else
printf(“\nGiven String is not Palindrom”);

getch();
}

//Written by Adarsh Patel

//find next date using pointer
#include <stdio.h>
#include <conio.h>
#include <iostream.h>
void main()
{
clrscr();
int month[]={31,28,31,30,31,30,31,
31,30,31,30,31};
int *d,*m,*y;

printf(“\n\tFind Next Date useing Pointer “);
printf(“\n\nEnter DD MM YYYY :”);
scanf(“%d %d %d”,d,m,y);

if(*m==2)
{
if(*y%4==0 && (*y%100!=0 || *y%400==0))
*(month+1) = 29;
}

if(*d<*(month+(*m-1)))
*d = *d + 1;
else if(*m == 12)
{
*d = 1;
*m = 1;
*y = *y + 1;
}
else //if(*d == *(month+(*m-1)))
{
*m=*m+1;
*d = 1;
}
printf(“\n Next Date is : DD – MM – YYYY”);
printf(“\n %d %d %d”,*d,*m,*y);

getch();
}

//Written by Adarsh Patel

// find previous date using pointer
#include <stdio.h>
#include <conio.h>
#include <iostream.h>
void main()
{
clrscr();
int month[]={31,28,31,30,31,30,31,
31,30,31,30,31};
int *d,*m,*y;

printf(“\n\tFind Previous Date using Pointer “);
printf(“\n\nEnter DD MM YYYY :”);
scanf(“%d %d %d”,d,m,y);

if(*m==3)
{
if(*y%4==0 && (*y%100!=0 || *y%400==0))
*(month+1) = 29;
}

if(*d>1)
*d = *d – 1;
else if(*m == 1)
{
*d = 31;
*m = 12;
*y = *y – 1;
}
else
{
*m=*m-1;
*d = *(month+(*m-1));
}
printf(“\n Previous Date is : DD – MM – YYYY”);
printf(“\n %d %d %d”,*d,*m,*y);

getch();
}

//Written by Adarsh Patel

//Remove leading, trailing, inbetween extra space with the use of pointer
#include <stdio.h>
#include <conio.h>
#include <string.h>
void main()
{
clrscr();
char *str,*dup_str;
char temp;
int i,j;
dup_str=str;

printf(“\nEnter String : “);
gets(str);

printf(“\nCurrent String is : “);
puts(str);

printf(“\nAddress of Str is : %u”,str);
printf(“\nAddress of str_dup is : %u”,dup_str);
printf(“\nStrlen is : %d\n\n”,strlen(str));

for(i=0,j=0;*(str+i)!=NULL;)
{
if(*(str+j) == ‘ ‘ && (*(str+j+1) == ‘ ‘ || *(dup_str) == ‘ ‘ || *(str+j+1) == NULL))
{
j++;
}
else
{
*(str+i) = *(str+j);
i++;
j++;
}
}

puts(str);
printf(“New Strlen is : %d\n”,strlen(str));
getch();
}

//Written by Adarsh Patel

//Find out maximum length word, with the use of pointer.
#include <stdio.h>
#include <conio.h>
#include <string.h>
void main()
{
clrscr();
char *str,*dup_str;
char temp;
int i,j,k=0,s=0;
dup_str=str;

printf(“\nEnter String : “);
gets(str);
printf(“\nCurrent String is : “);
puts(str);
printf(“\nAddress of Str is : %u”,str);
printf(“\nAddress of str_dup is : %u”,dup_str);
printf(“\nStrlen is : %d\n\n”,strlen(str));
j=0;
i=-1;

//    for(i=0;*(str+i)!=NULL;i++)
do
{
i++;
if(*(str+i)!=’ ‘ && *(str+i)!=NULL)
{
k++;
continue;
}
else if(*(str+i)==’ ‘ || *(str+i)==NULL)
{
if(k>j)
{
j=k;
s=i-k;
k=0;
}
else
k=0;
}
}while(*(str+i)!=NULL);

str=dup_str;
for(i=s;i<=s+j;i++)
printf(“%c”,*(str+i));

getch();
}

//Written by Adarsh Patel

//Find out maximum length word, with the use of pointer.
#include <stdio.h>
#include <conio.h>
#include <string.h>
void main()
{
clrscr();
char *str,*dup_str;
char temp;
int i,j,k=0,s=0;
dup_str=str;

printf(“\nEnter String : “);
gets(str);
printf(“\nCurrent String is : “);
puts(str);
printf(“\nAddress of Str is : %u”,str);
printf(“\nAddress of str_dup is : %u”,dup_str);
printf(“\nStrlen is : %d\n\n”,strlen(str));
j=0;
i=-1;

//    for(i=0;*(str+i)!=NULL;i++)
do
{
i++;
if(*(str+i)!=’ ‘ && *(str+i)!=NULL)
{
k++;
continue;
}
else if(*(str+i)==’ ‘ || *(str+i)==NULL)
{
if(k>j)
{
j=k;
s=i-k;
k=0;
}
else
k=0;
}
}while(*(str+i)!=NULL);

str=dup_str;
for(i=s;i<=s+j;i++)
printf(“%c”,*(str+i));

getch();
}

//Written by Adarsh Patel

//Enter n integer  value and sort in ascending and descending
#include <stdio.h>
#include <conio.h>
#include <string.h>
void main()
{
clrscr();
int *no,n,i,j;

printf(“Enter Number of Elements : “);
scanf(“%d”,&n);

for(i=0;i<n;i++)
{
printf(“Enter Number of [%d] : “,i);
scanf(“%d”,no+i);
}
//ascending order
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(*(no+j)<*(no+i))
{
*(no+j)= *(no+j) + *(no+i);
*(no+i)= *(no+j) – *(no+i);
*(no+j)= *(no+j) – *(no+i);
}
}
}
printf(“Number is Ascending Order : \n”);
for(i=0;i<n;i++)
printf(“\n %-3d : %-5d”,i+1,*(no+i));

//Descending order
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(*(no+j)>*(no+i))
{
*(no+j)= *(no+j) + *(no+i);
*(no+i)= *(no+j) – *(no+i);
*(no+j)= *(no+j) – *(no+i);
}
}
}
printf(“\nNumber is Descending Order : \n”);
for(i=0;i<n;i++)
printf(“\n %-3d : %-5d”,i+1,*(no+i));

getch();
}

//Written by Adarsh Patel

//next date using pointer without temp variable
/*    function atoi(const char *s)
return int value

function itoa(int value,char *string,radix)
return character value of intiger & last character is null*/
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>
void main()
{
clrscr();
int month[]={31,28,31,30,31,30,31,31,30,31,30,31};
char *ptr;

printf(“Enter the value of date as string(DD-MM-YYYY) : “);
gets(ptr);
printf(“\nDay value is   : %-5d”,atoi(ptr+0));
printf(“\nMonth value is : %-5d”,atoi(ptr+3));
printf(“\nYear value is  : %-5d”,atoi(ptr+6));

if(atoi(ptr+3)==3 && atoi(ptr+0) == 1)
{
if(atoi(ptr+6)%4==0 && (atoi(ptr+6)%100!=0 || atoi(ptr+6)%400==0))
{
month[atoi(ptr+3)-2]=29;
//1 change for previous date
}
}

//    if(atoi(ptr+0)!=month[atoi(ptr+3)-1])
if(atoi(ptr+0)>1)
{
if(atoi(ptr+0)>10)        //for one digit number
itoa(atoi(ptr+0)-1,ptr+0,10);
else                    //for two digit number
{
*(ptr+0) = ‘0’;
itoa(atoi(ptr+0)-1,ptr+1,10);
}
*(ptr+2) = ‘-‘;
}
else if(atoi(ptr+3)!=1)
{

itoa(month[atoi(ptr+3)-2],ptr+0,10);
//        *(ptr+0) = ‘3’;         //
//        *(ptr+1) = ‘1’;          //day = 31
*(ptr+2) = ‘-‘;
if(atoi(ptr+3)>10)
itoa(atoi(ptr+3)-1,ptr+3,10);
else
{
if( atoi(ptr+3) == 10)
{
*(ptr+3) = ‘0’;
*(ptr+4) = ‘9’;
}
else
{
*(ptr+3) = ‘0’;
itoa(atoi(ptr+3)-1,ptr+4,10);
}
}
*(ptr+5) = ‘-‘;    //remove null character and add ‘-‘
}
else
{
strcpy(ptr+0,”31-12″);
*(ptr+5) = ‘-‘;
itoa(atoi(ptr+6)-1,ptr+6,10);        //y–
}
printf(“\n\nPrevious date is :  %s”,ptr);

//    puts(ptr);
getch();
}

//Written by Adarsh Patel

/* ptrmaxfn.cpp
this program find out the minimum value from the array with the use of pointer
*/
#include <stdio.h>
#include <conio.h>
void main()
{
clrscr();
float *ptr,*a,min;
int i=0,n;
printf(“\nHow many elements are there : “);
scanf(“%d”,&n);
for(i=0;i<n;i++)
{
printf(“Enter element [%d]”,i);
scanf(“%f”,(a+i));
}

ptr = a;
min = *ptr;

for(i=0;i<n;i++)
{
printf(“\nContent is a[%d] = %-9.2f “,i,*ptr);
printf(“\tAddress %u”,ptr++);
}
printf(“\n\nAddress of a[0] = %u and ptr = %u\n”,&a[0],ptr);
ptr = a;
printf(“\nAddress of a[0] = %u and ptr = %u\n”,&a[0],ptr);
i=0;

while(i<n)
{
if(min>*ptr)
min = *ptr;
i++;
ptr++;
}
printf(“\nMinimum value is %f”,min);
getch();
}

//Written by Adarsh Patel

/*
this program find out the minimum character from the array with the use of pointer
*/
#include <stdio.h>
#include <conio.h>

void main()
{
clrscr();
char *str,min;
int i=0;
printf(“\nEnter String : “);
gets(str);
min = *str;

for(i=1;i<*(str+i)!=NULL;i++)
{
if(*(str+i)!=’ ‘)
{
if(*(str+i)<min)
min = *(str+i);
}
}

printf(“\nMinimum character from string is %5c”,min);
getch();
}

//Written by Adarsh Patel

/*
this program find out the maximum character from the array with the use of pointer
*/
#include <stdio.h>
#include <conio.h>

void main()
{
clrscr();
char *str,max;
int i=0;
printf(“\nEnter String : “);
gets(str);
max = *str;

for(i=1;i<*(str+i)!=NULL;i++)
{
if(*(str+i)!=’ ‘)
{
if(*(str+i)>max)
max = *(str+i);
}
}

printf(“\nMaxmimum character from string is %5c”,max);
getch();
}

 

//Written by Adarsh Patel
//Find Difference between Two Date using string and pointer
// with the use of atoi & itoa stdlib function

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
void main()
{
clrscr();
//    int d1,m1,y1,d2,m2,y2,i,temp,sum=0;
int *temp,i,sum=0;
int month[]={31,28,31,30,31,30,31,31,30,31,30,31};
char *date1;
char *date2;

printf(“Enter Date 1 : (DD-MM-YYYY) :”);
//    scanf(“%d %d %d”,&d1,&m1,&y1);
gets(date1);

printf(“Enter Date 2 : (DD-MM-YYYY) :”);
//    scanf(“%d %d %d”,&d2,&m2,&y2);
gets(date2);

*temp=atoi(date1+0);

for(i=atoi(date1+3);i<atoi(date2+3)+(atoi(date2+6)-atoi(date1+6))*12;i++)
{
if(i>12)
{
i=1;
itoa( atoi(date1+6)+1,date1+6,10); //y1++;
}

if(i==2)
{
if(atoi(date1+6)%4==0 && (atoi(date1+6)%100!=0 || atoi(date1+6)%400==0))
month[i-1]=29;
else
month[i-1]=28;
}

sum=sum+(month[i-1]-*temp);
temp=0;

printf(“Year = %d Month = %d\t Days = %d\t Sum = %d\n”,atoi(date1+6),i,month[i-1],sum);

if(i%12==0)
getch();

}

sum=sum+atoi(date2+0)-*temp;

printf(“\n Total Number of Days are : %5d”,sum);
getch();
}

 

//Written by Adarsh Patel

#include <stdio.h>
#include <conio.h>
#include <ctype.h>
void main()
{
clrscr();
char *str;
int upper=0,lower=0,i=0,j;

printf(“Enter String : “);
gets(str);

i=-1;
do
{
i++;
if(isupper(*(str+i)))
upper++;
else if(islower(*(str+i)))
lower++;

if(*(str+i)==’ ‘ || *(str+i)==NULL)
{
for(j=i-(upper+lower);*(str+j)!=’ ‘ && *(str+j)!=NULL;j++)
{
if(upper>lower)
*(str+j) = toupper(*(str+j));
else
*(str+j) = tolower(*(str+j));
}
upper=0;
lower=0;
}

}while(*(str+i)!=NULL);

printf(“\nNew String is : “);
puts(str);
getch();
}

 

//Written by Adarsh Patel

/*
Proper case with the use of pointer
Input    :    mca bca
Output    :    Mca Bca
*/
#include <stdio.h>
#include <conio.h>
#include <ctype.h>

void main()
{
clrscr();
char *str;
int i=0;

printf(“\nEnter String : “);
gets(str);

for(i=0;i<*(str+i)!=NULL;i++)
{
if(*(str+(i-1))==’ ‘ || i==0)
{
*(str+i) = toupper(*(str+i));
}
}

printf(“\nNew String is : %5s”,str);
getch();
}

 

//Written by Adarsh Patel

/*
Sort two dimention array with the use of pointer of array
*/
#include <stdio.h>
#include <conio.h>
#include <string.h>
void main()
{
clrscr();
char (*str)[20],*temp;
int i=0,n;

printf(“\nHow many elements are there :”);
scanf(“%d”,&n);

for(i=0;i<n;i++)
{
flushall();
printf(“\nEnter String [%d]: “,i);
gets(*(str+i));
}

for(i=0;i<n;i++)
{
for(int j=i+1;j<n;j++)
{
if(strcmp(*(str+i),*(str+j)) > 0)
{
strcpy(temp,*(str+i));
strcpy(*(str+i),*(str+j));
strcpy(*(str+j),temp);
}
}
}

printf(“\nSorted List is :\n”);
for(i=0;i<n;i++)
{
printf(“\n[%d] : %s”,i,*(str+i));
}

getch();
}

//Written by Adarsh Patel

/*
Sort two dimention array with the use of pointer of array
*/
#include <stdio.h>
#include <conio.h>
#include <string.h>
void main()
{
clrscr();
char (*str)[20],*temp;
int i=0,n;

printf(“\nHow many elements are there :”);
scanf(“%d”,&n);

for(i=0;i<n;i++)
{
flushall();
printf(“\nEnter String [%d]: “,i);
gets(*(str+i));
}

for(i=0;i<n;i++)
{
for(int j=i+1;j<n;j++)
{
if(strcmp(*(str+i),*(str+j)) > 0)
{
strcpy(temp,*(str+i));
strcpy(*(str+i),*(str+j));
strcpy(*(str+j),temp);
}
}
}

printf(“\nSorted List is :\n”);
for(i=0;i<n;i++)
{
printf(“\n[%d] : %s”,i,*(str+i));
}
getch();
}

 

//Written by Adarsh Patel

/*
Sort two dimention array with the use of pointer of array
*/
#include <stdio.h>
#include <conio.h>
#include <string.h>
void main()
{
clrscr();
char (*str)[20],*temp;
int i=0,n;

printf(“\nHow many elements are there :”);
scanf(“%d”,&n);

for(i=0;i<n;i++)
{
flushall();
printf(“\nEnter String [%d]: “,i);
gets(*(str+i));
}

for(i=0;i<n;i++)
{
for(int j=i+1;j<n;j++)
{
if(strcmp(*(str+i),*(str+j)) > 0)
{
strcpy(temp,*(str+i));
strcpy(*(str+i),*(str+j));
strcpy(*(str+j),temp);
}
}
}

printf(“\nSorted List is :\n”);
for(i=0;i<n;i++)
{
printf(“\n[%d] : %s”,i,*(str+i));
}
getch();
}

 

//Written by Adarsh Patel
//Find day on particular date with the use of pointer

#include <stdio.h>
#include <conio.h>
void main()
{
clrscr();
int *d1,*m1,*y1,*d2,*m2,*y2,i,*temp;
int month[]={31,28,31,30,31,30,31,31,30,31,30,31};
long int *sum;
char day[][20]={“Tuesday”,”Wednesday”,”Thursday”,”Friday”,”Saturday”,”Sunday”,”Monday”};

*d1 = 1;
*m1 = 1;
*y1 = 1980;
*sum = 0;

printf(“\n\t\tEnter Date 1-1-1980 on wards……\n\n”);
printf(“Enter Date : (DD-MM-YYYY) :”);
scanf(“%d-%d-%d”,d2,m2,y2);

if(*y2<1980)
{
printf(“\nWrong Date ……….\nTry again…..”);
return;
}
*temp = *d1;
for(i=*m1;i<*m2+(*y2-*y1)*12;i++)
{
if(i>12)
{
i=1;
*y1 = *y1 + 1;
}
if(i==2)
{
if(*y1%4==0 && (*y1%100!=0 || *y1%400==0))
month[i-1]=29;
else
month[i-1]=28;
}
*sum = *sum+(month[i-1] – *temp);
*temp=0;
}

*sum = *sum + *d2 – *temp;
printf(“\nDate     : %02d-%02d-%4d”,*d2,*m2,*y2);
printf(“\nDay is   : “);

switch(*sum%7)
{
case 0:
puts(day[0]);
break;
case 1:
puts(day[1]);
break;
case 2:
puts(day[2]);
break;
case 3:
puts(day[3]);
break;
case 4:
puts(day[4]);
break;
case 5:
puts(day[5]);
break;
case 6:
puts(day[6]);
break;
}
getch();
}

 

check more c programming tutorial @ c programming tutorial for beginners.

java video tutorial

 

android beginners tutorial

Adarsh Patel

My Self Adarsh Suryakant Patel. Currently I am working as Assistant Professor at Parul Instiute of Engineering and Technology, MCA Department, Limda, Vadodara. Major Responsibility : * Lectures * Organizing Training and Expert Sessions for * Placement and Recruitment * Faculty Representative