13

Click here to load reader

Programs discussed so far!

Embed Size (px)

Citation preview

Page 1: Programs discussed so far!

Programming Language CS-103

Snapshot of

Programs discussed in

Class & Labs

Page 2: Programs discussed so far!

/*Palindrome Program1*/ #include<stdio.h> #include<conio.h> void main(void) { clrscr(); int a,b[10],c[10],i=0,L,j,k; char ch='F'; printf("\nEnter a number:"); scanf("%d",&a); while(a>0) { b[i]=a%10; a=a/10; i++; } for(j=i-1,k=0;j>=0;j--,k++) { c[j]=b[k]; } for(L=0;L<i;L++) { if(c[L]=b[L]) { ch='T'; else ch='F'; } if(ch=='T') printf("Palindrome"); else printf("Number is not a Palindrome"); getch(); }

/* Removing Vowels from String */ #include<stdio.h> #include<conio.h> int check_vowel(char); void main(void) { char s[100],t[100]; int i,j=0; printf("Enter a string to delete Vowels:"); gets(s); for(i=0;s[i]!='\0';i++) { if(check_vowel(s[i])==0) // not a vowel { t[j]=s[i]; j++; } } t[j]='\0'; printf("String after deleting vowels:%s\n",t); getch(); } int check_vowel(char) { switch(c) { case 'a': case 'e': case 'i': case 'o': case 'u': case 'A': case 'E': case 'I': case 'O': case 'U': return 1; default: return 0; } }

/*Palindrome Program2*/ #include<stdio.h> #include<conio.h> #include<string.h> void main(void) { clrscr(); char a[25],b[25]; printf("Enter a string:"); gets(a); strrev(strcpy(b,a)); if(strcmp(a,b)==0) printf("String is a palindrome"); else printf("String is not a palindrome"); getch(); } /*Palindrome program3*/ #include<stdio.h> #include<conio.h> void main(void) { clrscr(); int a,s=0; printf("\nEnter a number:"); scanf("%d",&a); temp=a; // store 'a' for comparison while(a>0) { s=(s*10)+(a%10); a=a/10; } if(c==s) printf("Palindrome"); else printf("Number is not a Palindrome"); getch(); }

Page 3: Programs discussed so far!

/*Decimal to Binary Conversion*/ #include<stdio.h> #include<conio.h> void main(void) { int binary[10],n,r,c=0,i; clrscr(); printf("Enter a decimal number:"); scanf(%d",&n); while(n>0) { r=n%2; b[c]=r; n=n/2; c++; } printf("\nThe Binary Equivalent is:"); for(i=c-1;i>=0;i--) printf("%d",b[i]); getch(); }

/*Reverse of string*/ #include<stdio.h> #include<conio.h> void main(void) { char a[30], b[30]; int j,length; clrscr(); printf("Enter string:"); gets(a); length=strlen(a); printf("Length of string is:%d",length); for(j=0;j<=length;j++) { b[j]=a[length-1-j]; } printf("\nFinal String is: %s",b); getch(); }

/*sum of digits of integers*/ #include<stdio.h> #include<conio.h> void main(void) { int n,r,s=0; clrscr(); printf("Enter a number:"); scanf("%d",&n); while(n>0) { r=n%10; s=s+r; n=n/10; } printf("\nThe sum of the digits is:%d",s); getch(); }

//Type Casting #include<stdio.h> #include<conio.h> void main(void) { clrscr(); int i=9,j=2; float value; //value=(float)i/(float)j; //value=float(i)/float(j); printf("value = %f",value); getch(); }

//String #include<stdio.h> #include<conio.h> void main(void) { char name[6]={'h','e','l','l','o','o','p','\0'}; printf("%s",name); getch(); }

//increment operator #include<stdio.h> #include<conio.h> void main(void) { int num=0; clrscr(); printf("\nNumber =%d\n",num++); printf("Number =%d\n",++num); printf("Number =%d\n",num); getch(); }

Page 4: Programs discussed so far!

