/*Program to show sum of 10 elements of array & show the average.*/ #include<stdio.h> int main() { int a[10],i,sum=0; float av; printf("enter elements of an aaray: "); for(i=0;i<10;i++) scanf("%d",&a[i]); for(i=0;i<10;i++) sum=sum+a[i]; printf("sum=%d",sum); av=sum/10; printf("average=%.2f",av); return 0; } Output: enter elements of an array: 4 4 4 4 4 4 4 4 4 4 sum=40 average=4.00
/*Program to find the maximum no. in an array.*/ #include<stdio.h> void main() { int a[5],max,i; printf("enter element for the array: "); for(i=0;i<5;i++) scanf("%d",&a[i]); max=a[0]; for(i=1;i<5;i++) { if(max<a[i]) max=a[i]; } printf("maximum no= %d",max); } Output: enter elements for array: 5 4 7 1 2 maximum no= 7
/*Swapping of two arrays*/ #include<stdio.h> void main(){ int a[10],b[10],c[10],i; printf("Enter First array->"); for(i=0;i<10;i++) scanf("%d",&a[i]); printf("\nEnter Second array->"); for(i=0;i<10;i++) scanf("%d",&b[i]); printf("Arrays before swapping"); printf("\nFirst array->"); for(i=0;i<10;i++){ printf("%d",a[i]); } printf("\nSecond array->"); for(i=0;i<10;i++){ printf("%d",b[i]); } for(i=0;i<10;i++){ //write any swapping technique c[i]=a[i]; a[i]=b[i]; b[i]=c[i]; } printf("\nArrays after swapping"); printf("\nFirst array->"); for(i=0;i<10;i++){ printf("%d",a[i]); } printf("\nSecond array->"); for(i=0;i<10;i++){ printf("%d",b[i]); } }
/*Print odd and even numbers of an array separately*/ #include<stdio.h> #define MAX 100 void main() { int arr[MAX],n,i; printf("Enter size of Array: "); scanf("%d",&n); printf("Enter %d positive elements\n",n); for(i=0;i<n;i++) { scanf("%d",&arr[i]); } printf("Even numbers: "); for(i=0;i<n;i++) { if(arr[i]%2==0) { printf("%d ",arr[i]); } } printf("\nOdd numbers: "); for(i=0;i<n;i++) { if(arr[i]%2!=0) { printf("%d ",arr[i]); } } }
/*Find maximum and second maximum in an array*/ #include<stdio.h> #define MAX 100 void main() { int arr[MAX],n,i,max,smax; printf("Enter size of Array: "); scanf("%d",&n); printf("Enter %d elements\n",n); for(i=0;i<n;i++) { scanf("%d",&arr[i]); } max=arr[0]; for(i=1;i<n;i++) { if(arr[i]>max) { smax=max; max=arr[i]; } } printf("Maximum : %d Second Maximum:%d ",max,smax); }
/*Addition of all elements of array*/ #include<stdio.h> #define MAX 100 void main() { int arr[MAX],n,i,sum=0; printf("Enter size of Array: "); scanf("%d",&n); printf("Enter %d elements\n",n); for(i=0;i<n;i++) { scanf("%d",&arr[i]); } for(i=0;i<n;i++) { sum= sum+arr[i]; } printf("Sum of array : %d ",sum); }
/* Reverse an array*/ #include<stdio.h> #define MAX 100 void main() { int arr[MAX],n,i,temp; printf("Enter size of Array: "); scanf("%d",&n); printf("Enter %d elements\n",n); for(i=0;i<n;i++) { scanf("%d",&arr[i]); } for(i=0;i<n/2;i++) { temp=arr[i]; arr[i]=arr[n-i-1]; arr[n-i-1]=temp; } printf("Array after reversing : "); for(i=0;i<n;i++) { printf("%d\t\n",arr[i]);; } }
/*Sorting of array (Selection sort)*/ #include<stdio.h> #define MAX 100 void main() { int arr[MAX],n,i,j,temp; printf("Enter size of Array: "); scanf("%d",&n); printf("Enter %d elements\n",n); for(i=0;i<n;i++) { scanf("%d",&arr[i]); } for(i=0;i<n;i++) { for(j=i+1;j<n;j++) { if(arr[i]>arr[j]) // for reverse order sorting change the sign > to < { temp=arr[i]; arr[i]=arr[j]; arr[j]=temp; } } } printf("Array Sorting : "); for(i=0;i<n;i++) { printf("%d\n",arr[i]);; } }
/*Find how many times a number present in array*/ #include<stdio.h> #define MAX 100 void main() { int arr[MAX],n,i,item,count=0,a[MAX]; printf("Enter size of Array: "); scanf("%d",&n); printf("Enter %d elements\n",n); for(i=0;i<n;i++) { scanf("%d",&arr[i]); } printf("Enter element you want to search: "); scanf("%d",&item); for(i=0;i<n;i++) { if(arr[i]==item) { a[count]=i+1; count++; } } if(count!=0) { printf("Item found at "); for(i=0;i<count;i++) { printf("%d ",a[i]); } } else { printf("Item not found"); } }
/*Remove duplicate number from array*/ #include<stdio.h> #define MAX 100 void main() { int arr[MAX],n,i,j,k,temp,size=0; printf("Enter size of Array: "); scanf("%d",&n); printf("Enter %d elements\n",n); for(i=0;i<n;i++) { scanf("%d",&arr[i]); } for(i=0;i<n-1-size;i++)//-size is used because the size of array { //is reducing after removing duplicate element for(j=i+1;j<n-size;j++) { if(arr[i]==arr[j]) { for(k=j;k<n-1-size;k++) { arr[k]=arr[k+1]; } size++; } } } printf("Total repeated Elements were : %d\n",size); printf("Array after removing duplicate element\n"); n=n-size;//reducing size of array by removing duplicate elements for(i=0;i<n;i++) { printf("%d\n",arr[i]); } }
/*binary to decimal conversion:*/ #include<stdio.h> void main(){ long int bn,dn=0,j=1,remainder; printf("Enter any number any binary number: "); scanf("%ld",&bn); while(bn!=0){ remainder=bn%10; dn=dn+remainder*j; j=j*2; bn=bn/10; } printf("Equivalent decimal value: %ld",dn); }
/*decimal to octal converter*/ #include<stdio.h> void main(){ long int dn,remainder,q; int on[100],i=1,j; printf("Enter any decimal number"); scanf("%ld",&dn); q = dn; while(q!=0){ on[i++]= q % 8; q = q/ 8; } printf("Equivalent octal value %d: ",dn); for(j = i -1 ;j> 0;j--) printf("%d",on[j]); }
/*Decimal to hexadecimal*/ #include<stdio.h> void main(){ long int dn,remainder,q; int i=1,j,temp; char hn[100]; printf("Enter any decimal number: "); scanf("%ld",&dn); q= dn; while(q!=0){ temp = q % 16; if( temp < 10) temp =temp + 48; else temp = temp + 55; hn [i++]= temp; q = q/ 16; } printf("Equivalent hexadecimal value of decimal number %d: ",d); for(j = i -1 ;j> 0;j--) printf("%c",hn[j]); }
/*Binary to decimal conversion*/ #include<stdio.h> void main(){ long int bn,dn=0,j=1,remainder; printf("Enter any number any binary number: "); scanf("%ld",&bn); while(bn!=0){ remainder=bn%10; dn=dn+remainder*j; j=j*2; bn=bn/10; } printf("Equivalent decimal value: %ld",dn); }
/*Octal number to decimal number*/ #include<stdio.h> #include<math.h> main() long int on,dn =0; int i=0; printf("Enter any octal number: "); scanf("%ld",&on); while(on!=0){ dn = dn + (on % 10) * pow(8,i++); onl = on/10; } printf("decimal value: %ld",dn); }
/*hexadecimal to binary conversion*/ #include<stdio.h> #define MAX 1000 void main(){ char hd[MAX]; long int i=0; printf("Enter any hexadecimal number: "); scanf("%s",hd); printf("\nEquivalent binary value: "); while(hd[i]){ switch(hd[i]){ case '0': printf("0000"); break; case '1': printf("0001"); break; case '2': printf("0010"); break; case '3': printf("0011"); break; case '4': printf("0100"); break; case '5': printf("0101"); break; case '6': printf("0110"); break; case '7': printf("0111"); break; case '8': printf("1000"); break; case '9': printf("1001"); break; case 'A': printf("1010"); break; case 'B': printf("1011"); break; case 'C': printf("1100"); break; case 'D': printf("1101"); break; case 'E': printf("1110"); break; case 'F': printf("1111"); break; case 'a': printf("1010"); break; case 'b': printf("1011"); break; case 'c': printf("1100"); break; case 'd': printf("1101"); break; case 'e': printf("1110"); break; case 'f': printf("1111"); break; default: printf("\nInvalid hexadecimal digit %c ",hd[i]); } i++; } }
/*C program to open a file and store data in it */ #include<stdio.h> void main(){ FILE *fp; char ch; fp=fopen("file.txt","w"); printf("\nEnter data to be stored in to the file: "); while((ch=getchar())!=EOF) putc(ch,fp); fclose(fp); }
/*Copy data from one file to other*/ #include<stdio.h> void main(){ FILE *p,*q; char file1[20],file2[20]; char ch; printf("\nEnter the source file name to be copied: "); gets(file1); p=fopen(file1,"r"); if(p==NULL){ printf("cannot open %s",file1); exit(0); } printf("\nEnter the destination file name: "); gets(file2); q=fopen(file2,"w"); if(q==NULL){ printf("cannot open %s",file2); exit(0); } while((ch=getc(p))!=EOF) putc(ch,q); printf("\nCOMPLETED"); fclose(p); fclose(q); }
/*Program which produces its own source code as its output*/ #include<stdio.h> void main(){ FILE *fp; char c; fp = fopen(__FILE__,"r"); do{ c= getc(fp); putchar(c); } while(c!=EOF); fclose(fp); }
/* Know the last date of modification of any file */ #include <stdio.h> #include <time.h> #include <sys\stat.h> int main(){ struct stat s; FILE *fp; fp=fopen("exmple.txt","r"); fstat(fileno(fp),&s); printf("Last date of modification : %s",ctime(&s.st_ctime)); return 0; }
/*Read a text file by c program*/ #include<stdio.h> void main(){ char str[50]; FILE *p; if((p=fopen("demofile.txt","r"))==NULL){ printf("\nUnable t open file demofile.txt"); exit(1); } while(fgets(str,50,p)!=NULL) puts(str); fclose(p); }
/*Write entire array to a file */ #include<stdio.h> #include<stdlib.h> int main(){ FILE *p; int i,arr[10]; if((p=fopen("demofile.txt", "wb"))==NULL){ printf("\nUnable to open file demofile.txt"); exit(1); } printf("\nEnter ten values\n"); for(i=0;i<10;i++) scanf("%d",&arr[i]); fwrite(arr,sizeof(arr),1,p); fclose(p); return 0; }
/*Write biodata of student created using nested structure in a file */ #include<stdio.h> typedef struct { int day; int month; int year; }date; typedef struct { char name[30]; char place[30]; int age; date birthdate; }biodata; void main() { FILE *fp; biodata student; int i; fp=fopen("st.dat","w"); printf("Enter name: "); scanf(" %30[^\n]%*c",student.name); printf("Enter place: "); scanf(" %30[^\n]%*c",student.place); printf("Enter age: "); scanf("%d",&student.age); printf("Enter birth date dd mm yy : "); scanf("%d",&student.birthdate.day); scanf("%d",&student.birthdate.month); scanf("%d",&student.birthdate.year); i =fprintf(fp,"Name :%s\nPlace: %s\nAge: %d \nDOB %d/%d/%d",student.name,student.place,student.age,student.birthdate.day, student.birthdate.month, student.birthdate.year); if(i!=0) printf("Data stored"); else printf("failed"); fclose(fp); }
/*Count total number of character and word in a file */ #include<stdio.h> #include<stdlib.h> void main() { FILE *fp; char ch; int tc=0,tw=1; fp=fopen("file.txt","r"); if(fp==NULL) { printf("File doesn't exist\n"); exit(1); } while(!feof(fp)) { ch=getc(fp); tc++; if(ch==' ') { tw++; } } printf("Total character: %d \nTotal words: %d",tc,tw); }
/*Concate two file into third file */ #include<stdio.h> #include<stdlib.h> void main() { FILE *fp1,*fp2,*fp3; char ch; int tc=0,tw=1; fp1=fopen("file1.txt","r"); if(fp1==NULL) { printf("File doesn't exist\n"); exit(1); } fp2=fopen("file2.txt","r"); if(fp2==NULL) { printf("File doesn't exist\n"); exit(1); } fp3=fopen("file3.txt","w"); while(!feof(fp1)) { ch=getc(fp1); putc(ch,fp3); } fclose(fp1); while(!feof(fp2)) { ch=getc(fp2); putc(ch,fp3); } fclose(fp2); fclose(fp3); printf("file1 and file2 are concatenated in file3") }
/*Print first line of a file*/ #include<stdio.h> #include<stdlib.h> void main() { FILE *fp; char str[50]; fp=fopen("file.txt","r"); if(fp==NULL) { printf("File doesn't exist\n"); exit(1); } fscanf(fp,"%s",str); printf("First line of the file: %s",str); fclose(fp); }
/*Program to find square of a number using functions.*/ #include<stdio.h> void main() { int rev(int); int r,a; clrscr(); printf("enter any no: "); scanf("%d",&a); r=rev(a); printf("square is : %d",r); } int rev(int x) { return(x*x); }
/*Program to swap two numbers using functions.*/ #include<stdio.h> void main() { void swap(int,int); int a,b,r; printf("enter value for a&b: "); scanf("%d%d",&a,&b); swap(a,b); } void swap(int a,int b) { int temp; temp=a; a=b; b=temp; printf("after swapping the value for a & b is : %d %d",a,b); }
/*Program to find factorial of a number using functions.*/ #include<stdio.h> int findFactorial(int); int main(){ int i,factorial,num; printf("Enter a number: "); scanf("%d",&num); factorial = findFactorial(num); printf("Factorial of %d is: %d",num,factorial); return 0; } int findFactorial(int num){ int i,f=1; for(i=1;i<=num;i++) f=f*i; return f; }
/*Program to print table of a number using functions.*/ #include<stdio.h> #include<conio.h> void main() { void table(); table(); getch(); } void table() { int n,i,r; printf("enter a no to know table: "); scanf("%d",&n); for(i=1;i<=10;i++) { r=n*i; printf("%d*%d=%d\n",n,i,r); } } Output: enter a no to know table: 2 2*1=2 2*2=4 2*3=6 2*4=8 2*5=10 2*6=12 2*7=14 2*8=16 2*9=18 2*10=20
/*Find largest of two numbers using functions.*/ #include<stdio.h> void main() { void max(); max(); } void max() { int a[5],max,n,i; printf("How many no̢۪s you want to enter: "); scanf("%d",&n); printf("Enter element for the array: "); for(i=0;i<n;i++) scanf("%d",&a[i]); max=a[0]; for(i=1;i<5;i++) { if(max<a[i]) max=a[i]; } printf("maximum no= %d",max); }
/*program to check whether a number is even or odd.*/ #include<stdio.h> #include<conio.h> void check (int); void main() { int a; printf("enter any integer"); scanf("%d",&a); check(a); } void check(int a) { if(a%2==0) printf("%d is even",a); else printf("%d is odd",a); }
/*Read an array and find out max.(Array as argument)*/ #include<stdio.h> int max(int[],int); void main() { int a[10],m,i; printf("enter 10 integers"); for(i=0;i<10;i++) scanf("%d",&a[i]); m=max(a,10); } int max(int a[],int n) { int m=-32768,i; for(i=0;i<n;i++) { if(a[i]>m) m=a[i]; } return (m); }
/*Check whether character is upper case or lower case.*/ #include<stdio.h> void check(char); void main() { char ch; printf("enter any character"); scanf("%c",&ch); check(ch); } void check(char c) { if(ch>=65&&ch<=90) printf("char is upper case"); else if(ch>=97&&ch<=122) printf("character is lower case"); }
/*Check whether year is leap or not*/ #include<stdio.h> void LeapYear(int); void main() { int year; printf("\n Enter the year : "); scanf("%d",&year); LeapYear(year); } void LeapYear(int yr) { int rem1,rem2; rem1 = yr%4 ; rem2 = yr%100; if((rem1 == 0) && (rem2!=0) || yr%400 == 0) { printf("\n The given year %d is Leap Year ",yr); } else { printf("\n The given year %d is Not Leap Year ",yr); } }
/*Print n number without using loop*/ #include<stdio.h> void print_numbers(int,int); void main() { int n; printf(" Enter the value of n: "); scanf("%d",&n); print_numbers(1,n); } void print_numbers(int i,int max) { printf("%3d\t",i); if(i<max) { print_numbers(++i,max); } return; }
/*Addition of two matrices using arrays source code. */ #include<stdio.h> void main(){ int a[3][3],b[3][3],c[3][3],i,j; printf("Enter the First matrix: "); for(i=0;i<3;i++) for(j=0;j<3;j++) scanf("%d",&a[i][j]); printf("\nEnter the Second matrix : "); for(i=0;i<3;i++) for(j=0;j<3;j++) scanf("%d",&b[i][j]); for(i=0;i<3;i++) for(j=0;j<3;j++) c[i][j]=a[i][j]+b[i][j]; printf("\nThe Addition of two matrix is\n"); for(i=0;i<3;i++){ printf("\n"); for(j=0;j<3;j++) printf("%d\t",c[i][j]); } }
/*Subtraction of matrix*/ #include<stdio.h> void main(){ int a[3][3],b[3][3],c[3][3],i,j; printf("Enter the First matrix: "); for(i=0;i<3;i++) for(j=0;j<3;j++) scanf("%d",&a[i][j]); printf("\nEnter the Second matrix: "); for(i=0;i<3;i++) for(j=0;j<3;j++) scanf("%d",&b[i][j]); for(i=0;i<3;i++) for(j=0;j<3;j++) c[i][j]=a[i][j]-b[i][j]; printf("\nThe Subtraction of two matrix is\n"); for(i=0;i<3;i++){ printf("\n"); for(j=0;j<3;j++) printf("%d\t",c[i][j]); } }
/*Multiplication of matrix*/ #include <stdio.h> void main () { int a[5][5], b[5][5], c[5][5], i, j, k, sum = 0, m, n, o, p; printf("\nEnter the row and column of first matrix"); scanf("%d %d", &m, &n); printf("\nEnter the row and column of second matrix"); scanf("%d %d", &o, &p); if (n != o) { printf("Matrix mutiplication is not possible"); printf("\nColumn of first matrix must be same as row of second matrix"); } else { printf("\nEnter the First matrix: "); for (i = 0; i < m; i++) { for (j = 0; j < n; j++) { scanf("%d", &a[i][j]); } } printf("\nEnter the Second matrix: "); for (i = 0; i < o; i++) { for (j = 0; j < p; j++) { scanf("%d", &b[i][j]); } } for (i = 0; i < m; i++) { for (j = 0; j < p; j++) { c[i][j] = 0; for (i = 0; i < m; i++) { //row of first matrix { for (j = 0; j < p; j++) { //column of second matrix { sum = 0; for (k = 0; k < n; k++) { sum = sum + a[i][k] * b[k][j]; } c[i][j] = sum; } } } } } } printf("\nThe multiplication of two matrix is\n"); for (i = 0; i < m; i++) { printf("\n"); for (j = 0; j < p; j++) { printf("%d\t", c[i][j]); } } }
/*Sum of diagonal elements of a matrix in c */ #include<stdio.h> void main(){ int a[10][10],i,j,sum=0,m,n; printf("\nEnter the row and column of matrix: "); scanf("%d%d",&m,&n); printf("\nEnter the elements of matrix: "); for(i=0;i<m;i++) for(j=0;j<n;j++) scanf("%d",&a[i][j]); printf("\nThe matrix is\n"); for(i=0;i<m;i++){ printf("\n"); for(j=0;j<m;j++){ printf("%d\t",a[i][j]); } } for(i=0;i<m;i++){ for(j=0;j<n;j++){ if(i==j) sum=sum+a[i][j]; } } printf("\nSum of the diagonal elements of a matrix is: %d",sum); }
/*Inverse of a 3x3 matrix */ #include<stdio.h> int main(){ int a[3][3],i,j; float determinant=0; printf("Enter the 9 elements of matrix: "); for(i=0;i<3;i++) for(j=0;j<3;j++) scanf("%d",&a[i][j]); printf("\nThe matrix is\n"); for(i=0;i<3;i++){ printf("\n"); for(j=0;j<3;j++) printf("%d\t",a[i][j]); } for(i=0;i<3;i++) determinant = determinant + (a[0][i]*(a[1][(i+1)%3]*a[2][(i+2)%3] - a[1][(i+2)%3]*a[2][(i+1)%3])); printf("\nInverse of matrix is: \n"); for(i=0;i<3;i++){ for(j=0;j<3;j++) printf("%.3f\t",((a[(i+1)%3][(j+1)%3] * a[(i+2)%3][(j+2)%3]) - (a[(i+1)%3][(j+2)%3]*a[(i+2)%3][(j+1)%3]))/ determinant); printf("\n"); } }
/*Transpose of a matrix*/ #include<stdio.h> main(){ int a[10][10],b[10][10],i,j,k=0,m,n; printf("\nEnter the row and column of matrix"); scanf("%d %d",&m,&n); printf("\nEnter the First matrix: "); for(i=0;i<m;i++) for(j=0;j<n;j++) scanf("%d",&a[i][j]); printf("\nThe matrix is\n"); for(i=0;i<m;i++){ printf("\n"); for(j=0;j<m;j++){ printf("%d\t",a[i][j]); } } for(i=0;i<m;i++) for(j=0;j<n;j++) b[i][j]=0; for(i=0;i<m;i++){ for(j=0;j<n;j++){ b[i][j]=a[j][i]; printf("\n%d",b[i][j]); } } printf("\nTraspose of a matrix is : \nâ€Å“); for(i=0;i<m;i++){ printf("\n"); for(j=0;j<m;j++){ printf("%d\t",b[i][j]); } } }
/*Determinant of 3X3 matrix:*/ #include<stdio.h> void main(){ int a[3][3],i,j; long determinant; printf("Enter the 3*3 elements of matrix: "); for(i=0;i<3;i++) for(j=0;j<3;j++) scanf("%d",&a[i][j]); printf("\n Matrix is\n"); for(i=0;i<3;i++){ printf("\n"); for(j=0;j<3;j++) printf("%d\t",a[i][j]); } determinant = a[0][0]*((a[1][1]*a[2][2]) - (a[2][1]*a[1][2])) -a[0][1]*(a[1][0]*a[2][2] - a[2][0]*a[1][2]) + a[0][2]*(a[1][0]*a[2][1] - a[2][0]*a[1][1]); printf("\nDeterminant of 3X3 matrix: %ld",determinant); }
/*C program to check perfect number Definition: A number whose factors (other than itself) add up to the number. Example : 6 = 1 + 2 + 3, 28 = 1 + 2 + 4 + 7 + 14, 496 = 1 + 2 + 4 + 8 + 16 + 31 + 62 + 124 + 248 */ /*Code 1: to check a number is perfect or not*/ #include<stdio.h> void main(){ int n,i=1,sum=0; printf("Enter a number: "); scanf("%d",&n); while(i<n){ if(n%i==0) sum=sum+i; i++;} if(sum==n) printf("%d is a perfect number",i); else printf("%d is not a perfect number",i); } output: Enter a number: 6 6 is a perfect number /*Code 2: For min and max range*/ #include<stdio.h> void main(){ int n,i,sum; int min,max; printf("Enter the minimum range: "); scanf("%d",&min); printf("Enter the maximum range: "); scanf("%d",&max); printf("Perfect numbers in given range is: "); for(n=min;n<=max;n++){ i=1; sum = 0; while(i<n){ if(n%i==0) sum=sum+i; i++; } if(sum==n) printf("%d ",n);} } output: Enter the minimum range: 1 Enter the maximum range: 20 Perfect numbers in given range is: 6
/*Check the given number is Armstrong number or not using c program .*/ /* definition: Those numbers which sum of its digits to power of number of its digits is equal to that number are known as Armstrong numbers. Those three digit numbers which sum of the cube of its digits is equal to that number are known as three digit Armstrong numbers. For example 153 since 1^3 + 5^3 + 3^3 = 1+ 125 + 9 =153 Example : 1634 Total digits in 1634 is 4 And 1^4 + 6^4 + 3^4 +4^4 = 1 + 1296 + 81 + 64 =1634 Examples of Armstrong numbers are 1, 2, 3, 4, 5, 6, 7, 8, 9, 153, 370, 371, 407, 1634, 8208, 9474, 54748, 92727, 93084, 548834, 1741725 */ /*Code 1(works for 3 digit number)*/ #include<stdio.h> void main(){ int num,r,sum=0,temp; printf("Enter a number: "); scanf("%d",&num); temp=num; while(num!=0){ r=num%10; num=num/10; sum=sum+(r*r*r); } if(sum==temp) printf("%d is an Armstrong number",temp); else printf("%d is not an Armstrong number",temp); } output: Enter a number: 153 153 is an Armstrong number /*For greater than three digit number*/ #include<stdio.h> void main() { int num,m,temp,k,x=0,l=0,i,sum=1,rem; printf("Enter Number For Checking Armstrong : "); scanf("%d",&num); m=num; while(m>0) { k=m%10; m=m/10; if(k>0) { l=l+1; } } temp = num; while (num >0) {sum=1; rem = num % 10; for(i=1;i<=l;i++) { sum = sum * rem;} x= x+sum; num = num / 10; } if(temp == x) printf("%d is Amstrong Number",temp); else printf("%d is not Amstrong Number",temp); } /* Code 2: Program for Armstrong number in range of three digit*/ #include<stdio.h> void main(){ int num,r,sum,temp; int min,max; printf("Enter the minimum range: "); scanf("%d",&min); printf("Enter the maximum range: "); scanf("%d",&max); printf("Armstrong numbers in given range are: "); for(num=min;num<=max;num++){ temp=num; sum = 0; while(temp!=0){ r=temp%10; temp=temp/10; sum=sum+(r*r*r); } if(sum==num) printf("%d ",num); } } output: Enter the minimum range: 1 Enter the maximum range: 200 Armstrong numbers in given range are: 1 153 /*Code 3: Armstrong number in c using for loop */ #include<stdio.h> void main(){ int num,r,sum=0,temp; printf("Enter a number: "); scanf("%d",&num); for(temp=num;num!=0;num=num/10){ r=num%10; sum=sum+(r*r*r); } if(sum==temp) printf("%d is an Armstrong number",temp); else printf("%d is not an Armstrong number",temp); } output: Enter a number: 370 370 is an Armstrong number
/*Check given number is prime number or not using c program.*/ /* Definition of prime number: A natural number greater than one has not any other divisors except 1 and itself. In other word we can say which has only two divisors 1 and number itself. For example: 7 Their divisors are 1 and 7. */ #include<stdio.h> void main(){ int num,i,count=0; printf("Enter a number: "); scanf("%d",&num); for(i=2;i<=num/2;i++){ if(num%i==0){ count++; break; } } if(count==0 && num!= 1) printf("%d is a prime number",num); else printf("%d is not a prime number",num); return 0; } output: Enter a number: 5 5 is a prime number /* Code 2: print prime numbers from 1 to 100 */ #include<stdio.h> void main(){ int num,i,count; for(num = 1;num<=100;num++){ count = 0; for(i=2;i<=num/2;i++){ if(num%i==0){ count++; break; } } if(count==0 && num!= 1) printf("%d ",num); } } Output: 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
/*Check strong number or not. Definition of strong number: A number is called strong number if sum of the factorial of its digit is equal to number itself. For example: 145 since 1! + 4! + 5! = 1 + 24 + 120 = 145 */ /*Code 1:*/ #include<stdio.h> void main(){ int num,i,f,r,sum=0,temp; printf("Enter a number: "); scanf("%d",&num); temp=num; while(num){ i=1,f=1; r=num%10; while(i<=r){ f=f*i; i++;} sum=sum+f; num=num/10; } if(sum==temp) printf("%d is a strong number",temp); else printf("%d is not a strong number",temp); } output: Enter a number: 145 145 is a strong number /* Code 2: min and max range*/ #include<stdio.h> int main(){ int num,i,f,r,sum,temp; int min,max; printf("Enter minimum range: "); scanf("%d",&min); printf("Enter maximum range: "); scanf("%d",&max); printf("Strong numbers in given range are: "); for(num=min; num <= max; num++){ temp = num; sum=0; while(temp){ i=1; f=1; r=temp%10; while(i<=r){ f=f*i; i++; } sum=sum+f; temp=temp/10; } if(sum==num) printf("%d ",num);} } output: Enter minimum range: 100 Enter maximum range: 100000 Strong numbers in given range are: 145 40585
/*Program to generate the Fibonacci series Definition of Fibonacci numbers: A series of numbers in which each sequent number is sum of its two previous numbers is known as Fibonacci series and each numbers are called Fibonacci numbers. First two numbers are assumed to be 0 and 1 So Fibonacci numbers is Algorithm for Fibonacci series Fn = Fn-2 + Fn-1 */ #include<stdio.h> int main(){ int k=2,r; long int i=0l,j=1,f; printf("Enter the number range:"); scanf("%d",&r); printf("Fibonacci series is: %ld %ld",i,j); while(k<r){ f=i+j; i=j; j=f; printf(" %ld",j); k++; } return 0; } output: Enter the number range: 14 Fibonacci series is: 0 1 1 2 3 5 8 13 21 34 55 89 144 233
/*Program to check a number is palindrome*/ #include<stdio.h> void main(){ int num,r,sum=0,temp; printf("Enter a number: "); scanf("%d",&num); temp=num; while(num){ r=num%10; num=num/10; sum=sum*10+r; } if(temp==sum) printf("%d is a palindrome",temp); else printf("%d is not a palindrome",temp); } Sample output: Enter a number: 171 171 is a palindrome
/* A AB ABC ABCD ABCDE */ #include <stdio.h> int main() { int i, j; for(i=1;i<=5;i++) { for(j=0;j<i;j++) { printf("%c",'A' + j); } printf("\n"); } return 0; }
/* ABCDE ABCDE ABCDE ABCDE ABCDE */ #include <stdio.h> int main() { int i, j; for(i=5;i>=1;i--) { for(j=0;j<5;j++) { printf("%c",'A' + j); } printf("\n"); } return 0; }
/* E DE CDE BCDE ABCDE */ #include <stdio.h> int main() { int i, j; for(i=4;i>=0;i - -) { for(j=i;j<5;j++) { printf("%c",'A' + j); } printf("\n"); } return 0; }
/* A BA CBA DCBA EDCBA */ #include <stdio.h> int main() { int i, j; for(i=0;i<=4;i++) { for(j=i;j>=0;j--) { printf("%c",'A' + j); } printf("\n"); } return 0; }
/* E ED EDC EDCB EDCBA */ #include <stdio.h> int main() { int i, j; for(i=4;i>=0;i--) { for(j=4;j>=i;j--) { printf("%c",'A' + j); } printf("\n"); } return 0; }
/* AAAAA BBBB CCC DD E */ #include <stdio.h> int main() { int i, j; for(i=0;i<=4;i++) { for(j=4;j>=i;j--) { printf("%c",'A'+ i); } printf("\n"); } return 0; }
/* A BB CCC DDDD EEEEE */ #include <stdio.h> int main() { int i, j; for(i=0;i<=4;i++) { for(j=0;j<=i;j++) { printf("%c",'A' + i); } printf("\n"); } return 0; }
/* EDCBA EDCB EDC ED E */ #include <stdio.h> int main() { int i, j; for(i=0;i<=4;i++) { for(j=4;j>=i;j--) { printf("%c",'A' + j); } printf("\n"); } return 0; }
/* EDCBA DCBA CBA BA A */ #include <stdio.h> int main() { int i, j; for(i=4;i>=0;i--) { for(j=i;j>=0;j--) { printf("%c",'A'+ j); } printf("\n"); } return 0; }
/* ABCDE BCDE CDE DE E */ #include <stdio.h> int main() { int i, j; for(i=0;i<=4;i++) { for(j=i;j<=4;j++) { printf("%c", 'A' + j); } printf("\n"); } return 0; }
/* E DD CCC BBBB AAAAA */ #include <stdio.h> int main() { int i, j; for(i=4;i>=0;i--) { for(j=4;j>=i;j--) { printf("%c",'A' + i); } printf("\n"); } return 0; }
/* 1 12 123 1234 12345 */ #include<stdio.h> void main() { int i, j; for(i=1;i<=5;i++) { for(j=1;j<=i;j++) { printf("%d",j); } printf("\n");} }
/* 12345 1234 123 12 1 */ #include<stdio.h> void main() { int i, j; for(i=5;i>=1;i--) { for(j=1;j<=i;j++) { printf("%d",j); } printf("\n"); } }
/* 54321 4321 321 21 1 */ #include<stdio.h> void main() { int i, j; for(i=5;i>=1;i--) { for(j=i;j>=1;j--) { printf("%d",j); } printf("\n"); } }
/* 12345 2345 345 45 5 */ #include<stdio.h> void main() { int i, j; for(i=1;i<=5;i++) { for(j=i;j<=5;j++) { printf("%d",j); } printf("\n"); } }
/* 1 21 321 4321 54321 */ #include<stdio.h> void main() { int i, j; for(i=1;i<=5;i++) { for(j=i;j>=1;j--) { printf("%d",j); } printf("\n"); } }
/* 1 22 333 4444 55555 */ #include<stdio.h> void main() { int i, j; for(i=1;i<=5;i++) { for(j=1;j<=i;j++) { printf("%d",i); } printf("\n"); } }
/* 1 2 4 1 3 5 2 4 6 8 1 3 5 7 9 */ #include<stdio.h> void main() { int i,j,k; for(i=1;i<=5;i++) { if(i%2==0) { k=2; } else { k=1; } for(j=1;j<=i;j++,k+=2) { printf("%d ", k); } printf("\n"); } }
/* 1 2 3 4 5 6 7 8 9 10 36 37 38 39 40 41 42 43 44 11 35 64 65 66 67 68 69 70 45 12 34 63 84 85 86 87 88 71 46 13 33 62 83 96 97 98 89 72 47 14 32 61 82 95 100 99 90 73 48 15 31 60 81 94 93 92 91 74 49 16 30 59 80 79 78 77 76 75 50 17 29 58 57 56 55 54 53 52 51 18 28 27 26 25 24 23 22 21 20 19 */ #include<stdio.h> void main() { int a[10][10]={0},i,j,low=0,top=9,n=1; for(i=0;i<5;i++,low++,top--) { for(j=low;j<=top;j++,n++) a[i][j]=n; for(j=low+1;j<=top;j++,n++) a[j][top]=n; for(j=top-1;j>=low;j--,n++) a[top][j]=n; for(j=top-1;j>low;j--,n++) a[j][low]=n; } printf("\t\t\tPerfect Square\n"); for(i=0;i<10;i++) { printf("\n\n\t"); for(j=0;j<10;j++) { printf("%6d",a[i][j]); delay(300); } }
/* 5432* 543*1 54*21 5*321 *4321 */ #include<stdio.h> void main() { int i,j; for(i=1;i<=5;i++) { for(j=5;j>=1;j--) { if(i==j) printf("*"); else printf("%d",j); } printf("\n"); } }
/* 1 121 12321 1234321 */ #include<stdio.h> void main() { int i,j; for(i=1;i<=4;i++) { for(j=1;j<=i;j++) printf("%d",j); for(j=i-1;j>=1;j--) printf("%d",j); printf("\n"); } }
/* 1 1 12 21 123 321 1234 4321 1234554321 */ #include<stdio.h> void main() { int i,j,k; for(i=1;i<=5;i++) { for(j=1;j<=5;j++) { if(j<=i) printf("%d",j); else printf(" "); } for(j=5;j>=1;j--) { if(j<=i) printf("%d",j); else printf(" "); } printf("\n"); } }
/* 1 2*2 3*3*3 4*4*4*4 4*4*4*4 3*3*3 2*2 1 */ #include<stdio.h> void main() { int i,j; for(i=1;i<=4;i++) { for(j=1;j<=i;j++) { if(j<i) printf("%d*",i); else printf("%d",i); } printf(" \n"); } for(i=4;i>=1;i--) { for(j=1;j<=i;j++) { if(j<i) printf("%d*",i); else printf("%d",i); } printf(" \n"); } }
/* 1 121 12321 1234321 123454321 1234321 12321 121 1 */ #include<stdio.h> void main() { int num,r,c,sp; printf("Enter number of rows : "); scanf("%d",&num); for(r=1; r<=num; r++) { for(sp=num-r; sp>=1; sp--) printf(" "); for(c=1; c<=r; c++) printf("%d",c); for(c=r-1; c>=1; c--) printf("%d",c); printf("\n"); } for(r=1; r<=num; r++) { for(sp=r; sp>=1; sp--) printf(" "); for(c=1; c<=(num-r); c++) printf("%d",c); for(c=num-r-1; c>=1; c--) printf("%d",c); printf("\n"); } }
/* 0 909 89098 7890987 678909876 56789098765 4567890987654 345678909876543 23456789098765432 1234567890987654321 */ #include<stdio.h> void main() { int i,j; printf("0\n"); for(i=9;i>=1;i--) { for(j=i;j<=9;j++) printf("%d",j); printf("0"); for(j=9;j>=i;j--) printf("%d",j); printf("\n"); } }
/* 1 3 5 7 9 11 13 15 17 19 */ #include<stdio.h> void main() { int n=2,r,c,z=3; printf("1\n"); for(r=1; r<=2; r++) { for(c=1; c<=r*3; c++,z=z+2) printf("%d ",z); printf("\n"); } }
/* 11111 0000 111 00 1 */ #include<stdio.h> void main() { int i, j; for(i=5;i>=1;i--) { for(j=1;j<=i;j++) { printf("%d",i%2); } printf("\n"); } }
/* 11111 1 1 1 1 1 1 11111 */ #include<stdio.h> void main() { int i,j; for(i=1;i<=5;i++) { for(j=1;j<=5;j++) { if(j==5 || j==1 || i==1 || i==5) printf("1"); else printf(" "); } printf("\n"); } }
/* 1 2 4 3 6 9 4 8 12 16 5 10 15 20 25 6 12 18 24 30 36 7 14 21 28 35 42 49 8 16 24 32 40 48 56 64 9 18 27 36 45 54 63 72 81 10 20 30 40 50 60 70 80 90 100 */ #include<stdio.h> void main() { int i,j; for(i=1;i<=10;i++) { for(j=1;j<=i;j++) { printf("%d ",i*j); } printf("\n"); } }
/* 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 */ #include<stdio.h> int fib(int); void main() { int i,j,k=1; for(i=1;i<=5;i++) { for(j=1;j<=i;j++) { printf("%d ",fib(k++)); } printf("\n"); } } int fib(int n) { if(n<=1) return n; return(fib(n-1)+fib(n-2)); }
/*design a number rhombus pattern 1 1 2 2 3 3 4 4 3 3 2 2 1 1 */ #include<stdio.h> void main() { int num,i,c,s,n; printf("Enter maximum number : "); scanf("%d", &num); for(i=1; i<=num; i++) { for(s=num-i; s>=1; s--) printf(" "); printf("%d", i); for(s=i*2; s>1; s--) printf(" "); printf("%d", i); printf("\n"); } for(i=1,n=num-1; i<num; i++,n--) { for(s=i; s>=1; s--) printf(" "); printf("%d",n); for(s=n*2; s>1; s--) printf(" "); printf("%d", n); printf("\n"); } }
/* 4444444 4333334 4322234 4321234 4322234 4333334 4444444 */ #include<stdio.h> void main() { int i,j,k; for(i=4;i>=1;i--) { for(j=4;j>=i;j--) printf("%d",j); for(j=1;j<(i*2)-1;j++) printf("%d",i); for(j=i+1;j<=4;j++) printf("%d",j); printf("\n"); } for(i=2;i<=4;i++) { for(j=4;j>=i;j--) printf("%d",j); for(j=1;j<(i*2)-1;j++) printf("%d",i); for(j=i+1;j<=4;j++) printf("%d",j); printf("\n"); } }
/*Floyd's triangle Floyd's triangle is a right angled-triangle using the natural numbers. Examples of Floyd̢۪s triangle: Example 1 2 3 4 5 6 7 8 9 10 */ #include<stdio.h> void main(){ int i,j,r,k=1; printf("Enter the range: "); scanf("%d",&r); printf("FLOYD'S TRIANGLE\n\n"); for(i=1;i<=r;i++){ for(j=1;j<=i;j++,k++) printf(" %d",k); printf("\n"); } }
/*-- * ** *** **** ***** --*/ #include<stdio.h> void main() { int i, j, n; printf("Enter no. of rows: "); scanf("%d",&n); for(i=1;i<=n;i++) { for(j=1;j<=i;j++) { printf("*"); } printf("\n"); } }
/*-- * ** *** **** ***** --*/ #include<stdio.h> void main() { int i, j, n; printf("Enter no. of rows: "); scanf("%d",&n); for(i=n;i>=1;i--) { for(j=1;j<i;j++) { printf(" "); } for(j=5;j>=i;j--) { printf("*"); } printf("\n"); } }
/*-- ***** **** *** ** * --*/ #include<stdio.h> void main() { int i, j,n; printf("Enter no. of rows: "); scanf("%d",&n); for(i=n;i>=1;i--) { for(j=n;j>i;j--) { printf(" "); } for(j=1;j<=i;j++) { printf("*"); } printf("\n"); } }
/*-- * *** ***** ******* ********* --*/ #include<stdio.h> void main() { int i, j, n; printf("Enter no. of rows: "); scanf("%d",&n); for(i=1;i<=n;i++) { for(j=i;j<n;j++) { printf(" "); } for(j=1;j<(i*2);j++) { printf("*"); } printf("\n"); } }
/*-- ********** **** **** *** *** ** ** * * ** ** *** *** **** **** ********** --*/ #include<stdio.h> void main() { int i, j, k; for(i=1;i<=5;i++) { for(j=1;j<=6-i;j++) { printf("*"); } for(k=1;k<i;k++) { printf(" "); } for(j=1;j<=6-i;j++) { printf("*"); } printf("\n"); } for(i=2;i<=5;i++) { for(j=1;j<=i;j++) { printf("*"); } for(k=1;k<=5-i;k++) { printf(" "); } for(j=1;j<=i;j++) { printf("*"); } printf("\n"); } }
/* * ** * * * * * * * * * * * * * * * * * * ************ --*/ #include<stdio.h> void main() { int num,r,j,s; printf("Enter no of rows: "); scanf("%d", &num); printf("\n*\n"); for(r=1; r<=num; r++) { printf("*"); for(s=1; s<r; s++) printf(" "); printf("*\n"); } for(j=1; j<=num+2; j++) printf("*"); }
/*-- ******** * * * * ******** --*/ #include<stdio.h> void main() { int cols,rows,r,c,s; printf("Enter no. of columns: "); scanf("%d", &cols); printf("Enter no. of rows: "); scanf("%d", &rows); for(r=1; r<=cols; r++) printf("*"); printf("\n"); for(c=1; c<=rows-2; c++) { printf("*"); for(s=1; s<=cols-2; s++) printf(" "); printf("*\n"); } for(r=1; r<=cols; r++) printf("*"); }
/* * * ** ** * * * * * * * /* print m shape pyramid in c*/ #include<stdio.h> int main() { int num=5,j,i; for(i=1; i<=num; i++) { for(j=1; j<=num; j++) { if( (j==2 || j==3 || j==4) && (i==1) ) printf(" "); else if( (j==3) && (i==2) ) printf(" "); else if( (j==2 || j==4) && (i==3) ) printf(" "); else if( (j==2 || j==3 || j==4 ) && (i==4 || i==5) ) printf(" "); else printf("*"); } printf("\n"); } return 0; }
/* * * * * * * * * * * */ #include<stdio.h> int main() { int i,j,n; printf("Enter value of n: "); scanf("%d",&n); for(i=1;i<=n;i++) { for(j=i;j<=n;j++) { printf(" "); } for(j=1;j<=i;j++) { printf("* "); } printf("\n"); } return 0; }
/* * * * * * * * * * * */ #include<stdio.h> int main() { int i,j,n; printf("Enter no. of row: "); scanf("%d",&n); for(i=1;i<=n;i++) { for(j=1;j<i;j++) { printf(" "); } for(j=i;j<=n;j++) { printf("* "); } printf("\n"); } return 0; }
/*Pointer to structure*/ /* A pointer which is pointing to a structure is know as pointer to structure. */ #include<stdio.h> struct book{ char *name; char author[10]; int id; }bk={"Learn","snj",1}; void main(){ struct book *b=&bk; printf("Name: %s, Author: %s , Id: %d",b->name,(*b).author,b->id); } /*Explanation: p is pointer to structure address. -> and (*). Both are same thing. */
/* Pointer to Pointer or multiple pointer*/ #include<stdio.h> void main(){ int a=5,*ptr3=&a,**ptr2=&ptr3,***ptr1=&ptr2; printf("%d",ptr1[0][0][0]); } Output: 5 /* Explanation: ptr[i] =*(ptr+i) So, ptr1[0][0][0]=*(ptr1[0][0]+0)=**ptr1[0]=***ptr1 Another rule is: *&i=i but &*i is wrong So, ***ptr1=***(&ptr2) =**ptr2=**(&ptr2) =*ptr3= *(&a)= a=5 */
/*Reading an Array using pointer and sorting it*/ #include<stdio.h> int main(){ int i,j,temp1,temp2; int arr[10]; int *ptr; ptr=&arr; printf("Enter 10 Elements "); for(i=0;i<10;i++) { scanf("%d",ptr); ptr++; } for(i=0;i<10;i++){ for(j=0;j<10-i;j++){ if(*(arr+j)>*(arr+j+1)){ ptr=arr+j; temp1=*ptr++; temp2=*ptr; *ptr--=temp1; *ptr=temp2; } } } for(i=0;i<10;i++) printf(" %d",arr[i]); }
/* Factorial program using pointer */ #include<stdio.h> void findFactorial(int,int *); int main(){ int i,factorial,num; printf("Enter number: "); scanf("%d",&num); findFactorial(num,&factorial); printf("Factorial of %d is: %d",num,*factorial); return 0; } void findFactorial(int num,int *factorial){ int i; *factorial =1; for(i=1;i<=num;i++) *factorial=*factorial*i; }
/*Swapping array by pointers*/ #include<stdio.h> void swap(int *, int *); main() { int a[10],b[10],i; printf("Enter First array: "); for(i=0;i<10;i++) scanf("%d",&a[i]); printf("\nEnter Second array: "); for(i=0;i<10;i++) scanf("%d",&b[i]); for(i=0;i<10;i++) swap(&a[i], &b[i]); printf("\nArrays after swapping"); printf("\nFirst array: "); for(i=0;i<10;i++) printf("%d ",a[i]); printf("\nSecond array: "); for(i=0;i<10;i++) printf("%d ",b[i]); } void swap(int *arr1, int *arr2) { int temp; temp = *arr1; *arr1 = *arr2; *arr2 = temp; }
/*Area and Perimeter of Rectangle using pointer*/ #include <stdio.h> void rectangle(int a, int b, int * area, int * perim); void main() { int x, y; int area, perim; printf("Enter two values separated by space: " ); scanf("%d %d", &x, &y); rectangle(x, y, &area, &perim); printf("Area is %d Perimeter is %d\n", area, perim); } void rectangle(int a, int b, int * area,int * perim) { *area = a * b; *perim = 2 * (a + b); }
/*Concatenate two string using pointer*/ #include<stdio.h> int main(){ int i=0,j=0; char *str1,*str2,*str3; puts("Enter first string"); gets(str1); puts("Enter second string"); gets(str2); printf("Before concatenation the strings are\n"); puts(str1); puts(str2); while(*str1){ str3[i++]=*str1++; } while(*str2){ str3[i++]=*str2++; } str3[i]='\0'; printf("After concatenation the strings are\n"); puts(str3); return 0; }
/*Reading array using pointer and print it using pointer*/ #include<stdio.h> int main(){ int i; int arr[10]; int *ptr; ptr=arr; printf("Enter 10 Elements "); for(i=0;i<10;i++) { scanf("%d",ptr); ptr++; } ptr=arr;//Again initializing base address of array otherwise // pointer will point to the next address after reading //and print garbage value printf("Entered Numbers:\n"); for(i=0;i<10;i++) { printf("%d\n",*ptr); ptr++; } }
/*compare string using pointer*/ #include<stdio.h> void main() { char str1[20],str2[20],*p1,*p2; int i,flag=0; printf("Enter first string: "); scanf("%s",&str1); printf("Enter second string: "); scanf("%s",&str2); p1=&str1; p2=&str2; while(*p1!='\0' || *p2!='\0') { if(*p1==*p2) { ++p1; ++p2; } else { flag=1; break; } } if(*p1=='\0' && *p2=='\0' && flag==0)//first two conditions in if are for //matching the length of the strings printf("String are same"); else printf("String are not same"); }
/*Calculate length of string using pointer*/ #include<stdio.h> void main() { char str1[20],*p1; int i,len=0; printf("Enter string: "); scanf("%s",&str1); p1=&str1; while(*p1!='\0') { p1++; len++; } printf("Length of String : %d",len); }
/*Swapping two number using pointer*/ #include<stdio.h> void main() { int a,b,*p1,*p2; printf("Enter two numbers: "); p1=&a; p2=&b; scanf("%d%d",p1,p2); *p1=*p2+*p1; *p2=*p1-*p2; *p1=*p1-*p2; printf("After Swapping : %d %d",*p1,*p2); }
/* */ #include<stdio.h> int fact(int); int main(){ int num,f; printf("\nEnter a number: "); scanf("%d",&num); f=fact(num); printf("\nFactorial of %d is: %d",num,f); return 0; } int fact(int n){ if(n==1) return 1; else return (n*fact(n-1)); }
/*Sum of N numbers by recursion*/ #include<stdio.h> int main(){ int n,sum; printf("Enter the value of n: "); scanf("%d",&n); sum = getsum(n); printf("Sum of n numbers: %d",sum); return 0; } int getsum(n){ static int sum=0; if(n>0){ sum = sum + n; getsum(n-1); } return sum; }
/*Multiply using Recursion*/ #include<stdio.h> int multiply(int,int); int main(){ int a,b,p; printf("Enter any two integers: "); scanf("%d%d",&a,&b); p = multiply(a,b); printf("Multiplication of two integers is %d",p); return 0; } int multiply(int a,int b){ static int p=0,i=0; if(i < a){ p = p+ b; i++; multiply(a,b); } return p; }
/*LCM of two numbers by recursion*/ #include<stdio.h> int findlcm(int,int); int main() { int a,b,l; printf("Enter any two number "); scanf("%d%d",&a,&b); if(a>b) l = findlcm(a,b); else l =findlcm(b,a); printf("LCM of two number is %d",l); return 0; } int findlcm(int a,int b){ static int temp = 1; if(temp % b == 0 && temp % a == 0) return temp; temp++; findlcm(a,b); return temp; }
/*Reverse a number using recursion*/ #include<stdio.h> int main(){ int num,reverse; printf("Enter any number: "); scanf("%d",&num); reverse=rev(num); printf("Reverse of number: %d",reverse); return 0; } int rev(int num){ static sum,r; if(num){ r=num%10; sum=sum*10+r; rev(num/10); } else return 0; return sum; } Sample output: Enter any number: 784 Reverse of number: 487
/*Largest Number of array using recursion*/ #include<stdio.h> int getmax(int []); int size; int main() { int arr[100],max,i; printf("Enter the size of the array: "); scanf("%d",&size); printf("Enter %d elements of array: ", size); for(i=0;i<size;i++) scanf("%d",&arr[i]); max=getmax(arr); printf("Largest element of array is: %d",max); return 0; } int getmax(int arr[]) { static int i=0,max =0; if(i < size) { if(max<arr[i]) max=arr[i]; i++; getmax(arr); } return max; }
/*Check Prime using Recursion*/ #include<stdio.h> int checkprime(int,int); int main(){ int num,prime; printf("Enter a number: "); scanf("%d",&num); prime = checkprime(num,num/2); if(prime==1) printf("%d is a prime number",num); else printf("%d is not a prime number",num); return 0; } int checkprime(int num,int i) { if(i==1) { return 1; } else { if(num%i==0) return 0; else checkprime(num,i-1); } }
/*Reverse a string by recursion*/ #include<stdio.h> #define MAX 100 char* reverse(char[]); int main(){ char str[MAX],*rev; printf("Enter any string: "); scanf("%s",str); rev = reverse(str); printf("Reversed string is: %s",rev); return 0; } char* reverse(char str[]){ static int i=0; static char rev[MAX]; if(*str){ reverse(str+1); rev[i++] = *str; } return rev; }
/*Find the NCr value by using recursive function*/ #include<stdio.h> int main(){ int n,r,ncr; printf("Enter any two numbers->"); scanf("%d %d",&n,&r); ncr=fact(n)/(fact(r)*fact(n-r)); printf("The NCR factor of %d and %d is %d",n,r,ncr); return 0; } int fact(int n){ int i=1; while(n!=0){ i=i*n; n--; } return i; }
/*Sum of series 1+1/2+1/3+....+1/n*/ #include<stdio.h> void main() { int n,i,sum=0; printf("Enter any no: "); scanf("%d",&n); printf("1"); for(i=2;i<=n-1;i++) printf(" 1/%d +",i); for(i=1;i<=n;i++) sum=sum+i; printf(" 1/%d",n); printf("\nSum=1/%d",sum+1/n); } Output: Enter any no: 7 1 + 1/2 + 1/3 + 1/4 + 1/5 + 1/6 + 1/7 Sum=1/28
/*Series and find sum of 1+3+5+...+n */ #include<stdio.h> void main() { int n,i,sum=0; printf("Enter any no: "); scanf("%d",&n); for(i=1;i<n;i=i+2) { printf("%d+",i); sum=sum+i; } printf("%d",n); printf("\nsum=%d",sum+n); } Output: Enter any no: 7 1+3+5+7 Sum=16
/* Sum of 1 + 2 +.....+ n series */ #include<stdio.h> main(){ int n,i; int sum=0; printf("Enter the n i.e. max values of series: "); scanf("%d",&n); sum = (n * (n + 1)) / 2; printf("Sum of the series: "); for(i =1;i <= n;i++){ if (i!=n) printf("%d + ",i); else printf("%d = %d ",i,sum); } }
/* Sum of cube of n natural numbers.*/ #include<stdio.h> #include<math.h> main(){ int n,i; int sum=0; printf("Enter the n i.e. max values of series: "); scanf("%d",&n); sum = pow(((n * (n + 1) ) / 2),2); printf("Sum of the series : "); for(i =1;i<=n;i++){ if (i != n) printf("%d^3 + ",i); else printf("%d^3 = %d ",i,sum); } }
/*Sum of infinite GP series.*/ #include<stdio.h> main(){ float a,r; float sum=0; printf("Enter the first number of the G.P. series: "); scanf("%f",&a); printf("Enter the common ratio of G.P. series: "); scanf("%f",&r); if(1 > r) sum = a/(1-r); else sum = a/(r-1); printf("\nSum of the infinite G.P. series: %f",sum); }
/*Exponential series of 1+x+x2/2!+x3/3!+.......+xn/n!*/ #include<stdio.h> #include< math.h> void main( ) { int x, n, fact, i, j; float sum=1; printf("Enter the 'x' value:"); scanf("%d",&x); printf("\nEnter the 'n' value:"); scanf("%d",&n); for(i=1; i< =n ; i++) { fact=1; for( j=i ; j >=1; j--) fact=fact*j; sum=sum+(pow(x,i )/ fact); } printf("\nSum of the series : %f ",sum); }
/*(1^1) + (2^2) + (3^3) + (4^4) + (5^5) + ... + (n^n)*/ #include<stdio.h> #include<math.h> long power(int a, int b) { long i, p=1; for(i=1;i<=b;i++) { p=p*a; } return p; } int main() { long i,n,sum=0; printf("Enter value of n"); scanf("%d",&n); for(i=1;i<=n;i++) { sum=sum+power(i,i); } printf("Sum: %d",sum); return 0; }
/*(1!/1) + (2!/2) + (3!/3) + (4!/4) + (5!/5) + ... + (n!/n)*/ #include<stdio.h> long fact(int n) { long i, f=1; for(i=1;i<=n;i++) { f=f*i; } return f; } int main() { long i,n; double sum=0; printf("Enter value of n "); scanf("%d",&n); for(i=1;i<=n;i++) { sum=sum+(fact(i)/i); } printf("Sum: %lf",sum); return 0; }
/*Program to find sum of two numbers.*/ #include<stdio.h> void main() { int a,b,s; printf("Enter two no: "); scanf("%d%d",&a,&b); s=a+b; printf("sum=%d",s); } Output: Enter two no: 5 6 sum=11
/*Program to find area and circumference of circle.*/ #include<stdio.h> #include<conio.h> void main() { int r; float pi=3.14,area,cu; printf("enter radius of circle:"); scanf("%d",&r); area=pi*r*r; printf("area of circle=%f",area); cu=2*pi*r; printf("circumference=%f",cu); } Output: enter radius of a circle: 5 area of circle=78.000 circumference=31.4
/*Program to find the simple interest.*/ #include<stdio.h> void main() { int p,r,t,si; printf("enter principle, Rate of interest & time to find simple interest"); scanf("%d%d%d",&p,&r,&t); si=(p*r*t)/100; printf("simple intrest= %d",si); } Output: enter principle, rate of interest & time to find simple interest 200 2 5 simple interest=20
/*Program to convert temperature from degree centigrade to Fahrenheit.*/ #include<stdio.h> void main() { float c,f; clrscr(); printf("Enter temp in centigrade: "); scanf("%f",&c); f=(1.8*c)+32; printf("temp in Fahrenheit=%f ",f); } Output: enter temp in centigrade: 32 temp in Fahrenheit=89.59998
/*Program to find gross salary.*/ #include<stdio.h> void main() { int gs,bs,da,ta; printf("enter basic salary:"); scanf("%d",&bs); da=(10*bs)/100; ta=(12*bs)/100; gs=bs+da+ta; printf("gross salary=%d",gs); }
/*find whether given no. is even or odd.*/ #include<stdio.h> void main() { int n; printf("enter any no.:"); scanf("%d",&n); if(n%2==0) // you can use if(n&1==0) printf("no. is even"); else printf("no. is odd"); } Output: enter any no.: 7 no. is odd
/*Simple calculator arithmetic operator using switch case.*/ #include<stdio.h> void main() { int a,b,n,s,m,su,d; printf("enter two no. : "); scanf("%d%d",&a,&b); printf("enter 1 for sum, 2 for multiply, 3 for subtraction 4 for division:"); scanf("%d",&n); switch(n) { case 1: s=a+b; printf("sum=%d",s); break; case 2: m=a*b; printf("multiply=%d",m); break; case 3: su=a-b; printf("subtraction=%d",su); break; case 4: d=a/b; printf("divission=%d",d); break; default: printf("wrong input"); break; } } Output: enter two no: 7 4 enter 1 for sum 2 for multiply 3 for subtraction 4 for division: 1 sum=11
/*Program to display first 10 natural no. & their sum.*/ #include<stdio.h> void main() { int i,sum=0; for(i=1;i<=10;i++) { printf("%d no is= %d \n ",i,I); sum=sum+i; } printf("sum =%d ",sum); } Output: 1 no is=1 2 no is=2 3 no is=3 4 no is=4 5 no is=5 6 no is=6 7 no is=7 8 no is=8 9 no is=9 10 no is=10 sum=55
/*Read total seconds and convert it into hour, min and sec*/ #include<stdio.h> void main() { int ts,h,m,s; printf(" Enter total sec: "); scanf("%d",&ts); h=ts/3600; ts=ts%3600; m=ts/60; s=ts%60; printf("hour = %d min = %d, sec= %d",h,m,s); }
/*Find maximum among three using conditional operator*/ #include<stdio.h> void main() { int a,b,c; printf("Enter three numbers : "); scanf("%d%d%d",&a,&b,&c); printf("Max is: %d",a>b?a>c?a:c:b>c?b:c); }
/*Find input is number,character (Lower or upper) or other symbol*/ #include<stdio.h> void main() { char n; printf("Enter a data: "); scanf("%c",&n); if(n>=48&&n<=57)//ASCII value of 0-9 is 48-57 { printf("Number"); } else if(n>=65&&n<=90)//ASCII value of A-Z is 65-90 { printf("Upper case"); } else if(n>=97&&n<=122)//ASCII value of a-z is 97-122 { printf("Lower case"); } else { printf("Other symbol"); } }
/*K.M. to meter, centimeter or mm*/ #include<stdio.h> int main() { float km; int v; float ans; printf("Enter distance in K.M. : "); scanf("%f",&km); printf("1 for meter 2 for centimeter 3 for mm : "); fflush(stdin); scanf("%d",&v); if(v==1) ans=km*1000; if(v==2) ans=km*100000; if(v==3) ans=km*1000000; printf("Answer : %.2f",ans); return 0; }
/*Program to calculate power of a number*/ #include<stdio.h> void main(){ int pow,num,i=1; long int sum=1; printf("\nEnter a number: "); scanf("%d",&num); printf("\nEnter power: "); scanf("%d",&pow); while(i<=pow){ sum=sum*num; i++; } printf("\n%d to the power %d is: %ld",num,pow,sum); }
/*Program to find area of triangle using Heron's Formula*/ #include<stdio.h> void main() { int x=10,y=8,z=7; float s=0.0; double area=0; s=(x+y+z)/2.0; area=(s*(s-x)*(s-y)*(s-z)); printf("\nArea of triangle = %lf",area); }
/*Check Number is negative,positive or zero using conditional operator*/ #include<stdio.h> void main() { int n; printf("Enter a number: "); scanf("%d",&n); printf("%s",n>=0?(n==0?"Zero":"Positive"):"Negative"); }
/*Program to Reverse any number*/ #include<stdio.h> int main(){ int num,r,reverse=0; printf("Enter any number: "); scanf("%d",&num); while(num){ r=num%10; reverse=reverse*10+r; num=num/10; } printf("Reversed of number: %d",reverse); return 0; } output: Enter any number: 45 Reversed of number: 54
/*Program to subtract two numbers without using subtraction operator*/ #include<stdio.h> int main(){ int a,b,sum; printf("Enter any two integers: "); scanf("%d%d",&a,&b); sum = a + ~b + 1; printf("Difference of two integers: %d",sum); return 0; } Output: Enter any two integers: 6 4 Difference of two integers: 2
/*calculate roots of a quadratic equation*/ #include<stdio.h> #include<math.h> int main(){ float a,b,c; float d,root1,root2; printf("Enter a, b and c of quadratic equation: "); scanf("%f%f%f",&a,&b,&c); d = b * b - 4 * a * c; if(d < 0){ printf("Roots are complex number.\n"); printf("Roots of quadratic equation are: "); printf("%.3f%+.3fi",-b/(2*a),sqrt(-d)/(2*a)); printf(", %.3f%+.3fi",-b/(2*a),-sqrt(-d)/(2*a)); return 0; } else if(d==0){ printf("Both roots are equal.\n"); root1 = -b /(2* a); printf("Root of quadratic equation is: %.3f ",root1); return 0; } else{ printf("Roots are real numbers.\n"); root1 = ( -b + sqrt(d)) / (2* a); root2 = ( -b - sqrt(d)) / (2* a); printf("Roots of quadratic equation are: %.3f , %.3f",root1,root2); } return 0; } output: Enter a, b and c of quadratic equation: 1 2 1 Roots are equal Roots of quadratic equation are: -1.000
/*Factorial program using while loop --output cannot be greater than integer range*/ #include<stdio.h> void main(){ int i=1,f=1,num; printf("Enter a number: "); scanf("%d",&num); while(i<=num){ f=f*i; i++; } printf("Factorial of %d is: %d",num,f); } output: Enter a number: 5 Factorial of 5 is: 120
/*Factorial program using for loop */ #include<stdio.h> int main(){ int i,f=1,num; printf("Enter a number: "); scanf("%d",&num); for(i=1;i<=num;i++) f=f*i; printf("Factorial of %d is: %d",num,f); return 0; }
/*Swapping of two no̢۪s without using third variable four process.*/ #include<stdio.h> void main(){ int a=5,b=10; //process one a=b+a; b=a-b; a=a-b; printf("a= %d b= %d",a,b); //process two a=5;b=10; a=a+b-(b=a); printf("\na= %d b= %d",a,b); //process three a=5;b=10; a=a^b; b=a^b; a=b^a; printf("\na= %d b= %d",a,b); //process four a=5;b=10; a=b-~a-1; b=a+~b+1; a=a+~b+1; printf("\na= %d b= %d",a,b); }
/*Shift inputed data by two bits to the left.*/ #include<stdio.h> void main(){ int x,y; printf("Read the integer from keyboard :-"); scanf("%d",&x); x<<=2;//left shift operator y=x; printf("\nThe left shifted data is = %d",y); }
/*Area of Triangle*/ #include<stdio.h> int main() { floar r,area; printf("Enter radius of circle: "); scanf("%f",&r); area = 3.14 * r * r; printf("Area of circle is: %f",area); return 0; }
/*Print even number of the given range*/ #include<stdio.h> int main() { into min,max; printf("Enter min and max even numbers respectively: "); scanf("%d %d",&min,&max); for(i=min;i<=max;i=i+2) { printf("%d ",i); } return 0; }
/*Square root of any Number*/ #include<stdio.h> #include<math.h> int main(){ int num; scanf("%d",&num); printf("Square root: %f",sqrt(num)); return 0; }
/*Find maximum number between two*/ #include<stdio.h> int main(){ int a,b,max; scanf("%d %d",&a,&b); if(a > b) max = a; else max = b; printf("%d",max)); return 0; }
/*ASCII value of given character*/ #include<stdio.h> int main( char c; printf("Enter any character: "); scanf("%c",&c); printf("ASCII value of given character: %d",c); return 0; }
/*Print sum of individual digit of a number example: 123 -> 1+2+3=6 */ #include<stdio.h> void main() { int num,sum=0,a; printf("Enter a number: "); scanf("%d",&num); while(num!=0) { a=num%10; sum=sum+a; num=num/10; } printf("Sum of individual digits: %d",sum); }
/*Convert any number to words*/ #include<stdio.h> #include<math.h> #include<stdlib.h> void checkRange(int num); void checkRange1(int num); void checkRange2(int num); void checkRange3(int num); void checkRange4(int num); void checkDigit1(int num); void checkDigit2(int num); void checkDigit3(int num1,int num2); int main() { int num; printf("Enter a number : "); scanf("%d",&num); printf("Entered number in words : "); if(num<=100) checkRange1(num); else if(num>100 && num<1000) checkRange2(num); else if(num>=1000 && num<=100000) checkRange3(num); else if(num>100000 && num<=10000000) checkRange4(num); else checkRange(num); return 0; } void checkRange(num) { printf("Entered number should be between 0 to 10000000"); } void checkRange1(num) { int x,y,z; if(num>=0 && num<=10) checkDigit1(num); else if(num>10 && num<20) { x=num%10; checkDigit2(x); } else if(num>=20 && num<=100) { y=num/10; z=num%10; checkDigit3(y,z); } } void checkRange2(int num) { int x,y,z; y=num/100; checkDigit1(y); printf(" Hundred "); x=num%100; checkRange1(x); } void checkRange3(int num) { int x,y,z; if(num==1000) { printf(" Thousand "); exit(0); } else if(num==100000) { printf("Lakh"); exit(0); } else { x=num/1000; checkRange1(x); printf(" Thousand "); z=num%1000; if(z<=99) checkRange1(z); else checkRange2(z); } } void checkRange4(int num) { int x,y; if(num==10000000) { printf("One Crore"); exit(0); } x=num/100000; checkRange1(x); printf(" Lakhs "); y=num%100000; if(y==0) exit(0); else if(y<=99) checkRange1(y); else if(y>99 && y<=999) checkRange2(y); else checkRange3(y); } void checkDigit1(int num) { switch(num) { case 0: printf("Zero"); break; case 1: printf("One"); break; case 2: printf("Two"); break; case 3: printf("Three"); break; case 4: printf("Four"); break; case 5: printf("Five"); break; case 6: printf("Six"); break; case 7: printf("Seven"); break; case 8: printf("Eight"); break; case 9: printf("Nine"); break; case 10: printf("Ten"); break; default: printf(""); } } void checkDigit2(int num) { switch(num) { case 1: printf("Eleven"); break; case 2: printf("Twelve"); break; case 3: printf("Thirteen"); break; case 4: printf("Fourteen"); break; case 5: printf("Fifteen"); break; case 6: printf("Sixteen"); break; case 7: printf("Seventeen"); break; case 8: printf("Eighteen"); break; case 9: printf("Nineteen"); break; default: printf(""); } } void checkDigit3(int num1,int num2) { switch(num1) { case 2: printf("Twenty "); break; case 3: printf("Thirty "); break; case 4: printf("Forty "); break; case 5: printf("Fifty "); break; case 6: printf("Sixty "); break; case 7: printf("Seventy "); break; case 8: printf("Eighty "); break; case 9: printf("Ninety "); break; case 10: printf("Hundred"); break; default: printf(""); } checkDigit1(num2); }
/* Find LCM and GCD of two number */ #include<stdio.h> int main() { int a,b,t1,t2,lcm,gcd; printf("Enter two number: "); scanf("%d%d",&a,&b); t1=a; t2=b; while(t1!=t2) { if(t1>t2) { t1=t1-t2; } else { t2=t2-t1; } } gcd=t1; lcm=(a*b)/gcd; printf("LCM = %d , GCD= %d",lcm,gcd); return 0; }
/*Check whether given year is leap or not*/ #include<stdio.h> void LeapYear(int); void main() { int yr,rem1,rem2; printf("Enter year : "); scanf("%d",&yr); rem1 = yr%4 ; //rem1 = 0 for leap year rem2 = yr%100; //rem2! = 0 for leap year if((rem1 == 0) && (rem2!=0) || yr%400 == 0) { printf("The given year %d is Leap Year.",yr); } else { printf("The given year %d is Not Leap Year.",yr); } }
/*Convert String to Integer*/ #include<stdio.h> #include<stdlib.h> void main() { int num; char marks[3]; printf("Please Enter Marks : "); scanf("%s",marks); num = atoi(marks); printf("\n Marks : %d",num); } Output: Please Enter Marks : 25 // here 25 is as string Marks : 25
/*Count Total number of Capital and Small Letters */ #include<stdio.h> void main() { int upper=0,lower=0,i=0; char ch[50]; int i; printf("Enter a String : "); gets(ch); while(ch[i]!='\0') { if(ch[i]>='A' && ch[i]<='Z') upper++; if(ch[i]>='a' && ch[i]<='z') lower++; i++; } printf("\nUppercase Letters are: %d",upper"); printf("\nLowercase Letters are : %d",lower); }
/*Concat Two Strings without Using Library Function*/ #include<stdio.h> #include<string.h> void con(char[],char[]); int main() { char str1[50],str2[30]; printf("\nEnter String 1 : "); gets(str1); printf("\nEnter String 2 : "); gets(str2); con(str1,str2); printf("\nConcated string is :%s",str1); return 0 ; } void con(char str1[],char str2[]) { int i,j; i = strlen(str1); for(j=0; str2[j] != '\0'; i++,j++) str1[i]=str2[j]; str1[i]='\0'; }
/*Find Length of String Using Library Function*/ #include<stdio.h> #include<string.h> void main() { char str[100]; int len; printf("\nEnter the String : "); gets(str); len = strlen(str); printf("\nLength of Given String : %d",len); }
/*Find Length of String Without using Library Function*/ #include<stdio.h> void main() { char str[100]; int len; printf("\nEnter String : "); gets(str); len = 0; while(str[len]!='\0') len++; printf("\nLength of the String is : %d",len); }
/*Search whether character is present in the string or not*/ #include<stdio.h> void main() { char str[30],ch; int count=0,i; printf("\nEnter string : "); scanf("%s",&str); printf("\nEnter the character to be searched : "); scanf("%c",&ch); for(i=0;str[i]!='\0';i++) { if(str[i]==ch) count++; } if(count==0) printf("Character '%c'is not present",ch); else printf("character %c is: %d times",ch,count); }
/*Reverse string without using library function*/ #include<stdio.h> #include<string.h> //You can remove this header file if length of string will be counted by without strlen() function main() { char str[100],temp; int i=0,j=0; printf("Enter the string : "); gets(str); j=strlen(str)-1; while(i<j) { temp=str[i]; str[i]=str[j]; str[j]=temp; i++; j--; } printf("\nReverse string is :%s",str); return 0; }
/*Length of string using pointer*/ #include<stdio.h> int length(char*); void main() { char str[40]; int l; printf("\n enter a line of text"); gets(str); l=length(str); printf("\n string length is %d",l); } int length(char*s) { int l=0; while(*s!='\0') { l++; s++; } return l; }
/*Convert lower case into upper case without using library function*/ #include<stdio.h> void main() { char str[100]; int i; printf("\nEnter String : "); gets(str); for(i=0;i<str[i]!='\0';i++) { if(str[i]>=97&&str[i]<=122) str[i]=str[i]-32; } printf("%s",str); }
/*Count no. of vowel in a string using library function*/ #include<stdio.h> #include<string.h> void main() { char str[100]; int i,count=0; printf("\nEnter String : "); gets(str); strlwr(str); for(i=0;i<str[i]!='\0';i++) { if(str[i]=='a'||str[i]=='e'||str[i]=='i'||str[i]=='o'||str[i]=='u') count++; } printf("vowel = %d ",count); }
/*Check String is Palindrome or not without library function*/ #include<stdio.h> #include<string.h> void main() { char str[100]; int i,j,len,matched=0; printf("\nEnter String : "); gets(str); len=strlen(str);//You can find length without strlen for(i=0,j=len-1;i!=len/2;i++,j--) { if(str[i]!=str[j]) { matched=1; break; } } if(matched) { printf("Not palindrome"); } else { printf("palindrome"); } }
/*Get string from user without displaying it on console)*/ #include<stdio.h> int main() { char str[100],ch; int n,count=0; printf("\nEnter String : "); while(1) { ch=getch(); if(ch==13)//13 is ASCII value of \n { break; } str[count]=ch; count++; } printf("\nEntered String : %s",str); return 0; }
/*Structure as argument*/ /*Write a program to read information of a book and display the information.*/ #include<stdio.h> #include<string.h> struct book { int bno; char name[40]; }; void display(struct book); void main() { struct book b; printf("\nEnter book no: "); scanf("%d",&b.bno); printf("Enter book name: "); scanf("%s",b.name); display(b); } void display(struct book b) { printf("\n details are: "); printf("Book number=%d",b.bno); printf("Book name=%s",b.name); }
/*Pointer to Structure as argument*/ /*Write a program to read information of a book and display the information.*/ #include<stdio.h> struct book { int bno; char name[40]; }; void display(struct book*); void main() { struct book b; printf("\nEnter book no: "); scanf("%d",&b.bno); printf("Enter book name: "); scanf("%s",b.name); display(&b); } void display(struct book *b) { printf("\nDetails are: "); printf("Book number= %d \n",b->bno); printf("Book name= %s",b->name); }
/*Passing Array of Structure as function argument*/ #include<stdio.h> struct student{ char name[40]; int marks; }; void display(struct student[],int); void main() { struct student s[3]; int i; printf("enter information of 3 students\n"); for(i=0;i<3;i++) { printf("\nEnter name"); scanf("%30[^\n]%*c",s[i].name);//you can use gets() printf("\nEnter marks"); scanf("%d",&s[i].marks); } display(s,3); } void display (struct student s[], int n) { int i,count=0; for(i=0;i<n;i++) { if(s[i].marks>=100) { printf("Required details are\n"); printf("student name= %s",s[i].name); printf(" student marks=%d\n",s[i].marks); count++; } } if(count==0) { printf("No details available for marks>100"); } }
/*Nesting of Structure*/ #include<stdio.h> struct detail { char name[10]; float price; }; struct item { int id; struct detail d; }; void main() { struct item itm; printf("Enter item name, id and price "); scanf("%s%d%f",itm.d.name,&itm.id,&itm.d.price); printf("Entered details are:\n"); printf("Id: %d\nName: %s Price: %f", itm.id , itm.d.name, itm.d.price); }
/*Structure as Return type of a function*/ #include<stdio.h> struct book { int id; char name[40]; }; struct book getdata() { struct book b; printf("Enter book name and id"); scanf("%s%d",&b.name,&b.id); return b; } void display(struct book b) { printf("\nDetails are:\n"); printf("Book id = %d\n",b.id); printf("Book name= %s",b.name); } void main() { struct book b; b=getdata(); display(b); }
/*Array of Structure*/ #include<stdio.h> struct book { int id; char name[40]; }; void main() { struct book b[10]; int i,n; printf("Enter total number of book : "); scanf("%d",&n); for(i=0;i<n;i++) { printf("\nEnter book name and id of Book %d:\n",i+1); scanf("%s%d",&b[i].name,&b[i].id); } for(i=0;i<n;i++) { printf("\nBook %d:\n",i+1); printf("Book id = %d ",b[i].id); printf("Book name= %s",b[i].name); } }
Comments
Post a Comment