by Jesmin Akther | Oct 19, 2021 | C Programming
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 lower case is->%s",str);
return 0;
}
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
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;
}
printf("\nThe string in lowercase is->%s",str);
return 0;
}
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
COUNTING DIFFERENT CHARACTERS IN A STRING USING C PROGRAM
#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;
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);
}
}
printf("The sorted string\n");
for(i=0;i<=n;i++)
puts(str[i]);
return 0;
}
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++;
}
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 c programming language
#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;
}
C code which prints initial of any name
#include<stdio.h>
int main(){
char str[20];
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
Write a c program to print the string from given character
#include<string.h>
#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 character is: %s",p);
return 0;
}
Reverse a string in c without using temp
String reverse using strrev in c programming language
#include<stdio.h>
#include<string.h>
int main(){
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)
rev[j++] = str[--i];
rev[j]='\0';
printf("Reverse of string is : %s",rev);
return 0;
}
Sample output:
Enter any string : cquestionbank.blogspot.com
Reverse of string is : moc.topsgolb.knabnoitseuqc
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);
rev[i++] = *str;
}
return rev;
}
Sample output:
Enter any string: mona
Reversed string is: anom
String concatenation in c without using string functions
#include<stdio.h>
void stringConcat(char[],char[]);
int main(){
char str1[100],str2[100];
int compare;
printf("Enter first string: ");
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;
while(str1[i]!='\0'){
i++;
}
while(str2[j]!='\0'){
str1[i] = str2[j];
i++;
j++;
}
str1[i] = '\0';
}
Sample output:
Enter first string: cquestionbank
Enter second string: @blogspot.com
String after concatenation: cquestionbank@blogspot.com
C program to compare two strings without using string functions
#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: ");
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;
}
Sample output:
Enter first string: cquestionbank.blogspot.com
Enter second string: cquestionbank.blogspot.com
Both strings are equal.
String copy without using strcpy in c programming language
#include<stdio.h>
void stringCopy(char[],char[]);
int main(){
char str1[100],str2[100];
printf("Enter any string: ");
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: cquestionbank.blogspot.com
After copying: cquestionbank.blogspot.com
Program to convert string into ASCII values in c 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: cquestionbank.blogspot.com
ASCII values of each characters of given string: 99 113 117 101 115 116 105 111 110 98 97 110 107 46 98 108 111 103 115 112 111 116 46 99 111 109
by Jesmin Akther | Oct 10, 2021 | C Programming
Example 1: C program to check perfect number
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 divisor of 6 are 1, 2 and 3. Sum of its divisor is
1 + 2+ 3 =6 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
#include<stdio.h>
int 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);
return 0;
}
Sample output:
Enter a number: 6
6 is a perfect number
Similar Example:
Write a c program to check given number is perfect number or not.
Tips:
if(sum==n)
printf(“%d is a perfect number”,i);
else
printf(“%d is not a perfect number”,i);
Example 2: How to print prime numbers from 1 to 100 in c
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: 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.
#include<stdio.h>
int 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);
}
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
Write a c program to check whether a number is strong 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
#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);
}
return 0;
}
Sample output:
Enter minimum range: 100
Enter maximum range: 100000
Strong numbers in given range are: 145 40585
C program for odd or even number
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.
- C program to check even or odd
- C determine odd or even
- How to check odd number in c
- How to determine odd or even in c
- C even odd test
#include<stdio.h>
int main(){
int number;
printf("Enter any integer: ");
scanf("%d",&number);
if(number % 2 ==0)
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.
check the given number is palindrome number or not using c program
- Write a c program for palindrome
- C program to find palindrome of a number
- 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;
}
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: 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);
return 0;
}
Sample output:
Enter a number: 1221
1221 is a palindrome
Code 4: C program to check ifa 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;
}
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
Write a c program to check given string is palindrome number or not
Definition of Palindrome string:
A string is called palindrome if it symmetric. In other word a string is called palindrome if string remains same if its characters are reversed. For example: asdsa
If we will reverse it will remain same i.e. asdsa
Example of string palindrome: a,b, aa,aba,qwertrewq etc.
#include<string.h>
#include<stdio.h>
int main(){
char *str,*rev;
int i,j;
printf("\nEnter a string:");
scanf("%s",str);
for(i=strlen(str)-1,j=0;i>=0;i--,j++)
rev[j]=str[i];
rev[j]='\0';
if(strcmp(rev,str))
printf("\nThe string is not a palindrome");
else
printf("\nThe string is a palindrome");
return 0;
}
TO FIND FIBONACCI SERIES USING C PROGRAM
Code 1:
- Write a program to generate the Fibonacci series in c
- Write a program to print Fibonacci series in c
- Basic c programs Fibonacci series
- How to print Fibonacci series in c
- How to find Fibonacci series in c programming
- Fibonacci series in c using for loop
#include<stdio.h>
int main(){
int k,r;
long int i=0l,j=1,f;
//Taking maximum numbers form user
printf("Enter the number range:");
scanf("%d",&r);
printf("FIBONACCI SERIES: ");
printf("%ld %ld",i,j); //printing firts two values.
for(k=2;k<r;k++){
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
What is Fibonacci series?
Logic of Fibonacci series Definition of Fibonacci numbers:
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.
TO FIND FACTORIAL OF A NUMBER USING C PROGRAM
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
- C code for factorial of a number
- C program to find the factorial of a given number
- Factorial program in c using while loop
4.Factorial program in c without using recursion
#include<stdio.h>
int 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);
return 0;
}
Sample output:
Enter a number: 5
Factorial of 5 is: 120
Code 2:
- Factorial program in c using for loop
- Simple factorial program in c
- 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;
}
Write a c program for Floyd’s triangle.
What is Floyd’s 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 3
4 5 6
7 8 9 10
11 12 13 14 15
16 17 18 19 20 21
- Write a c program to print Floyd’s triangle
- C program to display Floyd’s triangle
- How to print Floyd’s triangle in c
#include<stdio.h>
int 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");
}
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
Write a c program to print Pascal triangle.
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
- Pascal triangle in c without using array
- C code to print Pascal triangle
- 3. Simple c program for Pascal triangle
- C program to generate Pascal triangle
- Pascal triangle program in c language
- 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 ",fact(i)/(fact(j)*fact(i-j)));
printf("\n");
}
return 0;
}
long fact(int num){
long f=1;
int i=1;
while(i<=num){
f=f*i;
i++;
}
return f;
}
TO FIND MULTIPLICATION TABLE USING C PROGRAM
- Multiplication tables in c program
- Write a c program to print multiplication table
- Code for multiplication table in c
- Multiplication table in c language
- Write a c program to print multiplication table
#include<stdio.h>
int main(){
int r,i,j,k;
printf("Enter the number range: ");
scanf("%d",&r);
for(i=1;i<=r;i++){
for(j=1;j<=10;j++)
printf("%d*%d=%d ",i,j,i*j);
printf("\n");
}
return 0;
}
Sample Output:
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
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
3*1=3 3*2=6 3*3=9 3*4=12 3*5=15 3*6=18 3*7=21 3*8=24 3*9=27 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
PRINTING ASCII VALUE USING C PROGRAM
Printing ascii value using c program
C code for ASCII table
C program to display ASCII values
#include<stdio.h>
int main(){
int i;
for(i=0;i<=255;i++)
printf("ASCII value of character %c: %d\n",i,i);
return 0;}
Output:
ASCII value of character : 0
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
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
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
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
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
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
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
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
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
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
C program to print hello world without using semicolon
#include<stdio.h>
void main(){
if(printf("Hello world")){
}
}
Solution: 2
#include<stdio.h>
void main(){
while(!printf("Hello world")){
}
}
Solution: 3
#include<stdio.h>
void main(){
switch(printf("Hello world")){
}
}
CONVERSION OF DECIMAL TO BINARY USING C PROGRAM
#include<stdio.h>
int main(){
long int m,no=0,a=1;
int n,rem;
printf("Enter any decimal number->");
scanf("%d",&n);
m=n;
while(n!=0){
rem=n%2;
no=no+rem*a;
n=n/2;
a=a*10;
}
printf("The value %ld in binary is->",m);
printf("%ld",no);
return 0;
}
C code for binary to decimal conversion:
#include<stdio.h>
int main(){
long int binaryNumber,decimalNumber=0,j=1,remainder;
printf("Enter any number any binary number: ");
scanf("%ld",&binaryNumber);
while(binaryNumber!=0){
remainder=binaryNumber%10;
decimalNumber=decimalNumber+remainder*j;
j=j*2;
binaryNumber=binaryNumber/10;
}
printf("Equivalent decimal value: %ld",decimalNumber);
return 0;
}
Sample output:
Enter any number any binary number: 1101
Equivalent decimal value: 13
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 binary to decimal algorithm:
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: 1 * 1 = 1
Step2: 1 * 2 = 2
Step3: 1 * 4 = 4
Step4: 1 * 8 = 8
Step5: 0 * 16 = 0
Step6: 1 * 32 = 32
Its decimal value: 1 + 2 + 4+ 8+ 0+ 32 = 47
That is (101111)2 = (47)10
by Jesmin Akther | Sep 22, 2021 | C Programming
C- Strings
Strings are actually one-dimensional array of characters terminated by a null character ‘\0’. Thus a null-terminated string contains the characters that comprise the string followed by a null.
The following declaration and initialization create a string consisting of the word “Hello”. To hold the null character at the end of the array, the size of the character array containing the string is one more than the number of characters in the word “Hello.”
char greeting[6] = {'H', 'e', 'l', 'l', 'o', '\0'};
If you follow the rule of array initialization then you can write the above statement as follows −
char greeting[] = "Hello";
Following is the memory presentation of the above defined string in C/C++ −
Actually, you do not place the null character at the end of a string constant. The C compiler automatically places the ‘\0’ at the end of the string when it initializes the array. Let us try to print the above mentioned string −
#include <stdio.h>
int main () {
char greeting[6] = {'H', 'e', 'l', 'l', 'o', '\0'};
printf("Greeting message: %s\n", greeting );
return 0;
}
When the above code is compiled and executed, it produces the following result −
Greeting message: Hello
C supports a wide range of functions that manipulate null-terminated strings −
Sr.No. |
Function & Purpose |
1 |
strcpy(s1, s2);
Copies string s2 into string s1. |
2 |
strcat(s1, s2);
Concatenates string s2 onto the end of string s1. |
3 |
strlen(s1);
Returns the length of string s1. |
4 |
strcmp(s1, s2);
Returns 0 if s1 and s2 are the same; less than 0 if s1<s2; greater than 0 if s1>s2. |
5 |
strchr(s1, ch);
Returns a pointer to the first occurrence of character ch in string s1. |
6 |
strstr(s1, s2);
Returns a pointer to the first occurrence of string s2 in string s1. |
The following example uses some of the above-mentioned functions:
#include <stdio.h>
#include <string.h>
int main () {
char str1[12] = "Hello";
char str2[12] = "World";
char str3[12];
int len ; /* copy str1 into str3 */
strcpy(str3, str1);
printf("strcpy( str3, str1) : %s\n", str3 ); /* concatenates str1 and str2 */
strcat( str1, str2);
printf("strcat( str1, str2): %s\n", str1 ); /* total lenghth of str1 after concatenation */
len = strlen(str1);
printf("strlen(str1) : %d\n", len );
return 0;
}
When the above code is compiled and executed, it produces the following result −
strcpy( str3, str1) : Hello strcat( str1, str2): HelloWorld strlen(str1) : 10
C- Structures
Arrays allow to define type of variables that can hold several data items of the same kind. Similarly structure is another user defined data type available in C that allows to combine data items of different kinds.
Structures are used to represent a record. Suppose you want to keep track of your books in a library. You might want to track the following attributes about each book −
- Title
- Author
- Subject
- Book ID
Defining a Structure
To define a structure, you must use the struct statement. The struct statement defines a new data type, with more than one member. The format of the struct statement is as follows −
struct [structure tag] {
member definition;
member definition;
...
member definition;
} [one or more structure variables];
The structure tag is optional and each member definition is a normal variable definition, such as int i; or float f; or any other valid variable definition. At the end of the structure’s definition, before the final semicolon, you can specify one or more structure variables but it is optional. Here is the way you would declare the Book structure −
struct Books {
char title[50];
char author[50];
char subject[100];
int book_id;
} book;
Accessing Structure Members
To access any member of a structure, we use the member access operator (.). The member access operator is coded as a period between the structure variable name and the structure member that we wish to access. You would use the keyword struct to define variables of structure type. The following example shows how to use a structure in a program:
#include <stdio.h>
#include <string.h>
struct Books {
char title[50];
char author[50];
char subject[100];
int book_id;
};
int main( ) {
struct Books Book1; /* Declare Book1 of type Book */
struct Books Book2; /* Declare Book2 of type Book */
/* book 1 specification */
strcpy( Book1.title, "C Programming");
strcpy( Book1.author, "Nuha Ali");
strcpy( Book1.subject, "C Programming Tutorial");
Book1.book_id = 6495407;
/* book 2 specification */
strcpy( Book2.title, "Telecom Billing");
strcpy( Book2.author, "Zara Ali");
strcpy( Book2.subject, "Telecom Billing Tutorial");
Book2.book_id = 6495700;
/* print Book1 info */
printf( "Book 1 title : %s\n", Book1.title);
printf( "Book 1 author : %s\n", Book1.author);
printf( "Book 1 subject : %s\n", Book1.subject);
printf( "Book 1 book_id : %d\n", Book1.book_id);
/* print Book2 info */
printf( "Book 2 title : %s\n", Book2.title);
printf( "Book 2 author : %s\n", Book2.author);
printf( "Book 2 subject : %s\n", Book2.subject);
printf( "Book 2 book_id : %d\n", Book2.book_id);
return 0;
}
When the above code is compiled and executed, it produces the following result −
Book 1 title : C Programming
Book 1 author : Nuha Ali
Book 1 subject : C Programming Tutorial
Book 1 book_id : 6495407
Book 2 title : Telecom Billing
Book 2 author : Zara Ali
Book 2 subject : Telecom Billing Tutorial
Book 2 book_id : 6495700
Structures as Function Arguments
You can pass a structure as a function argument in the same way as you pass any other variable or pointer.
#include <stdio.h>
#include <string.h>
struct Books {
char title[50];
char author[50];
char subject[100];
int book_id; }; /* function declaration */
void printBook( struct Books book );
int main( ) {
struct Books Book1; /* Declare Book1 of type Book */
struct Books Book2; /* Declare Book2 of type Book */
/* book 1 specification */
strcpy( Book1.title, "C Programming");
strcpy( Book1.author, "Nuha Ali");
strcpy( Book1.subject, "C Programming Tutorial");
Book1.book_id = 6495407; /* book 2 specification */
strcpy( Book2.title, "Telecom Billing");
strcpy( Book2.author, "Zara Ali");
strcpy( Book2.subject, "Telecom Billing Tutorial");
Book2.book_id = 6495700; /* print Book1 info */
printBook( Book1 ); /* Print Book2 info */
printBook( Book2 );
return 0;
}
void printBook( struct Books book ) {
printf( "Book title : %s\n", book.title);
printf( "Book author : %s\n", book.author);
printf( "Book subject : %s\n", book.subject);
printf( "Book book_id : %d\n", book.book_id);
}
When the above code is compiled and executed, it produces the following result −
Book title : C Programming
Book author : Nuha Ali
Book subject : C Programming Tutorial
Book book_id : 6495407
Book title : Telecom Billing
Book author : Zara Ali
Book subject : Telecom Billing Tutorial
Book book_id : 6495700
Pointers to Structures
You can define pointers to structures in the same way as you define pointer to any other variable −
struct Books *struct_pointer;
Now, you can store the address of a structure variable in the above defined pointer variable. To find the address of a structure variable, place the ‘&’; operator before the structure’s name as follows −
struct_pointer = &Book1;
To access the members of a structure using a pointer to that structure, you must use the → operator as follows −
struct_pointer->title;
Let us re-write the above example using structure pointer.
#include <stdio.h>
#include <string.h>
struct Books {
char title[50];
char author[50];
char subject[100];
int book_id; }; /* function declaration */
void printBook( struct Books *book );
int main( ) {
struct Books Book1; /* Declare Book1 of type Book */
struct Books Book2; /* Declare Book2 of type Book */
/* book 1 specification */
strcpy( Book1.title, "C Programming");
strcpy( Book1.author, "Nuha Ali");
strcpy( Book1.subject, "C Programming Tutorial");
Book1.book_id = 6495407; /* book 2 specification */
strcpy( Book2.title, "Telecom Billing");
strcpy( Book2.author, "Zara Ali");
strcpy( Book2.subject, "Telecom Billing Tutorial");
Book2.book_id = 6495700; /* print Book1 info by passing address of Book1 */
printBook( &Book1 ); /* print Book2 info by passing address of Book2 */ printBook( &Book2 );
return 0;
}
void printBook( struct Books *book ) {
printf( "Book title : %s\n", book->title);
printf( "Book author : %s\n", book->author);
printf( "Book subject : %s\n", book->subject);
printf( "Book book_id : %d\n", book->book_id);
}
When the above code is compiled and executed, it produces the following result −
Book title : C Programming
Book author : Nuha Ali
Book subject : C Programming Tutorial
Book book_id : 6495407
Book title : Telecom Billing
Book author : Zara Ali
Book subject : Telecom Billing Tutorial
Book book_id : 6495700
Bit Fields
Bit Fields allow the packing of data in a structure. This is especially useful when memory or data storage is at a premium. Typical examples include −
- Packing several objects into a machine word. e.g. 1 bit flags can be compacted.
- Reading external file formats — non-standard file formats could be read in, e.g., 9-bit integers.
C allows us to do this in a structure definition by putting :bit length after the variable. For example −
struct packed_struct {
unsigned int f1:1;
unsigned int f2:1;
unsigned int f3:1;
unsigned int f4:1;
unsigned int type:4;
unsigned int my_int:9;
} pack;
Here, the packed_struct contains 6 members: Four 1 bit flags f1..f3, a 4-bit type and a 9-bit my_int.
C automatically packs the above bit fields as compactly as possible, provided that the maximum length of the field is less than or equal to the integer word length of the computer. If this is not the case, then some compilers may allow memory overlap for the fields while others would store the next field in the next word.