/*Swapping of strings*/ #include<stdio.h> #include<conio.h> void main(void) { char fstr[30], sstr[30], temp[30]; clrscr(); printf("\n Enter string1:"); gets(fstr); printf("\n Enter string2:"); gets(sstr); printf("\nString before swapping \n String1:: %s \n string2::%s",fstr,sstr); strcpy(temp,fstr); strcpy(fstr,sstr); strcpy(sstr,temp); printf("\nString before swapping \n String1:: %s \n string2::%s",fstr,sstr); getch(); }

/*Total Number of vowels*/ #include<stdio.h> #include<conio.h> void main(void) { clrscr(); int i,j; char a[21]; printf("\n Please enter String:"); gets(a); for(i=0,j=0;a[i]!='\0';i++) { if(a[i]=='a' || a[i]=='A' || a[i]=='e' || a[i]=='E' || a[i]=='i' || a[i]=='I' || a[i]=='o' || a[i]=='O' || a[i]=='u' || a[i]=='U') j++; } printf("Total no. of Vowels are:%d",j); getch(); }

//Unions Example #include<stdio.h> #include<conio.h> void main(void) { union intflo { int intnum; float fltnum; } unex; printf("size of(union intflo)=%d\n",sizeof(union intflo); unex.intnum=734; printf("unex.intnum=%d\n",unex.intnum); unex.fltnum=867.43; printf("unex.fltnum=%.2f\n",unex.fltnum); getch(); }

//Structure Example #include<stdio.h> #include<conio.h> void main(void) { struct easy { int num; char ch; } struct easy ez1; ez1.num=2; ez1.ch='Z'; printf("ez1.num=%d,ez1.ch=%c",ez1.num,ez1.ch); getch(); }

//Stored address Example #include<stdio.h> #include<conio.h> void main(void) { int varx; char vary; printf("Address of varx:%x\n",&varx); printf("Address of vary:%x\n",&vary); getch(); }

//Pointers Example1 #include<stdio.h> #include<conio.h> void main(void) { int var=20; int *ip; ip=&var; printf("Address of var:%x\n",&var); printf("Address stored in pointer variable:%x\n",ip); printf("Value of *ip variable:%d\n",*ip); getch(); }

Page 5: Programs discussed so far!

/* BUBBLE SORT implementation using array ascending order*/ #include<stdio.h> #include<conio.h> void main(void) { clrscr(); int s, t, j, a[10]; printf("\nEnter total no of elements:"); scanf("%d",&s); printf("\nEnter %d elements",s); for(i=0;i<s;i++) scanf("%d",&a[i]); //Bubble Sorting for(i=s-2;i>=0;i--) { for(j=0;j<=1;j++) { if(a[j]>a[j+1]) { t=a[j]; a[j]=a[j+1]; a[j+1]=t; } } } printf("After sorting\n"); for(i=0;i<s;i++) printf("%d",d[i]); getch(); }

/*Alphabet and Vowel check #include<stdio.h> #include<conio.h> void main (void) { clrscr(); char J; printf("Enter a character:"); scanf("%c",&J); if((J>='a' && J<='z') || (J>='A' && J<='Z')) { printf("%c is an alphabet",J); if(J=='a' || J=='A' || J=='e' || J=='E' || J=='i' || J=='I' || J=='o' || J=='O' || J=='u' || J=='U') printf(" and a vowel"); else printf(" but not a vowel"); } else printf("%c is not an alphabet",J); getch(); }

//using 'getche' function #include<stdio.h> #include<conio.h> void main(void) { clrscr(); char name[20],ch='y'; float T1,T2,avg; while(ch=='y') { printf("Enter name:"); gets(name); printf("Enter marks for Test1:"); scanf("%f",&T1); printf("Enter marks for Test2:"); scanf("%f",&T2); avg=(T1+T2)/2; printf("Average=%f",avg); printf("More data (y/n)?"); ch=getche(); } }

//Factorial #include<stdio.h> #include<conio.h> void main(void) { clrscr(); int count,a; scanf(%d",&a); for(count=a;count>=1;count--) { a=a*(count-1); } printf("%d",a); getch(); }

Page 6: Programs discussed so far!

/*Greater among 3 number*/ #include<stdio.h> #include<conio.h> void main (void) { clrscr(); int i,a,b,c; printf("Enter three number with space[a!=b!=c]:"); scanf("%d %d %d",&a,&b,&c); if(a>b && a>c) printf("%d is the largest number",a); else if(b>a && b>c) printf("%d is the largest number",b); else printf("%d is the largest number",c); getch(); }

