ALL C Programs
ALL C Programs
itself. In other word we can say which has only two divisors 1 and number itself. For example: 5 Their divisors are 1 and 5. Note: 2 is only even prime number. Logic for prime number in c We will take a loop and divide number from 2 to
number/2. If the number is not divisible by any of the numbers then we will print it as prime number. Example of prime numbers : 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, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199 etc. Code 1: 1. C program to determine prime number 2. Determining if a number is prime in c 3. C program to find given number is prime or not #include<stdio.h> int main(){ int num,i,count=0;
MLEC@IT Page 1
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; } Sample output: Enter a number: 5 5 is a prime number Code 2: 1. C program for prime numbers between 1 to 100 2. How to find prime numbers from 1 to 100 in c 3. How to print prime numbers from 1 to 100 in c #include<stdio.h> int main(){ int num,i,count; for(num = 1;num<=100;num++){ count = 0; for(i=2;i<=num/2;i++){
MLEC@IT Page 2
if(num%i==0){ count++; break; } } if(count==0 && num!= 1) printf("%d ",num); } return 0; } 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 Code 3: 1. C program for prime numbers between 1 to n 2. C program to find prime numbers up to n 3. C program to list prime numbers 4. Write a c program to generate n prime numbers 5. C program to find n prime numbers #include<stdio.h> int main(){ int num,i,count,n; printf("Enter max range: "); scanf("%d",&n); for(num = 1;num<=n;num++){
MLEC@IT Page 3
count = 0; for(i=2;i<=num/2;i++){ if(num%i==0){ count++; break; } } if(count==0 && num!= 1) printf("%d ",num); } return 0; } Sample output: Enter max range: 50 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 Code 4: 1. C program to find prime numbers using while loop 2. Wap to find prime numbers in c 3. Write a c program to generate prime number 4. How to get prime numbers in c #include<stdio.h> int main(){ int num,i,count,min,max; printf("Enter min range: "); scanf("%d",&min);
MLEC@IT Page 4
printf("Enter max range: "); scanf("%d",&max); num = min; while(num<=max){ count = 0; i=2; while(i<=num/2){ if(num%i==0){ count++; break; } i++; } if(count==0 && num!= 1) printf("%d ",num); num++; } return 0; } Sample output: Enter min range: 50 Enter max range: 100 53 59 61 67 71 73 79 83 89 97 Code 5:
MLEC@IT Page 5
1. How to find out prime numbers in c programming 2. Display prime numbers in c 3. C program to find prime numbers between two numbers 4. C code to display prime numbers within a range #include<stdio.h> int main(){ int num,i,count,min,max; printf("Enter min range: "); scanf("%d",&min); printf("Enter max range: "); scanf("%d",&max); for(num = min;num<=max;num++){ count = 0; for(i=2;i<=num/2;i++){ if(num%i==0){ count++; break; } } if(count==0 && num!= 1)
MLEC@IT Page 6
Sample output:
Enter min range: 10 Enter max range: 50 11 13 17 19 23 29 31 37 41 43 47 Code 6: 1. Sum of prime numbers from 1 to 100 in c #include<stdio.h> int main(){ int num,i,count,sum=0; for(num = 1;num<=100;num++){ count = 0; for(i=2;i<=num/2;i++){ if(num%i==0){ count++; break;
MLEC@IT Page 7
} } if(count==0 && num!= 1) sum = sum + num; } printf("Sum of prime numbers is: %d ",sum); return 0; } Output: Sum of prime numbers is: 1060 Code 7: 1. C program to find sum of prime numbers #include<stdio.h> int main(){ int num,i,count,min,max,sum=0; printf("Enter min range: "); scanf("%d",&min); printf("Enter max range: ");
MLEC@IT Page 8
scanf("%d",&max); for(num = min;num<=max;num++){ count = 0; for(i=2;i<=num/2;i++){ if(num%i==0){ count++; break; } } if(count==0 && num!= 1) sum = sum + num; } printf("Sum of prime numbers is: %d ",sum); return 0; }
Sample output:
Enter min range: 50 Enter max range: 100
MLEC@IT
Page 9
printf("%d is a strong number",temp); else printf("%d is not a strong number",temp); return 0; } Sample output: Enter a number: 145 145 is a strong number Code 2: 1. C program for strong number 2. Strong number program in c #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++){
MLEC@IT Page 11
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); } return 0; } Sample output: Enter minimum range: 100 Enter maximum range: 100000 Strong numbers in given range are: 145 40585
MLEC@IT
Page 12
factorial of its digit is equal to number itself. For example: 145 since 1! + 4! + 5! = 1 + 24 + 120 = 145
printf("%d is even number.",number); else printf("%d is odd number.",number); return 0; } Sample output: Enter any integer: 5 5 is odd number. Code 2: 1. Display odd numbers in c 2. How to print odd numbers in c #include<stdio.h> int main(){ int number; int min,max; printf("Enter the minimum range: "); scanf("%d",&min); printf("Enter the maximum range: "); scanf("%d",&max);
MLEC@IT Page 14
printf("Odd numbers in given range are: "); for(number = min;number <= max; number++) if(number % 2 !=0) printf("%d ",number); return 0; } Sample output: Enter the minimum range: 1 Enter the maximum range: 20 Odd numbers in given ranges are: 1 3 5 7 9 11 13 15 17 19 Code 3: 1. Even and odd numbers program in c 2. C program to find even or odd #include<stdio.h> int main(){ int number; int min,max;
MLEC@IT
Page 15
printf("Enter the minimum range: "); scanf("%d",&min); printf("Enter the maximum range: "); scanf("%d",&max); printf("Odd numbers in given range are: "); for(number = min;number <= max; number++) if(number % 2 !=0) printf("%d ",number); printf("\nEven numbers in given range are: "); for(number = min;number <= max; number++) if(number % 2 ==0) printf("%d ",number); return 0; } Sample output: Enter the minimum range: 1 Enter the maximum range: 20 Odd numbers in given ranges are: 1 3 5 7 9 11 13 15 17 19 Even numbers in given ranges are: 2 4 6 8 10 12 14 16 18 20
MLEC@IT Page 16
Code 4: 1. Sum of odd numbers in c #include<stdio.h> int main(){ int number; int min,max; long sum =0; printf("Enter the minimum range: "); scanf("%d",&min); printf("Enter the maximum range: "); scanf("%d",&max); for(number = min;number <= max; number++) if(number % 2 !=0) sum = sum + number; printf("Sum %ld",sum); return 0; }
MLEC@IT Page 17
of
odd
numbers
in
given
range
is:
Sample output: Enter the minimum range: 1 Enter the maximum range: 100 Sum of odd numbers in given range is: 2500 Code 5: 1. Sum of odd and even numbers c program #include<stdio.h> int main(){ int number; int min,max; long odd_sum =0,even_sum = 0; printf("Enter the minimum range: "); scanf("%d",&min); printf("Enter the maximum range: "); scanf("%d",&max); for(number = min;number <= max; number++) if(number % 2 != 0) odd_sum = odd_sum + number; else even_sum = even_sum + number;
MLEC@IT
Page 18
of of
even odd
numbers numbers
in in
given given
range range
is: is:
Enter the minimum range: 1 Enter the maximum range: 10 Sum of even numbers in given range is: 30 Sum of odd numbers in given range is: 25
Algorithm:
Number is called even number if it is divisible by two otherwise odd. Example of even numbers: 0,2,4,8,9,10 etc. Example of odd numbers: 1, 3,5,7,9 etc.
#include<stdio.h> int 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); return 0; } Sample output: Enter a number: 131 131 is a palindrome Code 2: 1. Write a c program for palindrome
MLEC@IT Page 20
2. C program to find palindrome of a number 3. Palindrome number in c language #include<stdio.h> int 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("Palindrome numbers in given range are: "); for(num=min;num<=max;num++){ temp=num; sum=0; while(temp){ r=temp%10; temp=temp/10; sum=sum*10+r; } if(num==sum) printf("%d ",num); } return 0;
MLEC@IT Page 21
Sample output: Enter the minimum range: 1 Enter the maximum range: 50 Palindrome numbers in given range are: 1 2 3 4 5 6 7 8 9 11 22 33 44 Code 3: 1. How to check if a number is a palindrome using for loop #include<stdio.h> int 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*10+r; } if(temp==sum) printf("%d is a palindrome",temp); else printf("%d is not a palindrome",temp);
MLEC@IT Page 22
return 0; } Sample output: Enter a number: 1221 1221 is a palindrome Code 4: 1. C program to check if a number is palindrome using recursion #include<stdio.h> int checkPalindrome(int); int main(){ int num,sum; printf("Enter a number: "); scanf("%d",&num); sum = checkPalindrome(num); if(num==sum) printf("%d is a palindrome",num); else printf("%d is not a palindrome",num); return 0;
MLEC@IT Page 23
} int checkPalindrome(int num){ static int sum=0,r; if(num!=0){ r=num%10; sum=sum*10+r; checkPalindrome(num/10); } return sum; } Sample output: Enter a number: 25 25 is not a palindrome
Definition of Palindrome number or What is palindrome number? A number is called palindrome number if it is remain same when its digits are reversed. For example 121 is palindrome number. When we will reverse its digit it will remain same number i.e. 121
MLEC@IT
Page 24
Palindrome numbers examples: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 22, 33, 44, 55, 66, 77, 88, 99, 101, 111, 121, 131, 141, 151, 161, 171, 181, 191 etc.
Example etc.
of
string
palindrome:
a,b,
aa,aba,qwertrewq
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; }
MLEC@IT Page 26
else if(d==0){ printf("Both roots are equal.\n"); root1 = -b /(2* a); printf("Root ",root1); return 0; } else{ printf("Roots are real numbers.\n"); root1 = ( -b + sqrt(d)) / (2* a); root2 = ( -b - sqrt(d)) / (2* a); printf("Roots } return 0; } Sample output: Enter a, b and c of quadratic equation: 2 4 1 Roots are real numbers. Roots of quadratic equation are: -0.293, -1.707 of quadratic equation are: %.3f , %.3f",root1,root2); of quadratic equation is: %.3f
#include<stdio.h> #include<math.h> int main(){ float a,b,c; float d,root1,root2; printf("Enter ax^2+bx+c: "); scanf("%fx^2%fx%f",&a,&b,&c); d = b * b - 4 * a * c; if(d < 0){ printf("Roots are complex number.\n"); return 0; } root1 = ( -b + sqrt(d)) / (2* a); root2 = ( -b - sqrt(d)) / (2* a); printf("Roots of quadratic equation are: %.3f , %.3f",root1,root2); return 0; } quadratic equation in the format
MLEC@IT
Page 28
Sample output: Enter quadratic equation in the format ax^2+bx+c: 2x^2+4x+-1 Roots of quadratic equation are: 0.000, -2.000
f=i+j; i=j; j=f; printf(" %ld",j); } return 0; } Sample output: Enter the number range: 15 FIBONACCI SERIES: 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 Code 2: 1. Fibonacci series using array in c 2. Fibonacci series program in c language 3. Source code of Fibonacci series in c 4. Wap to print Fibonacci series in c #include<stdio.h> int main(){ int i,range; long int arr[40]; printf("Enter the number range: "); scanf("%d",&range);
MLEC@IT Page 30
arr[0]=0; arr[1]=1; for(i=2;i<range;i++){ arr[i] = arr[i-1] + arr[i-2]; } printf("Fibonacci series is: "); for(i=0;i<range;i++) printf("%ld ",arr[i]); return 0; } Sample output: Enter the number range: 20 Fibonacci series is: 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 Code 3: 1. Fibonacci series in c using while loop 2. C program to calculate Fibonacci series 3. C program to display Fibonacci series 4. Fibonacci series in c with explanation 5. C code to generate Fibonacci series #include<stdio.h>
MLEC@IT Page 31
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; }
Sample output: Enter the number range: 10 Fibonacci series is: 0 1 1 2 3 5 8 13 21 34 Code 4: 1. Sum of Fibonacci series in c
MLEC@IT
Page 32
#include<stdio.h> int main(){ int k,r; long int i=0,j=1,f; long int sum = 1; printf("Enter the number range: "); scanf("%d",&r); for(k=2;k<r;k++){ f=i+j; i=j; j=f; sum = sum + j; } printf("Sum of Fibonacci series is: %ld",sum); return 0; } Sample output: Enter the number range: 4 Sum of Fibonacci series is: 4
Algorithm:
MLEC@IT Page 33
We assume first two Fibonacci are 0 and 1 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. So Fibonacci numbers is Algorithm for Fibonacci series Fn = Fn-2 + Fn-1 Example of Fibonacci series: 0 , 1 ,1 , 2 , 3 , 5 , 8 , 13 , 21 , 34 , 55 ...
5 is Fibonacci number since sum of its two previous number i.e. 2 and 3 is 5 8 is Fibonacci number since sum of its two previous number i.e. 3 and 5 is 8 and so on.
MLEC@IT
Page 34
MLEC@IT
Page 35
Code 2: 1. Factorial program in c using for loop 2. Simple factorial program in c 3. C program to calculate factorial #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; } Code 3: 1. Factorial program in c using pointers 2. How to calculate factorial in c 3. Factorial program in c language #include<stdio.h> void findFactorial(int,int *); int main(){
MLEC@IT Page 36
int i,factorial,num; printf("Enter a 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; } Code 4: 1. Factorial program in c using function 2. C program to find factorial of a number #include<stdio.h> int findFactorial(int); int main(){ int i,factorial,num;
MLEC@IT Page 37
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; } Sample output: Enter a number: 8 Factorial of 8 is: 40320 Code 5: 1. Factorial series in c #include<stdio.h> int main(){ long f=1;
MLEC@IT Page 38
int i,num,min,max; printf("Enter the minimum range: "); scanf("%d",&min); printf("Enter the maximum range: "); scanf("%d",&max); printf("Factorial series in given range: "); for(num=min;num<=max;num++){ f=1; for(i=1;i<=num;i++) f=f*i; printf("%ld ",f); } return 0; } Sample output: Enter the minimum range: 1 Enter the maximum range: 10 Factorial series in given range: 1 2 6 24 120 720 5040 40320 362880 3628800
MLEC@IT
Page 39
Algorithm:
Factorial value Factorial of number is defined as: Factorial (n) = 1*2*3 * n For example: Factorial of 5 = 1*2*3*4*5 = 120 Note: Factorial of zero = 1
#include<stdio.h>
printf("FLOYD'S TRIANGLE\n\n"); for(i=1;i<=r;i++){ for(j=1;j<=i;j++,k++) printf(" %d",k); printf("\n"); } return 0; } Sample output: Enter the range: 10 FLOYD'S TRIANGLE 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
MLEC@IT
Page 41
What is Floyds triangle? Definition of floyd's triangle: Floyd's triangle is a right angled-triangle using the natural numbers. Examples of floyd's triangle: Example 1: 1 2 3 4 5 6 7 8 9 10 Example 2: 1 2 4 7 11 16 3 5 8 12 17 6 9 13 18 10 14 19 15 20 21
MLEC@IT
Page 42
1. Pascal triangle in c without using array 2. C code to print Pascal triangle 3. Simple c program for Pascal triangle 4. C program to generate Pascal triangle 5. Pascal triangle program in c language 6. C program to print Pascal triangle using for loop
#include<stdio.h> long fact(int); int main(){ int line,i,j; printf("Enter the no. of lines: "); scanf("%d",&line); for(i=0;i<line;i++){ for(j=0;j<line-i-1;j++) printf(" "); for(j=0;j<=i;j++) printf("%ld j)));
MLEC@IT Page 43
",fact(i)/(fact(j)*fact(i-
printf("\n"); } return 0; } long fact(int num){ long f=1; int i=1; while(i<=num){ f=f*i; i++; } return f; } Sample output: Enter the no. of lines: 8 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1 1 6 15 20 15 6 1 1 7 21 35 35 21 7 1
MLEC@IT
Page 44
Enter the number range: 5 1*1=1 1*2=2 1*3=3 1*4=4 1*5=5 1*6=6 1*7=7 1*8=8 1*9=9 1*10=10
MLEC@IT Page 45
2*1=2 3*1=3
2*2=4 3*2=6
2*3=6 3*3=9
2*4=8 3*4=12
2*5=10 3*5=15
2*6=12 3*6=18
2*7=14 3*7=21
2*8=16 3*8=24
2*9=18 3*9=27
2*10=20 3*10=30 4*1=4 4*2=8 4*3=12 4*4=16 4*5=20 4*6=24 4*7=28 4*8=32 4*9=36 4*10=40 5*1=5 5*2=10 5*3=15 5*4=20 5*5=25 5*6=30 5*7=35 5*8=40 5*9=45 5*10=50
value
of
character
%c:
ASCII value of character : 1 ASCII value of character : 2 ASCII value of character : 3 ASCII value of character : 4 ASCII value of character : 5 ASCII value of character : 6 ASCII value of character : 7 ASCII value of character: 8 ASCII value of character ASCII value of character : 10 ASCII value of character : 11 ASCII value of character : 12 : 13I value of character ASCII value of character : 14 ASCII value of character : 15 ASCII value of character : 16 ASCII value of character : 17 ASCII value of character : 18 ASCII value of character : 19 ASCII value of character : 20 ASCII value of character : 21
MLEC@IT Page 47
ASCII value of character : 22 ASCII value of character : 23 ASCII value of character : 24 ASCII value of character : 25 ASCII value of character : 26 ASCII value of character : 27 ASCII value of character : 28 ASCII value of character : 29 ASCII value of character : 30 ASCII value of character : 31 ASCII value of character : 32 ASCII value of character !: 33 ASCII value of character ": 34 ASCII value of character #: 35 ASCII value of character $: 36 ASCII value of character %: 37 ASCII value of character &: 38 ASCII value of character ': 39 ASCII value of character (: 40 ASCII value of character ): 41 ASCII value of character *: 42 ASCII value of character +: 43 ASCII value of character ,: 44 ASCII value of character -: 45 ASCII value of character .: 46 ASCII value of character /: 47 ASCII value of character 0: 48
MLEC@IT Page 48
ASCII value of character 1: 49 ASCII value of character 2: 50 ASCII value of character 3: 51 ASCII value of character 4: 52 ASCII value of character 5: 53 ASCII value of character 6: 54 ASCII value of character 7: 55 ASCII value of character 8: 56 ASCII value of character 9: 57 ASCII value of character :: 58 ASCII value of character ;: 59 ASCII value of character <: 60 ASCII value of character =: 61 ASCII value of character >: 62 ASCII value of character ?: 63 ASCII value of character @: 64 ASCII value of character A: 65 ASCII value of character B: 66 ASCII value of character C: 67 ASCII value of character D: 68 ASCII value of character E: 69 ASCII value of character F: 70 ASCII value of character G: 71 ASCII value of character H: 72 ASCII value of character I: 73 ASCII value of character J: 74 ASCII value of character K: 75
MLEC@IT Page 49
ASCII value of character L: 76 ASCII value of character M: 77 ASCII value of character N: 78 ASCII value of character O: 79 ASCII value of character P: 80 ASCII value of character Q: 81 ASCII value of character R: 82 ASCII value of character S: 83 ASCII value of character T: 84 ASCII value of character U: 85 ASCII value of character V: 86 ASCII value of character W: 87 ASCII value of character X: 88 ASCII value of character Y: 89 ASCII value of character Z: 90 ASCII value of character [: 91 ASCII value of character \: 92 ASCII value of character ]: 93 ASCII value of character ^: 94 ASCII value of character _: 95 ASCII value of character `: 96 ASCII value of character a: 97 ASCII value of character b: 98 ASCII value of character c: 99 ASCII value of character d: 100 ASCII value of character e: 101 ASCII value of character f: 102
MLEC@IT Page 50
ASCII value of character g: 103 ASCII value of character h: 104 ASCII value of character i: 105 ASCII value of character j: 106 ASCII value of character k: 107 ASCII value of character l: 108 ASCII value of character m: 109 ASCII value of character n: 110 ASCII value of character o: 111 ASCII value of character p: 112 ASCII value of character q: 113 ASCII value of character r: 114 ASCII value of character s: 115 ASCII value of character t: 116 ASCII value of character u: 117 ASCII value of character v: 118 ASCII value of character w: 119 ASCII value of character x: 120 ASCII value of character y: 121 ASCII value of character z: 122 ASCII value of character {: 123 ASCII value of character |: 124 ASCII value of character }: 125 ASCII value of character ~: 126 ASCII value of character : 127 ASCII value of character : 128 ASCII value of character : 129
MLEC@IT Page 51
ASCII value of character : 130 ASCII value of character : 131 ASCII value of character : 132 ASCII value of character : 133 ASCII value of character : 134 ASCII value of character : 135 ASCII value of character : 136 ASCII value of character : 137 ASCII value of character : 138 ASCII value of character : 139 ASCII value of character : 140 ASCII value of character : 141 ASCII value of character : 142 ASCII value of character : 143 ASCII value of character : 144 ASCII value of character : 145 ASCII value of character : 146 ASCII value of character : 147 ASCII value of character : 148 ASCII value of character : 149 ASCII value of character : 150 ASCII value of character : 151 ASCII value of character : 152 ASCII value of character : 153 ASCII value of character : 154 ASCII value of character : 155 ASCII value of character : 156
MLEC@IT Page 52
ASCII value of character : 157 ASCII value of character : 158 ASCII value of character : 159 ASCII value of character : 160 ASCII value of character : 161 ASCII value of character : 162 ASCII value of character : 163 ASCII value of character : 164 ASCII value of character : 165 ASCII value of character : 166 ASCII value of character : 167 ASCII value of character : 168 ASCII value of character : 169 ASCII value of character : 170 ASCII value of character : 171 ASCII value of character : 172 ASCII value of character : 173 ASCII value of character : 174 ASCII value of character : 175 ASCII value of character : 176 ASCII value of character : 177 ASCII value of character : 178 ASCII value of character : 179 ASCII value of character : 180 ASCII value of character : 181 ASCII value of character : 182 ASCII value of character : 183
MLEC@IT Page 53
ASCII value of character : 184 ASCII value of character : 185 ASCII value of character : 186 ASCII value of character : 187 ASCII value of character : 188 ASCII value of character : 189 ASCII value of character : 190 ASCII value of character : 191 ASCII value of character : 192 ASCII value of character : 193 ASCII value of character : 194 ASCII value of character : 195 ASCII value of character : 196 ASCII value of character : 197 ASCII value of character : 198 ASCII value of character : 199 ASCII value of character : 200 ASCII value of character : 201 ASCII value of character : 202 ASCII value of character : 203 ASCII value of character : 204 ASCII value of character : 205 ASCII value of character : 206 ASCII value of character : 207 ASCII value of character : 208 ASCII value of character : 209 ASCII value of character : 210
MLEC@IT Page 54
ASCII value of character : 211 ASCII value of character : 212 ASCII value of character : 213 ASCII value of character : 214 ASCII value of character : 215 ASCII value of character : 216 ASCII value of character : 217 ASCII value of character : 218 ASCII value of character : 219 ASCII value of character : 220 ASCII value of character : 221 ASCII value of character : 222 ASCII value of character : 223 ASCII value of character : 224 ASCII value of character : 225 ASCII value of character : 226 ASCII value of character : 227 ASCII value of character : 228 ASCII value of character : 229 ASCII value of character : 230 ASCII value of character : 231 ASCII value of character : 232 ASCII value of character : 233 ASCII value of character : 234 ASCII value of character : 235 ASCII value of character : 236 ASCII value of character : 237
MLEC@IT Page 55
ASCII value of character : 238 ASCII value of character : 239 ASCII value of character : 240 ASCII value of character : 241 ASCII value of character : 242 ASCII value of character : 243 ASCII value of character : 244 ASCII value of character : 245 ASCII value of character : 246 ASCII value of character : 247 ASCII value of character : 248 ASCII value of character : 249 ASCII value of character : 250 ASCII value of character : 251 ASCII value of character : 252 ASCII value of character : 253 ASCII value of character : 254 ASCII value of character : 255
Solution: 2 #include<stdio.h> void main(){ while(!printf("Hello world")){ } } Solution: 3 #include<stdio.h> void main(){ switch(printf("Hello world")){ } }
write a c program which produces its own source code as its output
How
do
you
write
program
which
produces
its
own
FILE *fp; char c; fp = fopen(__FILE__,"r"); do{ c= getc(fp); putchar(c); } while(c!=EOF); fclose(fp); return 0; } Output: #include<stdio.h> int main(){ FILE *fp; char c; fp = fopen(__FILE__,"r"); do{ c= getc(fp); putchar(c);
MLEC@IT Page 58
Code 1: 1. Warp to check a number is Armstrong 2. C program to check whether a number is Armstrong or not 3. Simple c program for Armstrong number 4. Armstrong number in c with output
MLEC@IT
Page 59
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);
return 0; }
The
time
complexity
of
program
that
determines
Code 2: 1. Write a c program for Armstrong number 2. C program for Armstrong number generation 3. How to find Armstrong number in c 4. Code for Armstrong number in c
return 0; }
Enter the maximum range: 200 Armstrong numbers in given range are: 1 153
return 0; }
Sample output: Enter a number: 370 370 is an Armstrong number Logic of Armstrong number in c
Code 4: 1. C program to print Armstrong numbers from 1 to 500 2. C program for finding Armstrong numbers
for(num=1;num<=500;num++){ temp=num;
MLEC@IT Page 64
sum = 0;
return 0; }
MLEC@IT
Page 65
THOSE NUMBERS WHICH SUM OF THE CUBE OF ITS DIGITS IS EQUAL TO THAT NUMBER ARE KNOWN AS ARMSTRONG NUMBERS. FOR EXAMPLE 153 SINCE 1^3 + 5^3 + 3^3 = 1+ 125 + 9 =153 OTHER ARMSTRONG NUMBERS: 370,371,407 ETC. IN GENERAL 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. EXAMPLE 1: 153 TOTAL DIGITS IN 153 IS 3 AND 1^3 + 5^3 + 3^3 = 1 + 125 + 27 = 153 Example 2: 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: 1, 2, 3, 4, 5, 6, 7, 8, 9, 153, 370, 371, 407, 1634, 8208, 9474, 54748, 92727, 93084, 548834, 1741725
MLEC@IT
Page 66
MLEC@IT
Page 67
Code 2: 1. C program to find perfect numbers 2. C perfect number code 3. Perfect number program in c language #include<stdio.h> int 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)
MLEC@IT Page 68
printf("%d ",n); } return 0; } Sample output: Enter the minimum range: 1 Enter the maximum range: 20 Perfect numbers in given range is: 6 Code 3: 3. C program to print perfect numbers from 1 to 100 #include<stdio.h> int main(){ int n,i,sum; printf("Perfect numbers are: "); for(n=1;n<=100;n++){ i=1; sum = 0; while(i<n){ if(n%i==0) sum=sum+i; i++; }
MLEC@IT Page 69
if(sum==n) printf("%d ",n); } return 0; } Output: Perfect numbers are: 6 28 Definition of perfect number or What
is
perfect
number?
Perfect number is a positive number which sum of all positive divisors excluding that number is equal to that number. For example 6 is perfect number since
Note: 6 is the smallest perfect number. Next perfect number is 28 since 1+ 2 + 4 + 7 + 14 = 28 Some more perfect numbers: 496, 8128
Code 1:
MLEC@IT Page 70
1. Write a c program to reverse a given number 2. C program to find reverse of a number 3. C program to reverse the digits of a number 4. Reverse of a number in c using while loop #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; } Sample output: Enter any number: 12 Reversed of number: 21 Code 2:
MLEC@IT
Page 71
1. Reverse very large or big numbers beyond the range of long int 2. Reverse five digit number c program Logic is we accept the number as string #include<stdio.h> #define MAX 1000 int main(){ char num[MAX]; int i=0,j,flag=0; printf("Enter any positive integer: "); scanf("%s",num); while(num[i]){ if(num[i] < 48 || num[i] > 57){ printf("Invalid integer number"); return 0; } i++; } printf("Reverse: "); for(j=i-1;j>=0;j--) if(flag==0 &&
MLEC@IT
num[j] ==48){
Page 72
} else{ printf("%c",num[j]); flag =1; } return 0; Sample output: Enter any positive integer:
234561000045645679001237800000000000 Reverse: 8732100976546540000165432 Code 3: 1. C program to reverse a number using for loop 2. How to find reverse of a number in c 3. Wap to reverse a number in c #include<stdio.h> int main(){ int num,r,reverse=0; printf("Enter any number: "); scanf("%d",&num); for(;num!=0;num=num/10){ r=num%10;
MLEC@IT Page 73
reverse=reverse*10+r; } printf("Reversed of number: %d",reverse); return 0; } Sample output: Enter any number: 123 Reversed of number: 321 Code 4: 1. C program to 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;
MLEC@IT Page 74
if(num){ r=num%10; sum=sum*10+r; rev(num/10); } else return 0; return sum; } Sample output: Enter any number: 456 Reverse of number: 654
Code 1: 1. C program to add digits of a number 2. C program for sum of digits of a number 3. C program to calculate sum of digits #include<stdio.h> int main(){ int num,sum=0,r; printf("Enter a number: ");
MLEC@IT Page 75
scanf("%d",&num); while(num){ r=num%10; num=num/10; sum=sum+r; } printf("Sum of digits of number: return 0; } Sample output: Enter a number: 123 Sum of digits of number: Code 2: 1. Sum of digits of a number in c using for loop #include<stdio.h> int main(){ int num,sum=0,r; printf("Enter a number: "); scanf("%d",&num); for(;num!=0;num=num/10){ r=num%10; sum=sum+r; } printf("Sum of digits of number:
MLEC@IT
%d",sum);
%d",sum);
Page 76
return 0; } Sample output: Enter a number: 567 Sum of digits of number: Code 3: 1. Sum of digits in c using recursion #include<stdio.h> int getSum(int); int main(){ int num,sum; printf("Enter a number: "); scanf("%d",&num); sum = getSum(num); printf("Sum of digits of number: return 0; } int getSum(int num){ static int sum =0,r; if(num!=0){
MLEC@IT Page 77
18
%d",sum);
r=num%10; sum=sum+r; getSum(num/10); } return sum; } Sample output: Enter a number: 45 Sum of digits of number: 9
MLEC@IT
Page 79
return 0; }
Algorithm:
In c ~ is 1's complement operator. This is equivalent to: ~a = -b + 1 So, a - ~b -1 = a-(-b + 1) + 1 = a + b 1 + 1 = a + b
MLEC@IT
Page 80
Write
program
or
code
to
subtract
two
numbers
#include<stdio.h> int main(){ int a,b; int sum; printf("Enter any two integers: "); scanf("%d%d",&a,&b); sum = a + ~b + 1; printf("Difference of two integers: %d",sum); return 0; } Sample Output: Enter any two integers: 5 4 Difference of two integers: 1
Write a c program to find largest among three numbers using binary minus operator
MLEC@IT
Page 81
#include<stdio.h> int main(){ int a,b,c; printf("\nEnter 3 numbers: "); scanf("%d %d %d",&a,&b,&c); if(a-b>0 && a-c>0) printf("\nGreatest is a :%d",a); else if(b-c>0) printf("\nGreatest is b :%d",b); else printf("\nGreatest is c :%d",c); return 0; }
Alogrithm:
**
Write a c program to find largest among three numbers using conditional operator
write a c program to find largest among three numbers using conditional operator
#include<stdio.h>
MLEC@IT Page 82
int main(){ int a,b,c,big; printf("\nEnter 3 numbers:"); scanf("%d %d %d",&a,&b,&c); big=(a>b&&a>c?a:b>c?b:c); printf("\nThe biggest number is: %d",big); return 0; }
If you have any suggestions on above c program to find largest or biggest of 3 numbers,
r=num%10; num=num/10; sum+=r; } if(sum>10) num=sum; else break; } printf("\nSum of the digits in single digit is: %ld",sum); return 0; }
C code for calculation of generic root in one line #include <stdio.h> int main(){ int num,x; printf("Enter any number: "); scanf("%d",&num); printf("Generic root: %d",(x=num%9)?x:9); return 0; } Sample output: Enter any number: 731
MLEC@IT Page 84
Generic root: 2 Meaning of generic root: It sum of digits of a number unit we don't get a single digit. For example: Generic root of 456: 4 + 5 + 6 = 15 since 15 is two digit numbers so 1 + 5 = 6 So, generic root of 456 = 6
Prime factor of a number in c #include<stdio.h> int main(){ int num,i=1,j,k; printf("\nEnter a number:"); scanf("%d",&num); while(i<=num){ k=0; if(num%i==0){ j=1; while(j<=i){ if(i%j==0) k++; j++; }
MLEC@IT Page 85
Write a c program to find out NCR factor of given number #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--;
MLEC@IT Page 86
} return i; }
Algorithm:
Cr = n! /((n-r)!r!)
How
to
convert
string
to
int
without
using
library
functions in c programming language #include<stdio.h> int stringToInt(char[] ); int main(){ char str[10]; int intValue; printf("Enter any integer as a string: "); scanf("%s",str); intValue = stringToInt(str);
MLEC@IT Page 87
printf("Equivalent integer value: %d",intValue); return 0; } int stringToInt(char str[]){ int i=0,sum=0; while(str[i]!='\0'){ if(str[i]< 48 || str[i] > 57){ printf("Unable integer.\n"); return 0; } else{ sum = sum*10 + (str[i] - 48); i++; } } return sum; } Sample output: Enter any integer as a string: 123 Equivalent integer value: 123
MLEC@IT Page 88
to
convert
it
into
#include<stdio.h> int main(){ int num = 1; print(num); return 0; } int print(num){ if(num<=100){ printf("%d ",num); print(num+1); } } Output: Sample output: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
MLEC@IT
Page 89
int a,b; int *ptra,*ptrb; int *temp; printf("Enter any two integers: "); scanf("%d%d",&a,&b); printf("Before swapping: a = %d, b=%d",a,b); ptra = &a; ptrb = &b; temp = ptra; *ptra = *ptrb; *ptrb = *temp; printf("\nAfter swapping: a = %d, b=%d",a,b); return 0; } Sample output: Enter any two integers: 5 10 Before swapping: a = 5, b=10 After swapping: a = 10, b=10
MLEC@IT
Page 91
#include<stdio.h> void swap(int *,int *); int main(){ int a,b; printf("Enter any two integers: "); scanf("%d%d",&a,&b); printf("Before swapping: a = %d, b=%d",a,b); swap(&a,&b); printf("\nAfter swapping: a = %d, b=%d",a,b); return 0; } void swap(int *a,int *b){ int *temp; temp = a; *a=*b; *b=*temp; } Sample output: Enter any two integers: 3 6 Before swapping: a = 3, b=6
MLEC@IT Page 92
Sample Output: Enter the values of n: Number 1: 12 Number 2: 32 Number 3: 35 Largest number is: 35
printf("%d ",num/factor); num = num % factor; } return 0; } Sample output: Enter a number: 123 Each digits of given number are: 1 2 3
%d",count);
Page 95
return 0; } Sample output: Enter a number: 23 Total digits is: Code 2: C code to count the total number of digit using for loop #include<stdio.h> int main(){ int num,count=0; printf("Enter a number: "); scanf("%d",&num); for(;num!=0;num=num/10) count++; printf("Total digits is: return 0; } Sample output: Enter a number: 456
MLEC@IT Page 96
%d",count);
Count the digits of a given number in c language using recursion #include<stdio.h> int countDigits(num); int main(){ int num,count; printf("Enter a number: "); scanf("%d",&num); count = countDigits(num); printf("Total digits is: return 0; } int countDigits(int num){ static int count=0; if(num!=0){ count++; countDigits(num/10); }
MLEC@IT Page 97
%d",count);
return count; } Sample output: Enter a number: 1234567 Total digits is: 7
Sample output:
scanf("%d",&n); sum = getSum(n); printf("Sum of n numbers: %d",sum); return 0; } int getSum(n){ int sum=0; while(n>0){ sum = sum + n; n--; } return sum; }
MLEC@IT
Page 100
void multiplyMatrix(int [MAX][MAX],int [MAX][MAX]); int m,n,o,p; int c[MAX][MAX]; int main(){ int a[MAX][MAX],b[MAX][MAX],i,j,k; printf("Enter the row and column of first matrix: "); scanf("%d %d",&m,&n); printf("Enter the row and column of second matrix: "); scanf("%d %d",&o,&p); if(n!=o){ printf("Matrix multiplication is
possible"); not
printf("\nColumn of first matrix must be same as row of second matrix"); } else{ printf("Enter the First matrix: "); for(i=0;i<m;i++) for(j=0;j<n;j++) scanf("%d",&a[i][j]);
MLEC@IT Page 101
printf("Enter the Second matrix: "); for(i=0;i<o;i++) for(j=0;j<p;j++) scanf("%d",&b[i][j]); printf("\nThe First matrix is: \n"); for(i=0;i<m;i++){ printf("\n"); for(j=0;j<n;j++){ printf("%d\t",a[i][j]); } } printf("\nThe Second matrix is: \n"); for(i=0;i<o;i++){ printf("\n"); for(j=0;j<p;j++){ printf("%d\t",b[i][j]); } } multiplyMatrix(a,b); } printf("\nThe multiplication of two matrix is: \n"); for(i=0;i<m;i++){
MLEC@IT Page 102
printf("\n"); for(j=0;j<p;j++){ printf("%d\t",c[i][j]); } } return 0; } void multiplyMatrix(int a[MAX][MAX],int b[MAX][MAX]){ static int sum,i=0,j=0,k=0; if(i<m){ //row of first matrix if(j<p){ //column of second matrix sum=sum+a[i][k]*b[k][j]; k++; multiplyMatrix(a,b); } c[i][j]=sum; sum=0; k=0; j++; multiplyMatrix(a,b); } j=0; i++; multiplyMatrix(a,b);
MLEC@IT Page 103
if(k<n){
} } Sample output: Enter the row and column of first matrix: 2 2 Enter the row and column of second matrix: 2 2 Enter the First matrix: 1 2 3 4 Enter the Second matrix: 2 3 4 5 The First matrix is: 1 3 2 4
C code to multiply two matrix without using recursion: #include<stdio.h> #define MAX 10 void multiplyMatrix(int [MAX][MAX],int [MAX][MAX]);
MLEC@IT Page 104
int m,n,o,p; int c[MAX][MAX]; int main(){ int a[MAX][MAX],b[MAX][MAX],i,j,k; printf("Enter the row and column of first matrix: "); scanf("%d %d",&m,&n); printf("Enter the row and column of second matrix: "); scanf("%d %d",&o,&p); if(n!=o){ printf("Matrix possible"); printf("\column of first matrix must be same as row of second matrix"); } else{ printf("Enter the First matrix: "); for(i=0;i<m;i++) for(j=0;j<n;j++) scanf("%d",&a[i][j]); printf("Enter the Second matrix: ");
MLEC@IT Page 105
multiplication
is
not
for(i=0;i<o;i++) for(j=0;j<p;j++) scanf("%d",&b[i][j]); printf("\nThe First matrix is: \n"); for(i=0;i<m;i++){ printf("\n"); for(j=0;j<n;j++){ printf("%d\t",a[i][j]); } } printf("\nThe Second matrix is: \n"); for(i=0;i<o;i++){ printf("\n"); for(j=0;j<p;j++){ printf("%d\t",b[i][j]); } } multiplyMatrix(a,b); } printf("\nThe \n"); for(i=0;i<m;i++){ printf("\n"); for(j=0;j<p;j++){
MLEC@IT Page 106
multiplication
of
two
matrix
is:
printf("%d\t",c[i][j]); } } return 0; } void multiplyMatrix(int a[MAX][MAX],int b[MAX][MAX]){ int sum,i=0,j=0,k=0; while(i<m){ //row of first matrix j=0; while(j<p){ k=0; sum=0; while(k<n){ sum=sum+a[i][k]*b[k][j]; k++; } c[i][j]=sum; j++; } i++; } } //column of second matrix
MLEC@IT
Page 107
C code to multiply two numbers by recursion: #include<stdio.h> int multiply(int,int); int main(){ int a,b,product; printf("Enter any two integers: "); scanf("%d%d",&a,&b); product = multiply(a,b); printf("Multiplication %d",product); return 0; } int multiply(int a,int b){ static int product=0,i=0; if(i < a){ product = product + b; i++; multiply(a,b);
MLEC@IT Page 108
of
two
integers
is
} return product; } Sample output: Enter any two integers: 5 8 Multiplication of two integers is 40 C code to multiply two numbers without using recursion: #include<stdio.h> int multiply(int,int); int main(){ int a,b,product; printf("Enter any two integers: "); scanf("%d%d",&a,&b); product = multiply(a,b); printf("Multiplication %d",product); return 0; }
MLEC@IT Page 109
of
two
integers
is
int multiply(int a,int b){ int product =0,i=0; while(i < a){ product = product + b; i++; } return product; }
C code to get LCM of two numbers by recursion: #include<stdio.h> int lcm(int,int); int main(){ int a,b,l; printf("Enter any two positive integers "); scanf("%d%d",&a,&b);
MLEC@IT Page 110
if(a>b) l = lcm(a,b); else l = lcm(b,a); printf("LCM of two integers is %d",l); return 0; } int lcm(int a,int b){ static int temp = 1; if(temp % b == 0 && temp % a == 0) return temp; temp++; lcm(a,b); return temp; } Sample output: Enter any two positive integers 5 2 LCM of two integers is 10 C code to get LCM of two numbers without using
recursion:
MLEC@IT Page 111
#include<stdio.h> #define MAX 100 int getMaxElement(int []); #include<stdio.h> int lcm(int,int); int main(){ int a,b,l; printf("Enter any two positive integers "); scanf("%d%d",&a,&b); if(a>b) l = lcm(a,b); else l = lcm(b,a); printf("LCM of two integers is %d",l); return 0; } int lcm(int a,int b){ int temp = a;
MLEC@IT Page 112
C code to get largest element of an array by recursion: #include<stdio.h> #define MAX 100 int getMaxElement(int []); int size; int main(){ int arr[MAX],max,i; printf("Enter the size of the array: "); scanf("%d",&size);
MLEC@IT Page 113
printf("Enter %d elements of an array: ", size); for(i=0;i<size;i++) scanf("%d",&arr[i]); max=getMaxElement(arr); printf("Largest element of an array is: %d",max); return 0; } int getMaxElement(int arr[]){ static int i=0,max =-9999; if(i < size){ if(max<arr[i]) max=arr[i]; i++; getMaxElement(arr); } return max; } Sample output: Enter the size of the array: 5 Enter 5 elements of an array: 1 4 5 6 2
MLEC@IT Page 114
Largest element of an array is: 6 C code to get largest element of an array without
recursion: #include<stdio.h> #define MAX 100 int getMaxElement(int []); int size; int main(){ int arr[MAX],max,i; printf("Enter the size of the array: "); scanf("%d",&size); printf("Enter %d elements of an array: ", size); for(i=0;i<size;i++) scanf("%d",&arr[i]); max=getMaxElement(arr); printf("Largest element of an array is: %d",max); return 0; }
MLEC@IT
Page 115
int getMaxElement(int arr[]){ int i=1,max; max=arr[0]; while(i < size){ if(max<arr[i]) max=arr[i]; i++; } return max; }
MLEC@IT
Page 116
printf("Enter the size of the array: "); scanf("%d",&size); printf("Enter %d elements of an array: ", size); for(i=0;i<size;i++) scanf("%d",&arr[i]); max=getMaxElement(arr); printf("Largest element of an array is: %d",max); return 0; } int getMaxElement(int arr[]){ static int i=0,max =-9999; if(i < size){ if(max<arr[i]) max=arr[i]; i++; getMaxElement(arr); } return max; } Sample output:
MLEC@IT Page 117
Enter the size of the array: 5 Enter 5 elements of an array: 1 4 5 6 2 Largest element of an array is: 6 C code to get largest element of an array without
recursion: #include<stdio.h> #define MAX 100 int getMaxElement(int []); int size; int main(){ int arr[MAX],max,i; printf("Enter the size of the array: "); scanf("%d",&size); printf("Enter %d elements of an array: ", size); for(i=0;i<size;i++) scanf("%d",&arr[i]); max=getMaxElement(arr); printf("Largest element of an array is: %d",max);
MLEC@IT
Page 118
return 0; } int getMaxElement(int arr[]){ int i=1,max; max=arr[0]; while(i < size){ if(max<arr[i]) max=arr[i]; i++; } return max; }
MLEC@IT
Page 119
long binaryNo; int decimalNo; printf("Enter any decimal number: "); scanf("%d",&decimalNo); binaryNo = toBinary(decimalNo); printf("Binary value is: %ld",binaryNo); return 0; } long toBinary(int decimalNo){ static long binaryNo,remainder,factor = 1; if(decimalNo != 0){ remainder = decimalNo % 2; binaryNo = binaryNo + remainder * factor; factor = factor * 10; toBinary(decimalNo / 2); } return binaryNo; } Sample output:
MLEC@IT
Page 120
Enter any decimal number: 10 Binary value is: 1010 C code to convert decimal number to binary number
without recursion: #include<stdio.h> long toBinary(int); int main(){ long binaryNo; int decimalNo; printf("Enter any decimal number: "); scanf("%d",&decimalNo); binaryNo = toBinary(decimalNo); printf("Binary value is: %ld",binaryNo); return 0; } long toBinary(int decimalNo){ long binaryNo,remainder,factor = 1; while(decimalNo != 0){
MLEC@IT Page 121
remainder = decimalNo % 2; binaryNo = binaryNo + remainder * factor; factor = factor * 10; decimalNo = decimalNo / 2; } return binaryNo; }
1. C code to print Fibonacci series by recursion 2. Fibonacci series in c by using recursion 3. C code for Fibonacci series using recursion 4. Program to generate Fibonacci series using recursion in c #include<stdio.h> void printFibonacci(int); int main(){ int k,n;
MLEC@IT Page 122
long int i=0,j=1,f; printf("Enter the range of the Fibonacci series: "); scanf("%d",&n); printf("Fibonacci Series: "); printf("%d %d ",0,1); printFibonacci(n); return 0; } void printFibonacci(int n){ static long int first=0,second=1,sum; if(n>0){ sum = first + second; first = second; second = sum; printf("%ld ",sum); printFibonacci(n-1); } } Sample output:
MLEC@IT Page 123
Enter the range of the Fibonacci series: 10 Fibonacci Series: 0 1 1 2 3 5 8 13 21 34 55 89 C code to print Fibonacci series without recursion: #include<stdio.h> void printFibonacci(int); int main(){ int k,n; long int i=0,j=1,f; printf("Enter the range of the Fibonacci series: "); scanf("%d %d ",&n); printf("Fibonacci Series: "); printf("%d ",0); printFibonacci(n); return 0; } void printFibonacci(int n){ long int first=0,second=1,sum;
MLEC@IT
Page 124
while(n>0){ sum = first + second; first = second; second = sum; printf("%ld ",sum); n--; } }
C code to reverse a string by recursion: #include<stdio.h> #define MAX 100 char* getReverse(char[]); int main(){ char str[MAX],*rev; printf("Enter any string: ");
} char* getReverse(char str[]){ static int i=0; static char rev[MAX]; if(*str){ getReverse(str+1); rev[i++] = *str; } return rev; } Sample output: Enter any string: mona Reversed string is: anom
MLEC@IT
Page 126
3. C program to calculate factorial using recursion 4. Recursive function for factorial in c #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)); }
MLEC@IT
Page 127
#include<stdio.h> int main(){ int n1,n2,gcd; printf("\nEnter two numbers: "); scanf("%d %d",&n1,&n2); gcd=findgcd(n1,n2); printf("\nGCD of %d and %d is: %d",n1,n2,gcd); return 0; } int findgcd(int x,int y){ while(x!=y){ if(x>y) return findgcd(x-y,y); else return findgcd(x,y-x); } return x; }
MLEC@IT
Page 128
#include<stdio.h> int main(){ int num,x; clrscr(); printf("\nEnter a number: "); scanf("%d",&num); x=findsum(num); printf("Sum of the digits of %d is: %d",num,x); return 0; } int r,s; int findsum(int n){ if(n){ r=n%10; s=s+r; findsum(n/10); } else return s; }
MLEC@IT
Page 129
#include<stdio.h> int main(){ int pow,num; long int res; long int power(int,int); printf("\nEnter a number: "); scanf("%d",&num); printf("\nEnter power: "); scanf("%d",&pow); res=power(num,pow); printf("\n%d to the power %d is: %ld",num,pow,res); return 0; } int i=1; long int sum=1; long int power(int num,int pow){ if(i<=pow){ sum=sum*num; power(num,pow-1); } else return sum;
MLEC@IT Page 130
#include<stdio.h> int main(){ int a[10],i,n,m,c,l,u; printf("Enter the size of an array: "); scanf("%d",&n); printf("Enter the elements of the array: " ); for(i=0;i<n;i++){ scanf("%d",&a[i]); } printf("Enter the number to be search: "); scanf("%d",&m); l=0,u=n-1; c=binary(a,n,m,l,u); if(c==0) printf("Number is not found."); else printf("Number is found.");
MLEC@IT Page 131
return 0; } int binary(int a[],int n,int m,int l,int u){ int mid,c=0; if(l<=u){ mid=(l+u)/2; if(m==a[mid]){ c=1; } else if(m<a[mid]){ return binary(a,n,m,l,mid-1); } else return binary(a,n,m,mid+1,u); } else return c; } Sample output: Enter the size of an array: 5 Enter the elements of the array: 8 9 10 11 12 Enter the number to be search: 8 Number is found.
MLEC@IT Page 132
#include<stdio.h>
MLEC@IT Page 133
int a,b,l; printf("Enter any two positive integers "); scanf("%d%d",&a,&b); if(a>b) l = lcm(a,b); else l = lcm(b,a); printf("LCM of two integers is %d",l); return 0; } int lcm(int a,int b){ int temp = a; while(1){ if(temp % b == 0 && temp % a == 0)
MLEC@IT Page 134
LCM
program
in
with
multiple
numbers
int main(){
positive
integers.
To
quit
press
else if(a>b) b = lcm(a,b); else b = lcm(b,a); } printf("LCM is %d",b); return 0; } int lcm(int a,int b){ int temp = a; while(1){ if(temp % b == 0 && temp % a == 0) break; temp++; } return temp; }
LCM
of
two
integers
is
smallest
positive
integer
which is multiple of both integers that it is divisible by the both of the numbers. For example: LCM of two integers 2 and 5 is 10 since 10 is the smallest positive numbers which is divisible by both 2 and 5.
It is clear that any number is not divisible by greater than number itself. In case of more than one numbers, a possible maximum number which can divide all of the numbers must be minimum of all of that numbers. For example: 10, 20, and 30 Min (10, 20, 30) =10 can divide all there numbers. So we will take one for loop which will start form min of the numbers and will stop the loop when it became one, since all numbers are divisible by one. Inside for loop we will write one if conditions which will check divisibility of both the numbers. Program:
Write
program
for
finding
gcd
(greatest
common
#include<stdio.h> int main(){ int x,y,m,i; printf("Insert any two number: "); scanf("%d%d",&x,&y);
MLEC@IT Page 138
if(x>y) m=y; else m=x; for(i=m;i>=1;i--){ if(x%i==0&&y%i==0){ printf("\nHCF of two number is : %d",i) ; break; } } return 0; }
program with c
#include<stdio.h> int main(){ int n1,n2; printf("\nEnter two numbers:"); scanf("%d %d",&n1,&n2); while(n1!=n2){ if(n1>=n2-1) n1=n1-n2;
MLEC@IT Page 139
HCF
#include<stdio.h> int main(){ int x,y=-1; printf("Insert numbers. To exit insert zero: "); while(1){ scanf("%d",&x); if(x<1) break; else if(y==-1) y=x; else if (x<y) y=gcd(x,y); else y=gcd(y,x); }
MLEC@IT Page 140
printf("GCD is %d",y); return 0; } int gcd(int x,int y){ int i; for(i=x;i>=1;i--){ if(x%i==0&&y%i==0){ break; } } return i; }
In HCF we try to find any largest number which can divide both the number. For example: HCF or GCD of 20 and 30 Both number 20 and 30 are divisible by 1, 2,5,10. HCF=max (1, 2, 3, 4, 10) =10 Logic for writing program: It is clear that any number is not divisible by greater than number itself. In case of more than one numbers, a possible maximum number which can divide all of the numbers must be minimum of all of that numbers. For example: 10, 20, and 30 Min (10, 20, 30) =10 can divide all there numbers. So we will take one for loop which will start form min of the numbers and will stop the loop when it became one, since all numbers are divisible by one. Inside for loop we will write one if conditions which will check divisibility of both the numbers. Program:
Write
program
for
finding
gcd
(greatest
common
#include<stdio.h>
MLEC@IT Page 142
int main(){ int x,y,m,i; printf("Insert any two number: "); scanf("%d%d",&x,&y); if(x>y) m=y; else m=x; for(i=m;i>=1;i--){ if(x%i==0&&y%i==0){ printf("\nHCF of two number is : %d",i) ; break; } } return 0; }
program with c
#include<stdio.h>
MLEC@IT Page 143
int main(){ int n1,n2; printf("\nEnter two numbers:"); scanf("%d %d",&n1,&n2); while(n1!=n2){ if(n1>=n2-1) n1=n1-n2; else n2=n2-n1; } printf("\nGCD=%d",n1); return 0; }
HCF
#include<stdio.h> int main(){ int x,y=-1; printf("Insert numbers. To exit insert zero: "); while(1){ scanf("%d",&x); if(x<1) break;
MLEC@IT Page 144
else if(y==-1) y=x; else if (x<y) y=gcd(x,y); else y=gcd(y,x); } printf("GCD is %d",y); return 0; } int gcd(int x,int y){ int i; for(i=x;i>=1;i--){ if(x%i==0&&y%i==0){ break; } } return i; }
MLEC@IT
Page 145
Swapping in c without temporary variable Swap 2 numbers without using third variable in c How to swap two numbers in c without using third
variable #include<stdio.h> int main(){ int a=5,b=10; //process one a=b+a; b=a-b; a=a-b; printf("a= %d //process two a=5;b=10; a=a+b-(b=a); printf("\na= %d //process three a=5;b=10; a=a^b;
MLEC@IT Page 146
b=
%d",a,b);
b=
%d",a,b);
b=a^b; a=b^a; printf("\na= %d //process four a=5;b=10; a=b-~a-1; b=a+~b+1; a=a+~b+1; printf("\na= %d //process five a=5,b=10; a=b+a,b=a-b,a=a-b; printf("\na= %d return 0; } b= %d",a,b); b= %d",a,b); b= %d",a,b);
MLEC@IT
Page 147
#include<stdio.h> int 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++){
MLEC@IT Page 148
#include<stdio.h> int main(){ int i=0,j=0,k=0; char str1[20],str2[20],temp[20]; puts("Enter first string"); gets(str1); puts("Enter second string"); gets(str2); printf("Before swaping the strings are\n"); puts(str1); puts(str2);
MLEC@IT Page 149
while(str1[i]!='\0'){ temp[j++]=str1[i++]; } temp[j]='\0'; i=0,j=0; while(str2[i]!='\0'){ str1[j++]=str2[i++]; } str1[j]='\0'; i=0,j=0; while(temp[i]!='\0'){ str2[j++]=temp[i++]; } str2[j]='\0'; printf("After swaping the strings are\n"); puts(str1); puts(str2); return 0; }
MLEC@IT
Page 150
#include<stdio.h> int main(){ long int decimalNumber,remainder,quotient; int binaryNumber[100],i=1,j; printf("Enter any decimal number: "); scanf("%ld",&decimalNumber); quotient = decimalNumber; while(quotient!=0){ binaryNumber[i++]= quotient % 2; quotient = quotient / 2; } printf("Equivalent binary value of decimal number %d: ",decimalNumber); for(j = i -1 ;j> 0;j--) printf("%d",binaryNumber[j]); return 0; } Sample output:
MLEC@IT Page 151
Enter any decimal number: 50 Equivalent binary value of decimal number 50: 110010
Algorithm:
Binary number system: It is base 2 number system which uses the digits from 0 and 1. Decimal number system: It is base 10 number system which uses the digits from 0 to 9 Convert from decimal to binary algorithm: Following binary Step 1: Divide the original decimal number by 2 Step 2: Divide the quotient by 2 Step 3: Repeat the step 2 until we get quotient equal to zero. Equivalent binary number would be remainders of each step in the reverse order. Decimal to binary conversion with example: steps describe how to convert decimal to
MLEC@IT
Page 152
For example we want to convert decimal number 25 in the binary. Step 1: Step 2: Step 3: Step 4: Step 5: 25 / 2 12 / 2 6 / 2 3 / 2 1 / 2 Remainder : 1 , Quotient : 12 Remainder : 0 , Quotient : 6 Remainder : 0 , Quotient : 3 Remainder : 1 , Quotient : 1 Remainder : 1 , Quotient : 0
MLEC@IT
Page 153
long int decimalNumber,remainder,quotient; int octalNumber[100],i=1,j; printf("Enter any decimal number: "); scanf("%ld",&decimalNumber); quotient = decimalNumber; while(quotient!=0){ octalNumber[i++]= quotient % 8; quotient = quotient / 8; } printf("Equivalent octal value of decimal number %d: ",decimalNumber); for(j = i -1 ;j> 0;j--) printf("%d",octalNumber[j]); return 0; } Sample output: Enter any decimal number: 50 Equivalent octal value of decimal number 50: 62 2. Easy way to convert decimal number to octal number in c
MLEC@IT Page 154
#include<stdio.h> int main(){ long int decimalNumber; printf("Enter any decimal number : "); scanf("%d",&decimalNumber); printf("Equivalent %o",decimalNumber); return 0; } Sample output: Enter any decimal number: 25 Equivalent octal number is: 31 Octal number system: It is base 8 number system which uses the digits from 0 to 7. Decimal number system: It is base 10 number system which uses the digits from 0 to 9 Decimal to octal conversion method: octal number is:
MLEC@IT
Page 155
Step 1: Divide the original decimal number by 8 Step 2: Divide the quotient by 8 Step3: Repeat the step 2 until we get quotient equal to zero. Result octal number would be remainders of each step in the reverse order. Decimal to octal conversion with example: For example we want to convert decimal number 525 in the octal. Step 1: Step 2: Step 3: Step 4: 525 / 8 65 / 8 8 / 8 1 / 8 Remainder : 5 , Quotient : 65 Remainder : 1 , Quotient : 8 Remainder : 0 , Quotient : 1 Remainder : 1 , Quotient : 0
MLEC@IT
Page 156
#include<stdio.h> int main(){ long int decimalNumber,remainder,quotient; int i=1,j,temp; char hexadecimalNumber[100]; printf("Enter any decimal number: "); scanf("%ld",&decimalNumber); quotient = decimalNumber; while(quotient!=0){ temp = quotient % 16; //To convert integer into character if( temp < 10) temp =temp + 48; else temp = temp + 55; hexadecimalNumber[i++]= temp; quotient = quotient / 16; } printf("Equivalent hexadecimal value of decimal
return 0; } Sample output: Enter any decimal number: 45 Equivalent hexadecimal value of decimal number 45: 2D 2. Easy way to convert decimal number to hexadecimal number: #include<stdio.h> int main(){ long int decimalNumber; printf("Enter any decimal number: "); scanf("%d",&decimalNumber); printf("Equivalent %X",decimalNumber); return 0; } Sample output: Enter any decimal number: 45
MLEC@IT Page 158
hexadecimal
number
is:
Equivalent hexadecimal number is: 2D Hexadecimal number system: It is base 16 number system which uses the digits from 0 to 9 and A, B, C, D, E, F. Decimal number system: It is base 10 number system which uses the digits from 0 to 9 Decimal to hexadecimal conversion method: Following steps describe how to convert decimal to
hexadecimal Step 1: Divide the original decimal number by 16 Step 2: Divide the quotient by 16 Step 3: Repeat the step 2 until we get quotient equal to zero. Equivalent binary number would be remainders of each step in the reverse order. Decimal to hexadecimal conversion example: For example we want to convert decimal number 900 in the hexadecimal. Step 1: Step 2:
MLEC@IT
900 / 16 56 / 16
Step 3:
3 / 16
Remainder : 3 , Quotient : 0
C program to convert binary to octal #include<stdio.h> #define MAX 1000 int main(){ char octalNumber[MAX]; long int i=0; printf("Enter any octal number: "); scanf("%s",octalNumber); printf("Equivalent binary value: "); while(octalNumber[i]){ switch(octalNumber[i]){ case '0': printf("000"); break; case '1': printf("001"); break;
MLEC@IT Page 160
case '2': printf("010"); break; case '3': printf("011"); break; case '4': printf("100"); break; case '5': printf("101"); break; case '6': printf("110"); break; case '7': printf("111"); break; default: } i++; } return 0; } Sample output: Enter any octal number: 123 Equivalent binary value: 001010011 printf("\nInvalid octal digit %c ",octalNumber[i]); return 0;
Algorithm:
Octal to binary conversion method: To convert or change the octal number to binary number replace the each octal digits by a binary number using octal to binary chart.
MLEC@IT Page 161
Octal 0 1 2 3 4 5 6 7
Octal to binary table
Octal to binary conversion examples: For example we want to convert or change octal number 65201 to decimal. For this we will replace each octal digit to binary values using the above table: Octal number: 6 5 2 0 1 Binary values: 110 101 010 000 001 So (65201)8 = (110101010000001)2
MLEC@IT
Page 162
C code to convert octal number to decimal number #include<stdio.h> #include<math.h> int main(){ long int octal,decimal =0; int i=0; printf("Enter any octal number: "); scanf("%ld",&octal); while(octal!=0){ decimal = decimal + (octal % 10) * pow(8,i++); octal = octal/10; } printf("Equivalent decimal value: %ld",decimal); return 0; } Sample output: Enter any octal number: 346 Equivalent decimal value: 230
MLEC@IT
Page 163
C program to change octal to decimal #include<stdio.h> int main(){ long int octalNumber; printf("Enter any octal number: "); scanf("%o",&octalNumber); printf("Equivalent %d",octalNumber); return 0; } Sample output: decimal number is:
Algorithm:
Octal to decimal conversion method:
MLEC@IT
Page 164
To convert an octal number to decimal number multiply each digits separately of octal number from right side by 8^0,8^1,8^2,8^3 respectively and then find out the sum of them. Octal to decimal conversion examples: For example we want to convert octal number 3401 to decimal number. Step 1: 1 * 8 ^0 = 1*1 =1 Step 2: 0 * 8 ^1 = 0*8 =0 Step 3: 4 * 8 ^2 = 4*64 =256 Step 4: 3 * 8 ^3 = 3*512 =1536 Sum= 1 + 0 + 256 + 1536 = 1793 So, (3401)8 = (1793)10
#define MAX 1000 int main(){ char binaryNumber[MAX],hexaDecimal[MAX]; long int i=0; printf("Enter any hexadecimal number: "); scanf("%s",hexaDecimal); printf("\nEquivalent binary value: "); while(hexaDecimal[i]){ switch(hexaDecimal[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;
MLEC@IT Page 166
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: } i++; } return 0; } Sample output: Enter any hexadecimal number: 2AD5 Equivalent binary value: 0010101011010101 printf("\nInvalid hexadecimal digit %c ",hexaDecimal[i]); return 0;
Algorithm:
Hexadecimal to binary conversion method: To convert or change the hexadecimal number to binary number replace the each octal digits by a binary number using hexadecimal to binary chart.
MLEC@IT Page 167
Hexadecimal 0 1 2 3 4 5 6 7 8 9 A B C D E F
Hexadecimal to binary table
Binary 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111
Hexadecimal to binary conversion examples: For example we want to convert or change hexadecimal number 65B2 to binary. For this we will replace each hexadecimal table: digit to binary values using the above
MLEC@IT
Page 168
So (65B2)16 = (0110010110110010)2
C program c program to convert binary to octal #include<stdio.h> int main(){ long int binaryNumber,octalNumber=0,j=1,remainder; printf("Enter any number any binary number: "); scanf("%ld",&binaryNumber); while(binaryNumber!=0){ remainder=binaryNumber%10; octalNumber=octalNumber+remainder*j; j=j*2; binaryNumber=binaryNumber/10; } printf("Equivalent octal value: %lo",octalNumber);
MLEC@IT Page 169
return 0; } Sample output: Enter any number any binary number: 1101 Equivalent hexadecimal value: 15 C code for how to convert large binary to octal #include<stdio.h> #define MAX 1000 int main(){ char binaryNumber[MAX],octalNumber[MAX]; long int i=0,j=0; printf("Enter any number any binary number: "); scanf("%s",binaryNumber); while(binaryNumber[i]){ binaryNumber[i] = binaryNumber[i] -48; ++i; } --i; while(i-2>=0){
MLEC@IT Page 170
octalNumber[j++] i=i-3; } if(i ==1) octalNumber[j] binaryNumber[i] ; else if(i==0) octalNumber[j] = else --j;
binaryNumber[i-2]
*4
binaryNumber[i-1] *2 + binaryNumber[i] ;
binaryNumber[i-1]
*2
binaryNumber[i] ;
printf("Equivalent octal value: "); while(j>=0){ printf("%d",octalNumber[j--]); } return 0; } Sample output: Enter any number any binary number: 1111111111111111111 1111111111111111111111111111111111111111111111111111111 1111111111111111111111111111111111111111111111111111111 1111111111111111111111111111111111111111111111111111111
MLEC@IT
Page 171
Alogrithm:
Binary to octal conversion method: Step1: Arrange the binary number in the group 3 from right side. Step 2: Replace the each group with following values: Octal values 0 1 2 3 4 5 6 7
Binary number 000 001 010 011 100 101 110 111
Binary to octal chart
MLEC@IT
Page 172
For
example
we
want
to
convert
binary
number
1011010101001101 to octal. Step 1: 001 011 010 101 001 101 Step 2: 1 3 2 5 1 5
So (1011010101001101)2 = (132515)8
#include<stdio.h> int main(){ long int binaryNumber,decimalNumber=0,j=1,remainder ; printf("Enter any number any binary number: "); scanf("%ld",&binaryNumber);
MLEC@IT
Page 173
while(binaryNumber!=0){ remainder=binaryNumber%10; decimalNumber=decimalNumber+remainder*j; j=j*2; binaryNumber=binaryNumber/10; } printf("Equivalent %ld",decimalNumber); return 0; } Sample output: Enter any number any binary number: 1101 Equivalent decimal value: 13 decimal value:
Algorithm:
Binary number system: It is base 2 number system which uses the digits from 0 and 1. Decimal number system: It is base 10 number system which uses the digits from 0 to 9
MLEC@IT
Page 174
For this we multiply each digit separately from right side by 1, 2, 4, 8, 16 respectively then add them.
Binary number to decimal conversion with example: For example we want to convert binary number 101111 to decimal: Step1: Step2: Step3: Step4: Step5: Step6: 1 * 1 = 1 1 * 2 = 2 1 * 4 = 4 1 * 8 = 8 0 * 16 = 0 1 * 32 = 32
MLEC@IT
Page 175
#include<stdio.h> int main(){ long int binary1,binary2; int i=0,remainder = 0,sum[20]; printf("Enter any first binary number: "); scanf("%ld",&binary1); printf("Enter any second binary number: "); scanf("%ld",&binary2); while(binary1!=0||binary2!=0){ sum[i++] remainder ) % 2; remainder remainder ) / 2; binary1 = binary1/10; binary2 = binary2/10; } if(remainder!=0)
MLEC@IT Page 176
= =
(binary1 (binary1
%10 %10
+ +
binary2 binary2
%10 %10
+ +
sum[i++] = remainder; --i; printf("Sum of two binary numbers: "); while(i>=0) printf("%d",sum[i--]); return 0; } Sample output: Enter any first binary number: 1100011 Enter any second binary number: 1101 Sum of two binary numbers: 1110000
Alogrithm:
Rule of binary addition: 0 + 0 = 0 1 + 0 = 1 0 + 1 = 1 1 + 1 = 1 and carry = 1 Q1. What is 1110? Answer: 1101 + 1110 = 11011
MLEC@IT Page 177
the
sum
of
the
binary
numbers
1101
and
C code for product of two binary numbers C program for multiplication of two binary numbers
#include<stdio.h> int binaryAddition(int,int); int main(){ long int binary1,binary2,multiply=0; int digit,factor=1; printf("Enter any first binary number: "); scanf("%ld",&binary1); printf("Enter any second binary number: "); scanf("%ld",&binary2);
MLEC@IT
Page 178
if(digit ==1){ binary1=binary1*factor; multiply binaryAddition(binary1,multiply); } else binary1=binary1*factor; binary2 = binary2/10; factor = 10; } printf("Product %ld",multiply); return 0; } int binaryAddition(int binary1,int binary2){ int i=0,remainder = 0,sum[20]; int binarySum=0; while(binary1!=0||binary2!=0){ of two binary numbers: =
MLEC@IT
Page 179
= =
(binary1 (binary1
%10 %10
+ +
binary2 binary2
%10 %10
+ +
binary1 = binary1/10; binary2 = binary2/10; } if(remainder!=0) sum[i++] = remainder; --i; while(i>=0) binarySum = binarySum*10 + sum[i--]; return binarySum; }
Sample output: Enter any first binary number: 1101 Enter any second binary number: 11 Product of two binary numbers: 100111
Algorithm:
Rule of binary multiplication:
MLEC@IT Page 180
0 * 0 = 0 1 * 0 = 0 0 * 1 = 0 1 * 1 = 1 Q. what is the product of the binary numbers 0110 and 0011? Answer: 0110 * 0011 = 10010
#include<stdio.h> #define MAX 1000 int main(){ long double fraDecimal=0.0,dFractional=0.0 ,fraFactor=0.5;
MLEC@IT
Page 181
long int dIntegral 0,bIntegral=0,bFractional[MAX]; long int intFactor=1,remainder,i=0,k=0,flag=0; char fraBinary[MAX]; printf("Enter any fractional binary number: "); scanf("%s",&fraBinary); while(fraBinary[i]){ if(fraBinary[i] == '.') flag = 1; else if(flag==0)
bIntegral = bIntegral * 10 + (fraBinary[i] -48); else bFractional[k++] = fraBinary[i] -48; i++; } while(bIntegral!=0){ remainder=bIntegral%10; dIntegral= dIntegral+remainder*intFactor; intFactor=intFactor*2; bIntegral=bIntegral/10; }
MLEC@IT
Page 182
for(i=0;i<k;i++){ dFractional fraFactor; fraFactor = fraFactor / 2; } fraDecimal = dIntegral + dFractional ; printf("Equivalent decimal value: %Lf",fraDecimal); return 0; } Sample output: Enter any fractional binary number: 11.11 Equivalent decimal value: 3.750000 = dFractional + bFractional[i] *
Algorithm:
Algorithm to convert the fractional binary to decimal or floating point binary to decimal: Step1. First we convert the integral part of binary number to decimal. from For this side we by multiply 1, 2, 4, each 8, digit 16 separately right
MLEC@IT
Page 183
respectively then add them this is integral part of decimal number. Step2. Now we convert the fractional number to decimal. For this we part of binary each digit
multiply
separately from left side by 1/2, 1/4, 1/8, 1/16 respectively then add them this is fractional part of decimal number. Step3: Add the integral and fractional part of decimal number. Example for floating point binary to decimal: For example we want to convert the binary number
101111.1101 to decimal number. Step1: Conversions of 101111 to decimal: S1: S2: S3: S4: S5: S6: 1 * 1 = 1 1 * 2 = 2 1 * 4 = 4 1 * 8 = 8 0 * 16 = 0 1 * 32 = 32
long double fraDecimal,fraBinary,bFractional 0.0,dFractional,fraFactor=0.1; long int dIntegral,bIntegral=0; long int intFactor=1,remainder,temp,i; printf("Enter any fractional decimal number: "); scanf("%Lf",&fraDecimal); dIntegral = fraDecimal; dFractional = fraDecimal - dIntegral;
while(dIntegral!=0){ remainder=dIntegral%2; bIntegral=bIntegral+remainder*intFactor; dIntegral=dIntegral/2; intFactor=intFactor*10; } for(i=1;i<=6;i++){ dFractional = dFractional * 2; temp = dFractional;
MLEC@IT
Page 186
printf("Equivalent binary value: %lf",fraBinary); return 0; } Sample output: Enter any fractional decimal number: 5.7 Equivalent binary value: 101.101100
Algorithm:
How to convert fractional decimal to binary: Following binary Step1. First we convert the integral part of binary number to decimal. Following steps describe how to convert decimal number to binary number: S1: Divide the decimal number by 2
MLEC@IT Page 187
steps
describe
how
to
convert
decimal
to
S2: Divide the quotient by 2 S3: Repeat the step 2 until we get quotient equal to zero. Equivalent binary number would be remainders of each step in the reverse order. Step2. Now we convert the fractional part of decimal number to binary. Following steps describe how to convert floating decimal to binary S1: Multiply the decimal number by 2 S2: Integral part of resultant decimal number will be first digit of fraction binary number. S3: Repeat the S1 using only fractional part of decimal number and then S2. Step3: Add the integral and fractional part of binary number. Example for floating point decimal to binary: For example we want to convert the decimal number 25.7 to binary number. Step1: Conversions of 25 to binary. S1: 25 / 2 Remainder : 1 , Quotient : 12
Page 188
MLEC@IT
12 / 2 6 / 2 3 / 2 1 / 2
So equivalent binary number is: 0.101100 Step 3: So binary value of decimal number 25.7 will be 11001 + 0.101100 = 1101.101100
void predigits(char c1,char c2); void postdigits(char c,int n); char roman_Number[1000]; int i=0; int main(){ int j; long int number; printf("Enter any natural number: "); scanf("%d",&number); if(number <= 0){ printf("Invalid number"); return 0; } while(number != 0){ if(number >= 1000){ postdigits('M',number/1000); number = number - (number/1000) * 1000; } else if(number >=500){ if(number < (500 + 4 * 100)){
MLEC@IT Page 190
postdigits('D',number/500); number = number - (number/500) * 500; } else{ predigits('C','M'); number = number - (1000-100); } } else if(number >=100){ if(number < (100 + 3 * 100)){ postdigits('C',number/100); number = number - (number/100) * 100; } else{ predigits('L','D'); number = number - (500-100); } } else if(number >=50){ if(number < (50 + 4 * 10)){ postdigits('L',number/50); number = number - (number/50) * 50; } else{ predigits('X','C'); number = number - (100-10); }
MLEC@IT Page 191
} else if(number >=10){ if(number < (10 + 3 * 10)){ postdigits('X',number/10); number = number - (number/10) * 10; } else{ predigits('X','L'); number = number - (50-10); } } else if(number >=5){ if(number < (5 + 4 * 1)){ postdigits('V',number/5); number = number - (number/5) * 5; } else{ predigits('I','X'); number = number - (10-1); } } else if(number >=1){ if(number < 4){ postdigits('I',number/1); number = number - (number/1) * 1; } else{
MLEC@IT Page 192
predigits('I','V'); number = number - (5-1); } } } printf("Roman number will be: "); for(j=0;j<i;j++) printf("%c",roman_Number[j]); return 0; } void predigits(char c1,char c2){ roman_Number[i++] = c1; roman_Number[i++] = c2; } void postdigits(char c,int n){ int j; for(j=0;j<n;j++) roman_Number[i++] = c; } Sample output:
MLEC@IT
Page 193
C code for roman numbers to English numbers #include<stdio.h> #include<string.h> int digitValue(char); int main(){ char roman_Number[1000]; int i=0; long int number =0; printf("Enter any roman number (Valid digits are I, V, X, L, C, D, M): \n"); scanf("%s",roman_Number); while(roman_Number[i]){ if(digitValue(roman_Number[i]) < 0){
MLEC@IT Page 194
roman
digit
if((strlen(roman_Number) -i) > 2){ if(digitValue(roman_Number[i]) digitValue(roman_Number[i+2])){ printf("Invalid roman number"); return 0; } } if(digitValue(roman_Number[i]) digitValue(roman_Number[i+1])) number digitValue(roman_Number[i]); else{ number (digitValue(roman_Number[i+1]) digitValue(roman_Number[i])); i++; } i++; } printf("Its decimal value is : %ld",number);
MLEC@IT Page 195
<
>= number +
number
+ -
return 0; } int digitValue(char c){ int value=0; switch(c){ case 'I': value = 1; break; case 'V': value = 5; break; case 'X': value = 10; break; case 'L': value = 50; break; case 'C': value = 100; break; case 'D': value = 500; break; case 'M': value = 1000; break; case '\0': value = 0; break; default: value = -1; } return value; } Sample output: Enter any roman number (Valid digits are I, V, X, L, C, D, M):
MLEC@IT
Page 196
C code to covert each digits of a number in English word Convert digits to words in c #include<stdio.h> int main(){ int number,i=0,j,digit; char * word[1000]; printf("Enter any integer: "); scanf("%d",&number); while(number){ digit = number %10; number = number /10;
MLEC@IT
Page 197
switch(digit){ case 0: word[i++] = "zero"; break; case 1: word[i++] = "one"; break; case 2: word[i++] = "two"; break; case 3: word[i++] = "three"; break; case 4: word[i++] = "four"; break; case 5: word[i++] = "five"; break; case 6: word[i++] = "six"; break; case 7: word[i++] = "seven"; break; case 8: word[i++] = "eight"; break; case 9: word[i++] = "nine"; break; } } for(j=i-1;j>=0;j--){ printf("%s ",word[j]); } return 0; } Sample output: Enter any integer: 23451208 two three four five one two zero eight
MLEC@IT
Page 198
C code to convert any number to English word: #include<stdio.h> #include<string.h> void toWord(int,int); char * getPositionValue(int); char * digitToWord(int); char word[100][30];
int i =0; int main(){ int j,k,subnumer; unsigned long int number; printf("Enter any postive number: "); scanf("%lu",&number); if(number ==0){
MLEC@IT Page 199
printf("Zero"); return 0; } while(number){ if(i==1){ toWord(number %10,i); number = number/10; }else{ toWord(number %100,i); number = number/100; } i++; } printf("Number in word: "); *word[i-1] = *word[i-1] - 32; for(j=i-1;j>=0;j--){ printf("%s",word[j]); } return 0; }
MLEC@IT
Page 200
if(number ==0){ }else if (number < 20 ||number %10==0){ strcpy(numberToword,digitToWord(number)); }else{ strcpy(numberToword,digitToWord((number/10)*10)); strcat(numberToword,digitToWord(number%10)); } strcat(numberToword,getPositionValue(position)); strcpy(word[i],numberToword); } char * getPositionValue(int postion){ static char positionValue[10]=" "; switch(postion){ case 1: "); break; case 2: "); break; case 3: strcpy(positionValue,"lakh "); break;
MLEC@IT Page 201
strcpy(positionValue,"hundreds strcpy(positionValue,"thousand
case 4: strcpy(positionValue,"crore "); break; case 5: strcpy(positionValue,"arab "); break; case 6: "); break; case 7: strcpy(positionValue,"neel "); break; case 8: strcpy(positionValue,"padam "); break; } return positionValue; } char * digitToWord(int digit){ static char digitInWord[10]=" "; switch(digit){ case 1: strcpy(digitInWord , "one "); break; case 2: strcpy(digitInWord , "two "); break; case 3: strcpy(digitInWord , "three "); break; case 4: strcpy(digitInWord , "four "); break; case 5: strcpy(digitInWord , "five "); break; case 6: strcpy(digitInWord , "six "); break; case 7: strcpy(digitInWord , "seven "); break; case 8: strcpy(digitInWord , "eight "); break; case 9: strcpy(digitInWord , "nine ");break; case 10: strcpy(digitInWord , "ten "); break; case 11: "); break;
MLEC@IT Page 202
strcpy(positionValue,"kharab
strcpy(digitInWord
, "eleven
case 12: "); break; case 13: "); break; case 14: "); break; case 15: "); break; case 16: "); break; case 17: "); break; case 18: "); break; case 19: "); break; case 20: "); break; case 30: "); break; case 40: "); break; case 50: "); break; case 60: "); break;
strcpy(digitInWord strcpy(digitInWord strcpy(digitInWord strcpy(digitInWord strcpy(digitInWord strcpy(digitInWord strcpy(digitInWord strcpy(digitInWord strcpy(digitInWord strcpy(digitInWord strcpy(digitInWord strcpy(digitInWord strcpy(digitInWord
, "twelve , "thirteen , "fourteen , "fifteen , "sixteen , "seventeen , "eighteen , "nineteen , "twenty , "thirty , "fourty , "fifty , "sixty
MLEC@IT
Page 203
strcpy(digitInWord strcpy(digitInWord
, "seventy , "eighty
C program for hexadecimal to binary conversion #include<stdio.h> int main(){ long int binaryNumber,hexadecimalNumber=0,j=1,remai nder; printf("Enter any number any binary number: "); scanf("%ld",&binaryNumber);
MLEC@IT
Page 204
while(binaryNumber!=0){ remainder=binaryNumber%10; hexadecimalNumber=hexadecimalNumber+remainder*j; j=j*2; binaryNumber=binaryNumber/10; } printf("Equivalent %lX",hexadecimalNumber); return 0; } Sample output: Enter any number any binary number: 1101 Equivalent hexadecimal value: D How to convert large binary number to hexadecimal #include<stdio.h> #define MAX 1000 int main(){ char binaryNumber[MAX],hexaDecimal[MAX]; int temp; long int i=0,j=0;
MLEC@IT Page 205
hexadecimal
value:
printf("Enter any number any binary number: "); scanf("%s",binaryNumber); while(binaryNumber[i]){ binaryNumber[i] = binaryNumber[i] -48; ++i; } --i; while(i-2>=0){ temp = *4 + binaryNumber[i-3] *8 + binaryNumber[i-2] binaryNumber[i-1] *2 + binaryNumber[i] ; if(temp > 9) hexaDecimal[j++] = temp + 55; else hexaDecimal[j++] = temp + 48; i=i-4; } if(i ==1) hexaDecimal[j] binaryNumber[i] + 48 ; else if(i==0) hexaDecimal[j] = else --j; binaryNumber[i] + 48 ; = binaryNumber[i-1] *2 +
MLEC@IT
Page 206
printf("Equivalent hexadecimal value: "); while(j>=0){ printf("%c",hexaDecimal[j--]); } return 0; } Sample output: Enter any number any binary number: 1010011011100011110 001001111011110001000100011101110111011110 Equivalent hexadecimal value: 14DC789EF111DDDE
Algorithm:
Binary to hexadecimal conversion method Step1: Arrange the binary number in the group 4 from right side. Step 2: Replace the each group with following values: Hexadecimal values 0 1 2
Page 207
0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111
Binary to hexadecimal chart
3 4 5 6 7 8 9 A B C D E F
Binary to hexadecimal conversion examples: For example we want to convert binary number
So (1011010101001101)2 = (1B54D)16
C code to convert one unit to other unit: #include<stdio.h> #include<math.h> int main(){ char fromUnit,toUnit; char *fUnit,*tUnit; long double fromValue,meterValue,toValue; int power =0; printf("To convert the 12 Inch to Foot\n"); printf("enter dc12\n"); printf("Ell: a\n"); printf("Femi: b\n"); printf("Foot: c\n"); printf("Inch: d\n"); printf("Light year: e\n"); printf("Metre: f\n"); printf("Mile: g\n"); printf("Nano meter: h\n");
MLEC@IT Page 209
the
the
unit
in
the
format
printf("Pace: i\n"); printf("Point: j\n"); printf("Yard: k\n"); printf("Mili meter: l\n"); printf("Centi meter: m\n"); printf("Deci meter: n\n"); printf("Deca meter: o\n"); printf("Hecto meter: p\n"); printf("Kilo meter: q\n"); scanf("%c%c%Lf",&fromUnit,&toUnit,&fromValue); switch(fromUnit){ case 'a': fUnit="ell"; break; case 'b': meterValue = fromValue ; power = 15; fUnit="fm";break; case 'c': fUnit="ft"; break; case 'd': fUnit="in"; break; case 'e': case 'f': fUnit="m"; break; case 'g': meterValue = fromValue * 1609.344; fUnit="mi"; break;
MLEC@IT Page 210
meterValue
fromValue
1.143;
meterValue meterValue
= =
fromValue fromValue = =
* *
0.3048; 0.0254; *
meterValue meterValue
fromValue
case 'h': meterValue = fromValue; fUnit="nm"; power = -9;break; case 'i': case 'j': case 'k': fUnit="yd"; break; case 'l': fUnit="mm"; break; case 'm': fUnit="cm"; break; case 'n': case 'o': case 'p': fUnit="hm"; break; case 'q': fUnit="km"; break; default: printf("Invalid input"); exit(0); } switch(toUnit){ case 'a': tUnit="ell"; break;
MLEC@IT Page 211
meterValue
fromValue =
0.762
; *
fUnit="pace"; break; meterValue meterValue meterValue meterValue meterValue meterValue meterValue meterValue = = = = = = = fromValue * * * * * * * 0.000351450; fUnit="pt";break; fromValue fromValue fromValue fromValue fromValue fromValue fromValue 0.9144; 0.001; 0.01; 0.1; 10; 100; 1000;
toValue
meterValue/1.143;
case 'b': tUnit="fm"; break; case 'c': tUnit="ft"; break; case 'd': tUnit="in"; break; case 'e': case 'f': tUnit="m";break; case 'g': tUnit="mi"; break; case 'h': tUnit="nm"; break; case 'i': tUnit="pace"; break; case 'j': tUnit="pt"; break; case 'k': tUnit="yd"; break; case 'l': tUnit="mm"; break; case 'm': tUnit="cm"; break; case 'n':
meterValue;
toValue toValue toValue toValue toValue toValue toValue toValue toValue toValue = = = =
meterValue/9.4607304725808; tUnit="ly";break;
MLEC@IT
Page 212
case 'o': toValue = meterValue/10; tUnit="deca meter"; break; case 'p': tUnit="hm"; break; case 'q': tUnit="km"; break; default: printf("Invalid input"); exit(0); } if(power==0) printf("%.4Lf else{ while(tovalue > 10 printf("%.4Lf } %s = %.4Lf*10^%d %s",fromValue,fUnit,toValue,power,tUnit); %s = %.4Lf %s",fromValue,fUnit,toValue,tUnit); toValue = meterValue/1000; toValue = meterValue/100;
return 0; }
MLEC@IT
Page 213
Conversion from uppercase to lower case using c program #include<stdio.h> #include<string.h> int main(){ char str[20]; int i; printf("Enter any string->"); scanf("%s",str); printf("The string is->%s",str); for(i=0;i<=strlen(str);i++){ if(str[i]>=65&&str[i]<=90) str[i]=str[i]+32; } printf("\nThe string in uppercase is->%s",str); return 0; }
Algorithm:
ASCII value of 'A' is 65 while 'a' is 97. Difference between them is 97 65 = 32
MLEC@IT
Page 214
So if we will add 32 in the ASCII value of 'A' then it will be 'a' and if will we subtract 32 in ASCII value of 'a' it will be 'A'. It is true for all alphabets. In general rule: Upper case character = Lower case character 32 Lower case character = Upper case character + 32
Write a c program to convert the string from lower case to upper case
Write a c program to convert the string from lower case to upper case
#include<stdio.h> int main(){ char str[20]; int i; printf("Enter any string->"); scanf("%s",str); printf("The string is->%s",str); for(i=0;i<=strlen(str);i++){ if(str[i]>=97&&str[i]<=122) str[i]=str[i]-32;
MLEC@IT Page 215
Algorithm:
ASCII value of 'A' is 65 while 'a' is 97. Difference between them is 97 65 = 32 So if we will add 32 in the ASCII value of 'A' then it will be 'a' and if will we subtract 32 in ASCII value of 'a' it will be 'A'. It is true for all alphabets. In general rule: Upper case character = Lower case character 32 Lower case character = Upper case character + 32
MLEC@IT
Page 216
#include <stdio.h> int isvowel(char chk); int main(){ char text[1000], chk; int count; count = 0; while((text[count] = getchar()) != '\n') count++; text[count] = '\0'; count = 0; while ((chk = text[count]) != '\0'){ if (isvowel(chk)){ if((chk = text[++count]) && isvowel(chk)){ putchar(text[count -1]); putchar(text[count]); putchar('\n'); } } else ++count; } return 0; } int isvowel(char chk){ if(chk == 'a' || chk == 'e' || chk == 'i' || chk == 'o' || chk == 'u') return 1;
MLEC@IT Page 217
return 0; }
Program for sorting of string in c language #include<stdio.h> int main(){ int i,j,n; char str[20][20],temp[20]; puts("Enter the no. of string to be sorted"); scanf("%d",&n); for(i=0;i<=n;i++) gets(str[i]); for(i=0;i<=n;i++) for(j=i+1;j<=n;j++){ if(strcmp(str[i],str[j])>0){ strcpy(temp,str[i]); strcpy(str[i],str[j]); strcpy(str[j],temp); }
MLEC@IT Page 218
Concatenation of two strings in c programming language #include<stdio.h> int main(){ int i=0,j=0; char str1[20],str2[20]; 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[i]!='\0'){ i++;
MLEC@IT Page 219
} while(str2[j]!='\0'){ str1[i++]=str2[j++]; } str1[i]='\0'; printf("After concatenation the strings are\n"); puts(str1); return 0; }
Concatenation
of
two
strings
using
pointer in
programming language
#include<stdio.h> int main(){ int i=0,j=0; char *str1,*str2,*str3; puts("Enter first string");
MLEC@IT Page 220
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; }
int i=0; printf("Enter a string: "); gets(str); printf("%c",*str); while(str[i]!='\0'){ if(str[i]==' '){ i++; printf("%c",*(str+i)); } i++; } return 0; } Sample output: Enter a string: Robert De Niro RDN
#include<string.h>
MLEC@IT Page 222
#include<stdio.h> int main(){ char *p; char s[20],s1[1]; printf("\nEnter a string: "); scanf("%[^\n]",s); fflush(stdin); printf("\nEnter character: "); gets(s1); p=strpbrk(s,s1); printf("\nThe string from the given %s",p); return 0; } character is:
Reverse a string in c without using temp String reverse using strrev in c programming language #include<stdio.h> #include<string.h> int main(){
MLEC@IT Page 223
char str[50]; char *rev; printf("Enter any string : "); scanf("%s",str); rev = strrev(str); printf("Reverse string is : %s",rev); return 0; } String reverse in c without using strrev String reverse in c without using string function How to reverse a string in c without using reverse function #include<stdio.h> int main(){ char str[50]; char rev[50]; int i=-1,j=0; printf("Enter any string : "); scanf("%s",str); while(str[++i]!='\0'); while(i>=0)
MLEC@IT Page 224
rev[j++] = str[--i]; rev[j]='\0'; printf("Reverse of string is : %s",rev); return 0; } Sample output: Enter any string :mlec Reverse of string is :celm Reverse a string in c using pointers C program to reverse a string using pointers #include<stdio.h> int main(){ char str[50]; char rev[50]; char *sptr = str; char *rptr = rev; int i=-1; printf("Enter any string : "); scanf("%s",str); while(*sptr){
MLEC@IT Page 225
sptr++; i++; } while(i>=0){ sptr--; *rptr = *sptr; rptr++; --i; } *rptr='\0'; printf("Reverse of string is : %s",rev); return 0; }
MLEC@IT
Page 226
C code to reverse a string by recursion: #include<stdio.h> #define MAX 100 char* getReverse(char[]); int main(){ char str[MAX],*rev; printf("Enter any string: ");
scanf("%s",str); rev = getReverse(str); printf("Reversed string is: %s",rev); return 0; } char* getReverse(char str[]){ static int i=0; static char rev[MAX]; if(*str){ getReverse(str+1);
MLEC@IT Page 227
rev[i++] = *str; } return rev; } Sample output: Enter any string: mona Reversed string is: anom
String
concatenation
in
without
using
string
functions #include<stdio.h> void stringConcat(char[],char[]); int main(){ char str1[100],str2[100]; int compare; printf("Enter first string: ");
MLEC@IT Page 228
scanf("%s",str1); printf("Enter second string: "); scanf("%s",str2); stringConcat(str1,str2); printf("String after concatenation: %s",str1); return 0; } void stringConcat(char str1[],char str2[]){ int i=0,j=0;
Sample output: Enter first string: mlec Enter second string: @it String after concatenation: mlec@it
#include<stdio.h> int stringCompare(char[],char[]); int main(){ char str1[100],str2[100]; int compare; printf("Enter first string: "); scanf("%s",str1); printf("Enter second string: ");
MLEC@IT Page 230
scanf("%s",str2); compare = stringCompare(str1,str2); if(compare == 1) printf("Both strings are equal."); else printf("Both strings are not equal"); return 0; } int stringCompare(char str1[],char str2[]){ int i=0,flag=0; while(str1[i]!='\0' && str2[i]!='\0'){ if(str1[i]!=str2[i]){ flag=1; break; } i++; } if (flag==0 && str1[i]=='\0' && str2[i]=='\0') return 1; else return 0;
MLEC@IT
Page 231
} Sample output: Enter first string: mlecit Enter second string: mlecit Both strings are equal.
String
copy
without
using
strcpy
in
programming
language #include<stdio.h>
void stringCopy(char[],char[]); int main(){ char str1[100],str2[100]; printf("Enter any string: ");
MLEC@IT Page 232
scanf("%s",str1); stringCopy(str1,str2); printf("After copying: %s",str2); return 0; } void stringCopy(char str1[],char str2[]){ int i=0; while(str1[i]!='\0'){ str2[i] = str1[i]; i++; } str2[i]='\0'; } Sample output: Enter any string: mlecit After copying: mlecit
MLEC@IT
Page 233
Program
to
convert
string
into
ASCII
values
in
programming language: #include<stdio.h> int main(){ char str[100]; int i=0; printf("Enter any string: "); scanf("%s",str); printf("ASCII values of each characters of given string: "); while(str[i]) printf("%d ",str[i++]);
return 0; } Sample Output: Enter any string:blogspot ASCII values of each characters of given string: 98 108 111 103 115 112 111 116
MLEC@IT Page 234
MLEC@IT
Page 235