to run the programs directly in c/c++ compiler copy … makhija (computer engineer) to run the...

48
Hemant Makhija (COMPUTER ENGINEER) www.goldenbeauty.webs.com To Run the Programs Directly in C/C++ compiler Copy the text of pdf Document.

Upload: phamkhanh

Post on 15-May-2018

227 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: To Run the Programs Directly in C/C++ compiler Copy … Makhija (COMPUTER ENGINEER) To Run the Programs Directly in C/C++ compiler Copy the text of pdf Document. Hemant Makhija (COMPUTER

Hemant Makhija(COMPUTER ENGINEER)

www.goldenbeauty.webs.com

To Run the Programs Directly in C/C++ compilerCopy the text of pdf Document.

Page 2: To Run the Programs Directly in C/C++ compiler Copy … Makhija (COMPUTER ENGINEER) To Run the Programs Directly in C/C++ compiler Copy the text of pdf Document. Hemant Makhija (COMPUTER

Hemant Makhija(COMPUTER ENGINEER)

www.goldenbeauty.webs.com

Paste into the notepad.Save the file with extension .c(e.g.: yourname.c)

Now the program is ready to be opened in C/C++ Compiler

Page 3: To Run the Programs Directly in C/C++ compiler Copy … Makhija (COMPUTER ENGINEER) To Run the Programs Directly in C/C++ compiler Copy the text of pdf Document. Hemant Makhija (COMPUTER

Hemant Makhija(COMPUTER ENGINEER)

www.goldenbeauty.webs.com

/*Prog to show the use of printf function.*/#include<stdio.h>#include<conio.h>void main(){ clrscr(); printf("*\n"); printf("**\n"); printf("***\n"); printf("**\n"); printf("*"); getch();}

Page 4: To Run the Programs Directly in C/C++ compiler Copy … Makhija (COMPUTER ENGINEER) To Run the Programs Directly in C/C++ compiler Copy the text of pdf Document. Hemant Makhija (COMPUTER

Hemant Makhija(COMPUTER ENGINEER)

www.goldenbeauty.webs.com

/*Prog to show the use of printf function.*/#include<stdio.h>#include<conio.h>void main(){ clrscr(); printf("Makhija\n"); printf("-------Hemant\n"); printf("-------------Jairamdas\n"); getch();}

Page 5: To Run the Programs Directly in C/C++ compiler Copy … Makhija (COMPUTER ENGINEER) To Run the Programs Directly in C/C++ compiler Copy the text of pdf Document. Hemant Makhija (COMPUTER

Hemant Makhija(COMPUTER ENGINEER)

www.goldenbeauty.webs.com

/*Prog to show the use all arithmetic operators*/#include<stdio.h>#include<conio.h>void main(){ int a,b,sum,sub,mul,div,mod; clrscr(); printf("Enter the value of A and B:\n"); scanf("%d%d",&a,&b); sum=a+b; sub=a-b; mul=a*b; div=a/b; mod=a%b; printf("\nThe sum of A and B is %d.",sum); printf("\nThe subtraction of A and B is %d.",sub); printf("\nThe multiplication of A and B is %d.",mul); printf("\nThe division of A and B is %d.",div); printf("\nThe modulo of A and B is %d.",mod); getch();}

Page 6: To Run the Programs Directly in C/C++ compiler Copy … Makhija (COMPUTER ENGINEER) To Run the Programs Directly in C/C++ compiler Copy the text of pdf Document. Hemant Makhija (COMPUTER

Hemant Makhija(COMPUTER ENGINEER)

www.goldenbeauty.webs.com

/*Prog to convert temperature from Fahrenheit to Celsius.*/#include<stdio.h>#include<conio.h>#include<math.h>void main(){ float f,c; clrscr(); printf("Enter the value of Fahrenheit:\n"); scanf("%f",&f); c=(f-32)/1.8; printf("The value of Celsius is: %.1f",c); getch();}

Page 7: To Run the Programs Directly in C/C++ compiler Copy … Makhija (COMPUTER ENGINEER) To Run the Programs Directly in C/C++ compiler Copy the text of pdf Document. Hemant Makhija (COMPUTER

Hemant Makhija(COMPUTER ENGINEER)

www.goldenbeauty.webs.com

/*Prog to find the biggest no from three nos.*/#include<stdio.h>#include<conio.h>void main(){

int a,b,c;clrscr();printf("Enter the three Nos.");scanf("%d%d%d",&a,&b,&c);if(a>b){

if(a>c){

printf("The max no is:%d",a);}else{

printf("The max no is:%d",c);}

}else{

if(b>c){

printf("The max no is:%d",b);}else{

printf("The max no is:%d",c);}

}getch();

}

Page 8: To Run the Programs Directly in C/C++ compiler Copy … Makhija (COMPUTER ENGINEER) To Run the Programs Directly in C/C++ compiler Copy the text of pdf Document. Hemant Makhija (COMPUTER

Hemant Makhija(COMPUTER ENGINEER)

www.goldenbeauty.webs.com

/*Prog to swap values of two variables using temporaryvariable.*/#include<stdio.h>#include<conio.h>void main(){

int a,b,c;clrscr();printf("\nEnter the value of X:");scanf("\n%d",&a);printf("\nEnter the value of Y:");scanf("\n%d",&b);c=a;a=b;b=c;printf("\nThe value of X is:%d",a);printf("\nThe value of Y is:%d",b);getch();

}

Page 9: To Run the Programs Directly in C/C++ compiler Copy … Makhija (COMPUTER ENGINEER) To Run the Programs Directly in C/C++ compiler Copy the text of pdf Document. Hemant Makhija (COMPUTER

Hemant Makhija(COMPUTER ENGINEER)

www.goldenbeauty.webs.com

/*Prog to show that the entered character is alphabet ordigit.*/#include<stdio.h>#include<conio.h>void main(){

char ch;clrscr();printf("Enter any Character:");ch = getchar();if(isalpha(ch))

{printf("The character is alphabet.");

}else{

if(isdigit(ch)){

printf("The character is digit.");}else{printf("The character entered is neither Alphabet nor

Digit.");}

}getch();

}

Page 10: To Run the Programs Directly in C/C++ compiler Copy … Makhija (COMPUTER ENGINEER) To Run the Programs Directly in C/C++ compiler Copy the text of pdf Document. Hemant Makhija (COMPUTER

Hemant Makhija(COMPUTER ENGINEER)

www.goldenbeauty.webs.com

/*Prog to show the formatted output.*/#include<stdio.h>#include<conio.h>void main(){

int a;clrscr();printf("Enter he value:");scanf("%d",&a);printf("\n%d",a);printf("\n%6d",a);printf("\n%06d",a);getch();

}

Page 11: To Run the Programs Directly in C/C++ compiler Copy … Makhija (COMPUTER ENGINEER) To Run the Programs Directly in C/C++ compiler Copy the text of pdf Document. Hemant Makhija (COMPUTER

Hemant Makhija(COMPUTER ENGINEER)

www.goldenbeauty.webs.com

/*Prog to show the formatted output.*/#include<stdio.h>#include<conio.h>void main(){

float a;clrscr();printf("Enter he value:");scanf("%f",&a);printf("\n%e",a);printf("\n%10.2e",a);getch();

}

Page 12: To Run the Programs Directly in C/C++ compiler Copy … Makhija (COMPUTER ENGINEER) To Run the Programs Directly in C/C++ compiler Copy the text of pdf Document. Hemant Makhija (COMPUTER

Hemant Makhija(COMPUTER ENGINEER)

www.goldenbeauty.webs.com

/*Prog to show the formatted output.*/#include<stdio.h>#include<conio.h>void main(){

char a,b=10;clrscr();printf("Enter the character & String:");scanf("%c",&a);scanf("%s",b);printf("\n%15c",a);printf("\n%15s",b);printf("\n%s",b);getch();

}

Page 13: To Run the Programs Directly in C/C++ compiler Copy … Makhija (COMPUTER ENGINEER) To Run the Programs Directly in C/C++ compiler Copy the text of pdf Document. Hemant Makhija (COMPUTER

Hemant Makhija(COMPUTER ENGINEER)

www.goldenbeauty.webs.com

/*Prog to show the formatted output.*/#include<stdio.h>#include<conio.h>void main(){

float a;clrscr();printf("Enter he value:");scanf("\n%f",&a);printf("\n%f",a);printf("\n%7.4f",a);printf("\n%7.2f",a);getch();

}

Page 14: To Run the Programs Directly in C/C++ compiler Copy … Makhija (COMPUTER ENGINEER) To Run the Programs Directly in C/C++ compiler Copy the text of pdf Document. Hemant Makhija (COMPUTER

Hemant Makhija(COMPUTER ENGINEER)

www.goldenbeauty.webs.com

/*Prog to calculate the total marks and percentage and to showthe grade.*/#include<stdio.h>#include<conio.h>void main(){

int a,b,c,d,e,total,per;clrscr();printf("\nEnter the marks in 5 subjects:\n");scanf("\n%d\n%d\n%d\n%d\n%d",&a,&b,&c,&d,&e);total=a+b+c+d+e;per=total/5;printf("\nThe total is %d.",total);printf("\nThe percentage is %d.",per);if(per>=66)printf("\nGrade:Distinction");

else if(per>=60&&per<66)printf("\nGrade:First Class");

else if(per>=50&&per<60)printf("\nGrade:Second Class");

else if(per>=40&&per<50)printf("\nGrade:Pass");

elseprintf("\nGrade:Fail");

getch();}

Page 15: To Run the Programs Directly in C/C++ compiler Copy … Makhija (COMPUTER ENGINEER) To Run the Programs Directly in C/C++ compiler Copy the text of pdf Document. Hemant Makhija (COMPUTER

Hemant Makhija(COMPUTER ENGINEER)

www.goldenbeauty.webs.com

/*Prog to find the area of the circle.*/#include<stdio.h>#include<conio.h>#include<math.h>void main(){

float r,area,pi;pi=3.14;clrscr();printf("Enter the value of Radius:");scanf("%f",&r);area=pi*r*r;printf("\nThe area of the circle is %f",area);getch();

}

Page 16: To Run the Programs Directly in C/C++ compiler Copy … Makhija (COMPUTER ENGINEER) To Run the Programs Directly in C/C++ compiler Copy the text of pdf Document. Hemant Makhija (COMPUTER

Hemant Makhija(COMPUTER ENGINEER)

www.goldenbeauty.webs.com

/*Prog to find the square of the given no.*/#include<stdio.h>#include<conio.h>#include<math.h>void main(){

float no,sqr;clrscr();printf("Enter the No.:");scanf("%f",&no);sqr=no*no;printf("\nThe Square of the No is %f",sqr);getch();

}

Page 17: To Run the Programs Directly in C/C++ compiler Copy … Makhija (COMPUTER ENGINEER) To Run the Programs Directly in C/C++ compiler Copy the text of pdf Document. Hemant Makhija (COMPUTER

Hemant Makhija(COMPUTER ENGINEER)

www.goldenbeauty.webs.com

/*Prog to show the Sin and Cos table.*/#include<stdio.h>#include<conio.h>#include<math.h>#define pi 3.14void main(){

int deg;float angle,value1,value2;clrscr();printf("\nAngle Sin Value Cos Value");for(deg=0;deg<=180;deg=deg+15){angle=(pi*deg)/180;value1=sin(angle);value2=cos(angle);printf("\n%3d %.2f

%.2f",deg,value1,value2);}getch();

}

Page 18: To Run the Programs Directly in C/C++ compiler Copy … Makhija (COMPUTER ENGINEER) To Run the Programs Directly in C/C++ compiler Copy the text of pdf Document. Hemant Makhija (COMPUTER

Hemant Makhija(COMPUTER ENGINEER)

www.goldenbeauty.webs.com

/*Prog to read an integer and print sum of digits.*/#include<stdio.h>#include<conio.h>void main(){

int a,sum,x;clrscr();printf("Enter The No:");scanf("%d",&a);sum=0;do{x=a%10;sum=sum+x;a=a/10;}while(a>0);printf("%d",sum);getch();

}

Page 19: To Run the Programs Directly in C/C++ compiler Copy … Makhija (COMPUTER ENGINEER) To Run the Programs Directly in C/C++ compiler Copy the text of pdf Document. Hemant Makhija (COMPUTER

Hemant Makhija(COMPUTER ENGINEER)

www.goldenbeauty.webs.com

/*Prog to show the Floyd’s triangle.12 34 5 67 8 9 10------------*/#include<stdio.h>#include<conio.h>void main(){

int a,h,i,j;clrscr();printf("Enter the no.");scanf("%d",&a);h=1;for(i=1;i<=a;i++){

for(j=1;j<=i;j++){printf(" %2d",h);h=h+1;}

printf("\n");}getch();

}

Page 20: To Run the Programs Directly in C/C++ compiler Copy … Makhija (COMPUTER ENGINEER) To Run the Programs Directly in C/C++ compiler Copy the text of pdf Document. Hemant Makhija (COMPUTER

Hemant Makhija(COMPUTER ENGINEER)

www.goldenbeauty.webs.com

/*Prog to show the Floyed's triangle.101101010110101-------*/#include<stdio.h>#include<conio.h>void main(){

int a,c,mod,i,j,h;clrscr();printf("Enter the no.");scanf("%d",&a);for(i=0;i<=a;i=i+1){

h=i%2;if(h==1)

mod=1;elsemod=0;for(j=1;j<=i;j++){

printf("%d",mod);if(mod==1)mod=0;elsemod=1;

}printf("\n");

}getch();

}

Page 21: To Run the Programs Directly in C/C++ compiler Copy … Makhija (COMPUTER ENGINEER) To Run the Programs Directly in C/C++ compiler Copy the text of pdf Document. Hemant Makhija (COMPUTER

Hemant Makhija(COMPUTER ENGINEER)

www.goldenbeauty.webs.com

/*Prog to show the value of Y according to the value of X.Y=1 FOR X>0-1FOR X<0

0 FOR X=0*/#include<stdio.h>#include<conio.h>void main(){

float x,y;clrscr();printf("enter value of value of x:-");scanf("%f",&x);if (x>0)printf("y is 1");else if (x==0)printf("y is 0");else if (x<0)printf("y is -1");getch();

}

Page 22: To Run the Programs Directly in C/C++ compiler Copy … Makhija (COMPUTER ENGINEER) To Run the Programs Directly in C/C++ compiler Copy the text of pdf Document. Hemant Makhija (COMPUTER

Hemant Makhija(COMPUTER ENGINEER)

www.goldenbeauty.webs.com

/*Prog to show the value of Y according to the value of X.Y=1 FOR X>0-1FOR X<0

0 FOR X=0*/#include<stdio.h>#include<conio.h>void main(){float x,y;clrscr();printf("enter value of value of x:-");scanf("%f",&x); if (x<=0) {

if (x<0){printf("y is -1");}else{printf("y is 0");}

}else{printf("y is 1");}getch();}

Page 23: To Run the Programs Directly in C/C++ compiler Copy … Makhija (COMPUTER ENGINEER) To Run the Programs Directly in C/C++ compiler Copy the text of pdf Document. Hemant Makhija (COMPUTER

Hemant Makhija(COMPUTER ENGINEER)

www.goldenbeauty.webs.com

/*Prog to show the value of Y according to the value of X.Y=1 FOR X>0-1FOR X<0

0 FOR X=0*/#include<stdio.h>#include<conio.h>void main(){

float x,y;clrscr();printf("enter value of value of x:-");scanf("%f",&x);y=(x!=0) ? ((x>0) ? (1) : (-1)) : 0;printf("value of y is%f",y);getch();

}

Page 24: To Run the Programs Directly in C/C++ compiler Copy … Makhija (COMPUTER ENGINEER) To Run the Programs Directly in C/C++ compiler Copy the text of pdf Document. Hemant Makhija (COMPUTER

Hemant Makhija(COMPUTER ENGINEER)

www.goldenbeauty.webs.com

/*Program showing charges according used units*/#include<stdio.h>#include<conio.h>void main(){

float unit,charge,total;char name[10];clrscr();printf("Enter the name and units used:");gets(name);scanf("%f",&unit);if(unit<=200)charge=unit*0.8;else if(unit>200&&unit<=300)charge=(200*0.8)+((unit-200)*0.9);elsecharge=(200*0.8)+(100*0.9)+((unit-300)*1);total=charge+100;if(total>400)total=total+(total*0.15);printf("\nYour name is %s.",name);printf("\nYour total amount is %0.2f",total);getch();

}

Page 25: To Run the Programs Directly in C/C++ compiler Copy … Makhija (COMPUTER ENGINEER) To Run the Programs Directly in C/C++ compiler Copy the text of pdf Document. Hemant Makhija (COMPUTER

Hemant Makhija(COMPUTER ENGINEER)

www.goldenbeauty.webs.com

/*Program showing prime nos between given range*/#include<stdio.h>#include<conio.h>void main(){

int a,b,i,j,cnt,div;clrscr();printf("Enter the range");scanf("%d",&a);scanf("%d",&b);for(i=a;i<=b;i++){

cnt=0;if(i==1)printf("1 is a special no.");else{

for(j=1;j<=i;j++){

div=i%j;if(div==0)cnt=cnt+1;

}}if(cnt==2)printf("\n%d is a prime no.",i);

}getch();

}

Page 26: To Run the Programs Directly in C/C++ compiler Copy … Makhija (COMPUTER ENGINEER) To Run the Programs Directly in C/C++ compiler Copy the text of pdf Document. Hemant Makhija (COMPUTER

Hemant Makhija(COMPUTER ENGINEER)

www.goldenbeauty.webs.com

/*Prog. showing use of go to statement*/#include<stdio.h>#include<conio.h>#include<math.h> void main(){

int i;clrscr();i=0;label:if (i<=14){i=i+1;printf("%d\n",i);goto label;}getch();}

Page 27: To Run the Programs Directly in C/C++ compiler Copy … Makhija (COMPUTER ENGINEER) To Run the Programs Directly in C/C++ compiler Copy the text of pdf Document. Hemant Makhija (COMPUTER

Hemant Makhija(COMPUTER ENGINEER)

www.goldenbeauty.webs.com

/*Prog. showing reverse of a no.*/#include<stdio.h>#include<conio.h>void main(){

int a,b;printf("Enter any no.");scanf("%d",&a);while(a>0){

b=a%10;printf("%d",b);a=a/10;

}getch();}

Page 28: To Run the Programs Directly in C/C++ compiler Copy … Makhija (COMPUTER ENGINEER) To Run the Programs Directly in C/C++ compiler Copy the text of pdf Document. Hemant Makhija (COMPUTER

Hemant Makhija(COMPUTER ENGINEER)

www.goldenbeauty.webs.com

/*Prog. showing factorial of a given no.*/#include<stdio.h>#include<conio.h>void main(){

int a,i,fac=1;clrscr();printf("Enter the no.");scanf("%d",&a);for(i=1;i<=a;i++){

fac=fac*i;}printf("Factorial of the given no is. %d",fac);getch();

}

Page 29: To Run the Programs Directly in C/C++ compiler Copy … Makhija (COMPUTER ENGINEER) To Run the Programs Directly in C/C++ compiler Copy the text of pdf Document. Hemant Makhija (COMPUTER

Hemant Makhija(COMPUTER ENGINEER)

www.goldenbeauty.webs.com

/*Fibonacci series*/#include<stdio.h>#include<conio.h>#include<math.h>void main(){

int sum=1,i=0,j=0,n;clrscr();printf("Enter the value:");scanf("%d",&n);while (sum<=n){

printf(" %d",sum);i=j;j=sum;sum=i+j;

}getch();

}

Page 30: To Run the Programs Directly in C/C++ compiler Copy … Makhija (COMPUTER ENGINEER) To Run the Programs Directly in C/C++ compiler Copy the text of pdf Document. Hemant Makhija (COMPUTER

Hemant Makhija(COMPUTER ENGINEER)

www.goldenbeauty.webs.com

/*Prog Calculating the equation V=P(1+r)^n*/#include<stdio.h>#include<conio.h>#include<math.h>void main(){

float v,r,n,p;clrscr();printf("enter value of principal");scanf("%f",&p);printf("enter value of rate of interest");scanf("%f",&r);printf("enter value of no of years");scanf("%f",&n);v=pow((p*(1+r)),n);printf("%f",v);getch();

}

Page 31: To Run the Programs Directly in C/C++ compiler Copy … Makhija (COMPUTER ENGINEER) To Run the Programs Directly in C/C++ compiler Copy the text of pdf Document. Hemant Makhija (COMPUTER

Hemant Makhija(COMPUTER ENGINEER)

www.goldenbeauty.webs.com

/*Prog. showing starts in following pattern***** **** *** ** **/#include<stdio.h>#include<conio.h>#include<math.h>void main(){

int i,j,n,k;clrscr();printf("enter value:");scanf("%d",&n);for (i=0;i<=n;i++){

for(k=i;k>=0;k--){

printf(" ");}for(j=1;j<=n-i;j++){

printf("*");}printf("\n");

}getch();

}

Page 32: To Run the Programs Directly in C/C++ compiler Copy … Makhija (COMPUTER ENGINEER) To Run the Programs Directly in C/C++ compiler Copy the text of pdf Document. Hemant Makhija (COMPUTER

Hemant Makhija(COMPUTER ENGINEER)

www.goldenbeauty.webs.com

/*prog. showing following pattern1223334444*/#include<stdio.h>#include<conio.h>void main(){

int a,i,j;clrscr();printf("Enter the no of rows.");scanf("%d",&a);for(i=1;i<=a;i++){

for(j=1;j<=i;j++){

printf("%d",i);}printf("\n");

}getch();

}

Page 33: To Run the Programs Directly in C/C++ compiler Copy … Makhija (COMPUTER ENGINEER) To Run the Programs Directly in C/C++ compiler Copy the text of pdf Document. Hemant Makhija (COMPUTER

Hemant Makhija(COMPUTER ENGINEER)

www.goldenbeauty.webs.com

/*Prog. to read the age of 10 persons and count the no ofpersons in the age group 50 to 60*/#include<stdio.h>#include<conio.h>#include<math.h>void main(){

int a[10],age,i,cnt=0;clrscr();for(i=0;i<10;i++){

printf("enter value of age");scanf("%d",&a[i]);

if(a[i]>50 && a[i]<60){cnt=cnt+1;}

}printf("%d",cnt);

getch();}

Page 34: To Run the Programs Directly in C/C++ compiler Copy … Makhija (COMPUTER ENGINEER) To Run the Programs Directly in C/C++ compiler Copy the text of pdf Document. Hemant Makhija (COMPUTER

Hemant Makhija(COMPUTER ENGINEER)

www.goldenbeauty.webs.com

/*Prog showing the binary value of the given no.*/#include<stdio.h>#include<conio.h>#include<math.h>void main(){

long int a,sum,x,i;clrscr();printf("Enter The No:");scanf("%ld",&a);sum=0;i=0;while(a>0){

x=a%2;sum=sum+(x*pow(10,i));a=a/2;i++;

}

printf("%ld",sum);getch();

}

Page 35: To Run the Programs Directly in C/C++ compiler Copy … Makhija (COMPUTER ENGINEER) To Run the Programs Directly in C/C++ compiler Copy the text of pdf Document. Hemant Makhija (COMPUTER

Hemant Makhija(COMPUTER ENGINEER)

www.goldenbeauty.webs.com

/*Prog to create PASCAL’s triangle*/#include<stdio.h>#include<conio.h>void main(){

int p[10][10],k,i,j,n;clrscr();for(i=0;i<10;i++){

for(j=0;j<=i;j++){

if(j==0||i==j)p[i][j]=1;elsep[i][j]=p[i-1][j-1]+p[i-1][j];printf("%5d",p[i][j]);

}printf("\n");

}getch();

}

Page 36: To Run the Programs Directly in C/C++ compiler Copy … Makhija (COMPUTER ENGINEER) To Run the Programs Directly in C/C++ compiler Copy the text of pdf Document. Hemant Makhija (COMPUTER

Hemant Makhija(COMPUTER ENGINEER)

www.goldenbeauty.webs.com

/*Prog. to printf ASCII values of given string*/#include<stdio.h>#include<conio.h>void main(){

char name[10],i;clrscr();printf("Enter ur name:");scanf("%s",&name);for (i=0;i<10;i++){

if(name[i]=='\0')break;elseprintf("\nASCII value of %c = %d",name[i],name[i]);

}getch();

}

Page 37: To Run the Programs Directly in C/C++ compiler Copy … Makhija (COMPUTER ENGINEER) To Run the Programs Directly in C/C++ compiler Copy the text of pdf Document. Hemant Makhija (COMPUTER

Hemant Makhija(COMPUTER ENGINEER)

www.goldenbeauty.webs.com

/*Prog showing the use of strcmp function*/#include<stdio.h>#include<conio.h>void main(){

int y;char name1[20],name2[20];clrscr();printf("Enter the string");scanf("%s",&name1);printf("Enter another string");scanf("%s",&name2);y=strcmp(name1,name2);if(y==0)printf("Both the strings are same.");else if(y>0)printf("The strings are not same.\n First string is larger

than Second.");elseprintf("The strings are not same.\n Second string is larger

than First.");getch();

}

Page 38: To Run the Programs Directly in C/C++ compiler Copy … Makhija (COMPUTER ENGINEER) To Run the Programs Directly in C/C++ compiler Copy the text of pdf Document. Hemant Makhija (COMPUTER

Hemant Makhija(COMPUTER ENGINEER)

www.goldenbeauty.webs.com

/*Prog to show whether the second strings is substring of firstor not*/#include<stdio.h>#include<conio.h>void main(){

int a;char s1[10],s2[10],s3[20];clrscr();printf("\nEnter First string\n");gets(s1);printf("\nEnter second string\n");gets(s2);if(strstr(s1,s2)==NULL)printf("Substring is not found");elseprintf("\nSecond string is substring of first");getch();

}

Page 39: To Run the Programs Directly in C/C++ compiler Copy … Makhija (COMPUTER ENGINEER) To Run the Programs Directly in C/C++ compiler Copy the text of pdf Document. Hemant Makhija (COMPUTER

Hemant Makhija(COMPUTER ENGINEER)

www.goldenbeauty.webs.com

/*Prog. to copy n characters from second string to First*/#include<stdio.h>#include<conio.h>void main(){

int a;char s1[10],s2[10],s3[20];clrscr();printf("\nEnter First string\n");gets(s1);printf("\nEnter second string\n");gets(s2);printf("\nEnter the no of characters you want to copy\n");scanf("%d",&a);printf("\nAddition of both the strings is

\n%s",strncat(s1,s2,a));getch();

}

Page 40: To Run the Programs Directly in C/C++ compiler Copy … Makhija (COMPUTER ENGINEER) To Run the Programs Directly in C/C++ compiler Copy the text of pdf Document. Hemant Makhija (COMPUTER

Hemant Makhija(COMPUTER ENGINEER)

www.goldenbeauty.webs.com

/*Prog showing following pattern 1 232 34543 4567654 567898765 */#include<stdio.h>#include<conio.h>void main(){

int i,m,j,k,a;clrscr();printf("\nEnter the no of rows");scanf("%d",&a);for(i=1;i<=a;i++){

m=i;for(j=a;j>=i;j--){

printf(" ");}for(k=1;k<=i;k++){

printf(" %d",m);m=m+1;

}for(k=1;k<=(i-1);k++){

if(k==1)m=m-2;elsem=m-1;printf(" %d",m);

}printf("\n");

}getch();

}

Page 41: To Run the Programs Directly in C/C++ compiler Copy … Makhija (COMPUTER ENGINEER) To Run the Programs Directly in C/C++ compiler Copy the text of pdf Document. Hemant Makhija (COMPUTER

Hemant Makhija(COMPUTER ENGINEER)

www.goldenbeauty.webs.com

/*Prog showing following pattern 1 232 34543 4567654 567898765 */#include<stdio.h>#include<conio.h>void main(){

int i,m,j,k,a;clrscr();printf("\nEnter the no of rows");scanf("%d",&a);for(i=1;i<=a;i++){

for(j=a;j>=i;j--){

printf(" ");}m=i;for(k=1;k<=i;k++){

printf(" %d",m);m=m+1;

}m=2*i-2;for(k=1;k<=(i-1);k++){

printf(" %d",m);m--;

}printf("\n");

}getch();

}

Page 42: To Run the Programs Directly in C/C++ compiler Copy … Makhija (COMPUTER ENGINEER) To Run the Programs Directly in C/C++ compiler Copy the text of pdf Document. Hemant Makhija (COMPUTER

Hemant Makhija(COMPUTER ENGINEER)

www.goldenbeauty.webs.com

/*User Defined Function showing prime nos between given range*/#include<stdio.h>#include<conio.h>void main(){

int a,y;clrscr();printf("Enter the no");scanf("%d",&a);y=(primeno(a));if(y==1)printf("No is Prime no");elseprintf("No is Not prime no.");getch();

}int primeno(int a){

int b,i,j,cnt,div,y,k;cnt=0;if(a==1)printf("1 is a special no.");else{

for(j=1;j<=a;j++){

div=a%j;if(div==0)cnt=cnt+1;

}}if(cnt==2)k=1;else

k=0;return(k);

}

Page 43: To Run the Programs Directly in C/C++ compiler Copy … Makhija (COMPUTER ENGINEER) To Run the Programs Directly in C/C++ compiler Copy the text of pdf Document. Hemant Makhija (COMPUTER

Hemant Makhija(COMPUTER ENGINEER)

www.goldenbeauty.webs.com

/*User Defined Function to change lowercase characters touppercase*/#include<stdio.h>#include<conio.h>char upper(char a);void main(){

char a;clrscr();printf("Enter any no.");scanf("%c",&a);printf("%c",upper(a));getch();

}char upper(char a){

return(a-32);}

Page 44: To Run the Programs Directly in C/C++ compiler Copy … Makhija (COMPUTER ENGINEER) To Run the Programs Directly in C/C++ compiler Copy the text of pdf Document. Hemant Makhija (COMPUTER

Hemant Makhija(COMPUTER ENGINEER)

www.goldenbeauty.webs.com

/*User Defined Function to interchange the values of twovariables*/#include<stdio.h>#include<conio.h>void change(int a,int b);void main(){

int a,b,c;clrscr();printf("\nEnter the value of X:");scanf("\n%d",&a);printf("\nEnter the value of Y:");scanf("\n%d",&b);change(a,b);getch();

}void change(int a,int b){

int c;c=a;a=b;b=c;printf("\nThe value of X is:%d",a);printf("\nThe value of Y is:%d",b);

}

Page 45: To Run the Programs Directly in C/C++ compiler Copy … Makhija (COMPUTER ENGINEER) To Run the Programs Directly in C/C++ compiler Copy the text of pdf Document. Hemant Makhija (COMPUTER

Hemant Makhija(COMPUTER ENGINEER)

www.goldenbeauty.webs.com

/*User Defined Function to copy one string to another*/#include<stdio.h>#include<conio.h>void add(char s1[10],char s2[10]);void main(){

char s1[10],s2[10],s3[15];clrscr();printf("Enter first string");gets(s1);printf("Enter the second string");gets(s2);add(s1,s2);getch();

}void add(char s1[10],char s2[10]){

char s3[20];int i=0,j=0,n=0;while(s1[i]!='\0'){

s3[n]=s1[i];n++;i++;

}while(s2[j]!='\0'){

s3[n]=s2[j];n++;j++;

}s3[n]='\0';printf("Addition of the both strings is\n %s",s3);getch();

}

Page 46: To Run the Programs Directly in C/C++ compiler Copy … Makhija (COMPUTER ENGINEER) To Run the Programs Directly in C/C++ compiler Copy the text of pdf Document. Hemant Makhija (COMPUTER

Hemant Makhija(COMPUTER ENGINEER)

www.goldenbeauty.webs.com

/*User Defined Function to Compare two strings*/#include<stdio.h>#include<conio.h>void copy(char s1[10],char s2[10]);void main(){

char s1[10],s2[10];clrscr();printf("Enter first string\n");gets(s1);printf("Enter the second string\n");gets(s2);copy(s1,s2);getch();

}void copy(char s1[10],char s2[10]){

int i=0,a;printf("How many characters you want to copy\nFrom second

string to first string.");scanf("%d",&a);do{

s1[i]=s2[i];i++;

}while(i<a);printf("The final string is %s",s1);

}

Page 47: To Run the Programs Directly in C/C++ compiler Copy … Makhija (COMPUTER ENGINEER) To Run the Programs Directly in C/C++ compiler Copy the text of pdf Document. Hemant Makhija (COMPUTER

Hemant Makhija(COMPUTER ENGINEER)

www.goldenbeauty.webs.com

/*User Defined Function to add one string to another*/#include<stdio.h>#include<conio.h>void comp(char s1[10],char s2[10]);void main(){

char s1[10],s2[10];clrscr();printf("Enter first string\n");gets(s1);printf("Enter the second string\n");gets(s2);comp(s1,s2);getch();

}void comp(char s1[10],char s2[10]){

int i=0,j=0;do{

if(s1[i]!=s2[i])j=1;i++;

}while(s1[i]!='\0' || s2[i]!='\0');if(j==0)printf("Both the strings are equal");elseprintf("Strings are not equal");

}

Page 48: To Run the Programs Directly in C/C++ compiler Copy … Makhija (COMPUTER ENGINEER) To Run the Programs Directly in C/C++ compiler Copy the text of pdf Document. Hemant Makhija (COMPUTER

Hemant Makhija(COMPUTER ENGINEER)

www.goldenbeauty.webs.com

/*User Defined Function showing factorial of a given no. usingRecursion*/#include<stdio.h>#include<conio.h>int fact(int a);void main(){

int a,i;clrscr();printf("Enter the no.");scanf("%d",&a);printf("Factorial of the given no is. %d",fact(a));getch();

}int fact(int a){

int b;if(a==1)b=1;elseb=a*fact(a-1);return(b);

}