/*Print all the divisors of a given number*/ #include<stdio.h> #include<conio.h> void main(void) { clrscr(); int i, n; printf("Enter a number:"); scanf("%d",&n); printf("\n The divisors are\n\n"); for(i=1;i<=n;i++) { if(n%i==0) { printf("%d\t",i); } } getch(); }

/* Adding two numbers using Function*/ #include<stdio.h> #include<conio.h> //int add(int a,int b); int add(int a,int b) { int add; add=a+b; return add; } int main() { int num1,num2,sum; printf("\nEnter two numbers to add:"); scanf("%d %d",&num1,&num2); sum=add(num1,num2); printf("sum=%d",sum); getch(); }

//Character count without white spaces #include<stdio.h> #include<conio.h> void main(void) { int i=0,count=0; char str[20]; clrscr(); printf("Enter a string:"); gets(str);

while(str[i]!='\0') { if(str[i]!=' ') { count++; } i++; } printf("Character count:%d",count); getch(); }

//Character count with white spaces #include<stdio.h> #include<conio.h> void main(void) { clrscr(); int i=0; char str[20]; printf("Enter a string:"); gets(str); while(str[i]!='\0') { i++; } printf("Total characters are:%d",i); getch(); }

Page 7: Programs discussed so far!

//right angle triangle & its reflection type2 #include<stdio.h> #include<conio.h> void main(void) { int i,j,rows; clrscr(); printf("Enter number of rows:"); scanf("%d",&rows); for(i=1;i<=rows;++i) { for(j=1;j<=i;++j) { printf("%d",i); } printf("\n"); } for(k=i;k>=0;k--) { for(L=1;L<=k;L++) { printf("%d",k); } printf("\n"); } getch(); } * ** *** ** *

//right angle triangle & its reflection type1 #include<stdio.h> #include<conio.h> void main(void) { int i,j,rows; clrscr(); printf("Enter number of rows:"); scanf("%d",&rows); for(i=1;i<=rows;++i) { for(j=1;j<=i;++j) { printf("*"); } printf("\n"); } for(k=i;k>=0;k--) { for(L=1;L<=k;L++) { printf("*"); } printf("\n"); } getch(); } * ** *** ** *

/*Prime Number*/ #include<stdio.h> #include<conio.h> int prime(int a); void main(void) { clrscr(); int n; printf("enter a number:"); scanf("%d",&n); prime(n); getch(); } int prime(int a) { int i; char Ch='T'; for(i=2;i<a;i++) { if(a%i==0) { printf("\n%d is not a prime number",a); Ch='F'; break; } // above if ends here } //for loop ends here if(Ch=='T') printf("\n%d is a prime number",a); } //function definition ends here

Page 8: Programs discussed so far!

//PyramidType1 #include<stdio.h> #include<conio.h> void main(void) { int i,space,rows,k=0,L,m,space1; clrscr(); printf("Enter the number of rows:"); scanf("%d",&rows); for(i=1;i<=rows;i++) { for(space=1;space<=rows-i;space++) { printf(" "); } for(k=0;k<2*i-1;k++) { printf("*"); } printf("\n"); } for(L=i-1;L>=1;L--) { for(space1=1;space1<=rows-L;space1++) printf(" "); for(m=L;m<=2*L-1;m++) printf("*"); for(m=1;m<=L-1;m++) printf("*"); printf("\n"); } getch(); }

//PyramidType2 #include<stdio.h> #include<conio.h> void main(void) { int i,space,rows,k=0,L,m,space1; clrscr(); printf("Enter the number of rows:"); scanf("%d",&rows); for(i=1;i<=rows;i++) { for(space=1;space<=rows-i;space++) { printf(" "); } for(k=0;k<2*i-1;k++) { printf("*"); } printf("\n"); } for(L=i-1;L>=1;L--) { for(space1=1;space1<=rows-L+1;space1++) printf(" "); for(m=L;m<2*L-1;m++) printf("*"); for(m=1;m<L-1;m++) printf("*"); printf("\n"); } getch(); }

//Pyramid3 #include<stdio.h> #include<conio.h> void main (void) { int i,j,m,k,n,space; clrscr(); printf("Enter no.of rows:\n"); scanf("%d",&n); textcolor(RED); for(i=1;i<=n;i++) { for(space=1;space<=n-i;space++) { printf(" "); } for(k=1;k<=2*i-1;k++) { cprintf("8"); } printf("\n"); } textcolor(YELLOW); for(m=i-2;m>=1;m--) { for(j=1;j<i-m;j++) { printf(" "); } for(k=1;k<=2*m-1;k++) { cprintf("8"); } printf("\n"); } getch(); }

Page 9: Programs discussed so far!

//Graphics Example #include <graphics.h> #include <conio.h> main() { int gd = DETECT, gm; initgraph(&gd, &gm, "C:\\TC\\BGI"); setcolor(YELLOW); rectangle(0,30,639,450); settextstyle(SANS_SERIF_FONT,HORIZ_DIR,2); setcolor(WHITE); outtextxy(275,0,"Bar Chart"); setlinestyle(SOLID_LINE,0,2); line(100,420,100,60); line(100,420,600,420); line(90,70,100,60); line(110,70,100,60); line(590,410,600,420); line(590,430,600,420); outtextxy(95,35,"Y"); outtextxy(610,405,"X"); outtextxy(85,415,"O"); setfillstyle(LINE_FILL,BLUE); bar(150,100,200,419); setfillstyle(XHATCH_FILL,RED); bar(225,150,275,419); setfillstyle(WIDE_DOT_FILL,GREEN); bar(300,200,350,419); setfillstyle(INTERLEAVE_FILL,MAGENTA); bar(375,125,425,419); setfillstyle(HATCH_FILL,BROWN); bar(450,175,500,419); getch(); }

//string functions #include<stdio.h> #include<conio.h> void main(void) { clrscr(); char str1[12]="Hello"; char str2[12]="World"; char str3[12]; int len; /*copy str1 into str3*/ strcpy(str3,str1); printf("%s\n",str3); /*concatenate str1 & str2 */ strcat(str1,str2); printf("%s\n",str1); /*total length of str1 after concatenation*/ len=strlen(str1); printf("String length(str1):%d\n",len); getch(); }

/*Recursive Function to sum first n natural nos.*/ #include<stdio.h> #include<conio.h> int sum(int n); int main() { int add,num; printf("Enter a +ve integer:"); scanf("%d",&num); add=sum(num); printf("sum=%d",add); getch(); } int sum(int n) { if(n==0) return 0; else return (n+sum(n-1)); }

//Word count #include<stdio.h> #include<conio.h> void main(void) { int i=0,count=1; char str[20]; clrscr(); printf("Enter a string:"); gets(str);

while(str[i]!='\0') { if(str[i]==' ') { count++; } i++; } printf("Total words are:%d",count); getch(); }

Page 10: Programs discussed so far!

/* The following example shows the usage of isprint() function. */ #include <stdio.h> #include <conio.h> #include <ctype.h> void main(void) { int var1 = 'k'; int var2 = '8'; int var3 = '\t'; int var4 = ' '; if( isprint(var1) ) { printf("var1 = |%c| can be printed\n", var1 ); } else { printf("var1 = |%c| can't be printed\n", var1 ); } if( isprint(var2) ) { printf("var2 = |%c| can be printed\n", var2 ); } else { printf("var2 = |%c| can't be printed\n", var2 ); } if( isprint(var3) ) { printf("var3 = |%c| can be printed\n", var3 ); } else { printf("var3 = |%c| can't be printed\n", var3 ); } if( isprint(var4) ) { printf("var4 = |%c| can be printed\n", var4 ); } else { printf("var4 = |%c| can't be printed\n", var4 ); } getch(); }

/* The following example shows the usage of isalnum() function. */ #include <stdio.h> #include <conio.h> #include <ctype.h> void main(void) { int var1 = 'd'; int var2 = '2'; int var3 = '\t'; int var4 = ' '; if( isalnum(var1) ) {printf("var1 = |%c| is alphanumeric\n", var1 ); } else {printf("var1 = |%c| is not alphanumeric\n", var1 );} if( isalnum(var2) ) {printf("var2 = |%c| is alphanumeric\n", var2 ); } else {printf("var2 = |%c| is not alphanumeric\n", var2 );} if( isalnum(var3) ) {printf("var3 = |%c| is alphanumeric\n", var3 );} else {printf("var3 = |%c| is not alphanumeric\n", var3 );} if( isalnum(var4) ) {printf("var4 = |%c| is alphanumeric\n", var4 ); } else {printf("var4 = |%c| is not alphanumeric\n", var4 );} getch(); }

/* The following example shows the usage of isalpha() function. */ #include <stdio.h> #include <conio.h> #include <ctype.h> void main(void) { int var1 = 'd'; int var2 = '2'; int var3 = '\t'; int var4 = ' '; If( isalpha(var1) ) {printf("var1 = |%c| is an alphabet\n", var1 ); } else {printf("var1 = |%c| is not an alphabet\n",var1 );} if( isalpha(var2) ) {printf("var2 = |%c| is an alphabet\n", var2 ); } else {printf("var2 = |%c| is not an alphabet\n",var2 );} if( isalpha(var3) ) {printf("var3 = |%c| is an alphabet\n", var3 );} else {printf("var3 = |%c| is not an alphabet\n", var3 );} if( isalpha(var4) ) {printf("var4 = |%c| is an alphabet\n", var4 ); } else { printf("var4 = |%c| is not an alphabet\n", var4 ); } getch(); }

Page 11: Programs discussed so far!

/* The following example shows the usage of islower() function. */ #include <stdio.h> #include <conio.h> #include <ctype.h> void main(void) { int var1 = 'Q'; int var2 = 'q'; int var3 = '3'; if( islower(var1) ) {printf("var1 = |%c| is lowercase character\n", var1);} else {printf("var1 = |%c| is not lowercase character\n", var1 ); } if( islower(var2) ) { printf("var2 = |%c| is lowercase character\n", var2 ); } else { printf("var2 = |%c| is not lowercase character\n", var2 ); } if( islower(var3) ) { printf("var3 = |%c| is lowercase character\n", var3 ); } else { printf("var3 = |%c| is not lowercase character\n", var3 ); } getch(); }

/* The following example shows the usage of isspace() function. */ #include <stdio.h> #include <conio.h> #include <ctype.h> void main(void) { int var1 = 't'; int var2 = '1'; int var3 = ' '; if( isspace(var1) ) { printf("var1 = |%c| is a white-space character\n", var1 ); } else { printf("var1 = |%c| is not a white-space character\n", var1 ); } if( isspace(var2) ) { printf("var2 = |%c| is a white-space character\n", var2 ); } else { printf("var2 = |%c| is not a white-space character\n", var2 ); } if( isspace(var3) ) { printf("var3 = |%c| is a white-space character\n", var3 ); } else { printf("var3 = |%c| is not a white-space character\n", var3 ); } getch(); }

/* The following example shows the usage of isupper() function. */ #include <stdio.h> #include <conio.h> #include <ctype.h> void main(void) { int var1 = 'M'; int var2 = 'm'; int var3 = '3'; if( isupper(var1) ) { printf("var1 = |%c| is uppercase character\n", var1 ); } else { printf("var1 = |%c| is not uppercase character\n", var1 ); } if( isupper(var2) ) { printf("var2 = |%c| is uppercase character\n", var2 ); } else { printf("var2 = |%c| is not uppercase character\n", var2 ); } if( isupper(var3) ) { printf("var3 = |%c| is uppercase character\n", var3 ); } else { printf("var3 = |%c| is not uppercase character\n", var3 ); } getch(); }

Page 12: Programs discussed so far!

/*program that accept values in 2-Dimensional 3 by 3 array and displays the sum of all the elements */

#include<stdio.h> #include<conio.h> void main(void) { int arr[3][3], i, j, sum=0; /*Accepts input from the user and stores it in 2-D array*/ for(i=0;i<3;i++) { for(j=0;j<3;j++) { printf(“\nEnter the value for A[%d][%d]: “,i,j); scanf(“%d”,&arr[i][j]); } } /*Calculate sum of elements in 2-D array*/ for(i=0;i<3;i++) { for(j=0;j<3;j++) { sum=sum+arr[i][j]; } } /*Display the value of sum*/ printf(“\nThe sum of the elements of 2-D array is %d”, sum); getch(); }

/*Transpose means changing rows into columns and vice versa. */ #include<stdio.h> #include<conio.h> void main(void) { int a[10][10], b[10][10], m,n,I,j; printf(“\nEnter number of rows & column of array:”); scanf(“%d %d”,&m,&n); printf(“\nEnter elements of 2-D array:\n”); for(i=0;i<m;i++) { for(j=0;j<n;j++) { scanf(“%d”,&a[i][j]); } } printf(“\n\n2-D array before transposing :\n\n”); for(i=0;i<m;i++) { for(j=0;j<n;j++) { printf(“\t%d”,a[i][j]); } printf(“\n\n”); } /*Transposing array*/ for(i=0;i<m;i++) { for(j=0;j<n;j++) { b[j][i]=a[i][j]; } } printf(“\n\n2-D array after transposing:\n\n”); for(i=0;i<n;i++) { for(j=0;j<m;j++) { printf(“\t%d”,b[i][j]); } printf(“\n\n”); } getch(); }

/*Graphics*/ #include<conio.h> #define left 1 #define top 1 #define right 15 #define bot 1 void main(void) { clrscr(); window(left, top, right, bot); textcolor(12 or LIGHTRED); textbackground(6 or BROWN); cputs(“Chocolate”); gotoxy(1,1); getch(); }

/*Circle*/ #include<graphics.h> #include<conio.h> main() { int gd=DETECT,gm; initgraph(&gd,&gm,""); /* initialization of graphic mode */ circle(150,150,100); getch(); closegraph(); /* Restore orignal screen mode */ }

/*Rectangle*/ #include<graphics.h> #include<conio.h> main() { int gd = DETECT, gm; initgraph(&gd, &gm, "C:\\TC\\BGI"); rectangle(100,100,200,200); getch(); closegraph(); }

Page 13: Programs discussed so far!

/* The following example shows the usage of isdigit() function. */ #include <stdio.h> #include <conio.h> #include <ctype.h> void main(void) { int var1 = 'h'; if( isdigit(var1) ) { printf("var1 = |%c| is a digit\n", var1 ); } else { printf("var1 = |%c| is not a digit\n", var1 ); } getch(); }

/* The following example shows the usage of toupper() function. */ #include <stdio.h> #include <conio.h> #include <ctype.h> void main(void) { int i = 0; char str[] = "Tutorials Point"; while(str[i]) { putchar(toupper(str[i])); i++; } getch(); }

/* The following example shows the usage of tolower() function. */ #include <stdio.h> #include <conio.h> #include <ctype.h> void main(void) { int i = 0; char str[] = "TUTORIALS POINT"; while( str[i] ) { putchar(tolower(str[i])); i++; } getch(); }

Pointer is nothing but a variable which stores the address of another variable. While using the pointer we can store any data type suppose using an array we can be able to store particular defined data type only, but in pointers we can store any data type. It’s like a reference variable.