Few Sample Programs With Examples
Few Sample Programs With Examples
(Accredited ‘A’ Grade by NAAC, Approved by AICTE, New Delhi, Affiliated to JNTU Kakinada)
SAMPLE PROGRAMS
1. Program to read an integer from User
#include <stdio.h>
Void main()
int number;
scanf("%d", &number);
return 0;
Enter a integer: 25
You entered: 25
Explanation : In this program, an integer variable number is declared. The printf() function displays
Enter an integer: on the screen. Then, the scanf() function reads an integer data from the user and stores in
variable number. Finally, the value stored in the variable number is displayed on the screen using printf()
function.
#include <stdio.h>
Void main()
11
12 + 11 = 23
In this program, user is asked to enter two integers. Two integers entered by the user is stored in
variables firstNumber and secondNumber respectively. This is done using scanf()function.
Then, variables firstNumber and secondNumber are added using + operator and the result is stored
in sumOfTwoNumbers.
#include <stdio.h>
int main()
return 0;
}
1.12
Product = 2.69
#include <stdio.h>
int main()
scanf("%lf", &firstNumber);
scanf("%lf",&secondNumber);
temporaryVariable = firstNumber;
firstNumber = secondNumber;
secondNumber = temporaryVariable;
printf("\nAfter swapping, firstNumber = %.2lf\n", firstNumber);
return 0;
Output
#include<stdio.h>
int main() {
int rad;
scanf("%d", &rad);
ci = 2 * PI * rad;
return (0);
Output :
Enter radius of a circle : 1
Area of circle : 3.14
Circumference : 6.28
1
2
3
Enter radius of a circle : 1
Area of circle : 3.14
Circumference : 6.28
#include<stdio.h>
int main()
{
int a,b,sub;
//Read value of a
printf("Enter the first no.: ");
scanf("%d",&a);
//Read value of b
printf("Enter the second no.: ");
scanf("%d",&b);
//formula of subtraction
sub= a-b;
printf("subtract is = %d\n", sub);
return 0;
}
Output
First run:
Enter the first no.: 40
Enter the second no.: 45
subtract is = -5
Second run:
Enter the first no.: 45
Enter the second no.: 40
subtract is = 5
switch case Examples/Programs
13. Print weekday name program according to given weekday number using switch
/*C program to read weekday number and print weekday name using switch.*/
#include <stdio.h>
int main()
{
int wDay;
switch(wDay)
{
case 0:
printf("Sunday");
break;
case 1:
printf("Monday");
break;
case 2:
printf("Tuesday");
break;
case 3:
printf("Wednesday");
break;
case 4:
printf("Thursday");
break;
case 5:
printf("Friday");
break;
case 6:
printf("Saturday");
break;
default:
printf("Invalid weekday number.");
}
printf("\n");
return 0;
}
Output
First Run:
Enter weekday number (0-6): 3
Wednesday
Second run:
Enter weekday number (0-6): 9
Invalid weekday number.
14.Print gender (Male/Female) program according to given M/F using switch
/*C program to read gender (M/F) and print corresponding gender using switch.*/
#include <stdio.h>
int main()
{
char gender;
switch(gender)
{
case 'M':
case 'm':
printf("Male.");
break;
case 'F':
case 'f':
printf("Female.");
break;
default:
printf("Unspecified Gender.");
}
printf("\n");
return 0;
}
Output
First Run:
Enter gender (M/m or F/f): M
Male.
Second Run:
Enter gender (M/m or F/f): x
Unspecified Gender.
#include <stdio.h>
int main()
{
char ch;
return 0;
}
Output
First Run:
Enter a character: E
E is a VOWEL.
Second Run:
Enter a character: X
X is a CONSONANT.
Third Run:
Enter a character: +
+ is not an alphabet.
int main()
{
int num1,num2;
float result;
char ch; //to store operator choice
result=0;
switch(ch)
{
case '+':
result=num1+num2;
break;
case '-':
result=num1-num2;
break;
case '*':
result=num1*num2;
break;
case '/':
result=(float)num1/(float)num2;
break;
case '%':
result=num1%num2;
break;
default:
printf("Invalid operation.\n");
}
printf("Result: %d %c %d = %f\n",num1,ch,num2,result);
return 0;
}
Output
First run:
Enter first number: 10
Enter second number: 20
Choose operation to perform (+,-,*,/,%): +
Result: 10 + 20 = 30.000000
Second run:
Enter first number: 10
Enter second number: 3
Choose operation to perform (+,-,*,/,%): /
Result: 10 / 3 = 3.333333
Third run:
Enter first number: 10
Enter second number: 3
Choose operation to perform (+,-,*,/,%): >
Invalid operation.
Result: 10 > 3 = 0.000000
17.Check EVEN or ODD program using switch
#include <stdio.h>
int main()
{
int number;
return 0;
}
Output
First run:
Enter a positive integer number: 10
10 is an EVEN number.
Second run:
Enter a positive integer number: 11
11 is an ODD number.
#include <stdio.h>
int main()
{
int month;
int days;
switch(month)
{
case 4:
case 6:
case 9:
case 11:
days=30;
break;
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
days=31;
break;
case 2:
days=28;
break;
default:
days=0;
break;
}
if(days)
printf("Number of days in %d month is: %d\n",month,days);
else
printf("You have entered an invalid month!!!\n");
return 0;
}
Output
First run:
Enter month: 3
Number of days in 3 month is: 31
Second run:
Enter month: 2
Number of days in 2 month is: 28
Third run:
Enter month: 11
Number of days in 11 month is: 30
Fourth run:
Enter month: 13
You have entered an invalid month!!!
#include<stdio.h>
int main()
{
//counter, it can also be declared as 'char'
int count;
//initializing counter by 'a'
count= '0';
//definig label
start:
printf("%c [%d] ",count,count);
count++;
//jumping back to 'stat' if condition is true
if(count <= '9')
goto start;
return 0;
}
Output
0 [48] 1 [49] 2 [50] 3 [51] 4 [52] 5 [53] 6 [54] 7 [55] 8 [56] 9 [57]
20.print ASCII values of all lowercase alphabets
Program
#include<stdio.h>
int main()
{
//counter, it can also be declared as 'char'
int count;
//initializing counter by 'a'
count= 'a';
//definig label
start:
printf("%c [%d] ",count,count);
count++;
//jumping back to 'stat' if condition is true
if(count <= 'z')
goto start;
return 0;
}
Output
Program
#include<stdio.h>
int main()
{
//counter, it can also be declared as 'char'
int count;
//initializing counter by 'A'
count= 'A';
//definig label
start:
printf("%c [%d] ",count,count);
count++;
//jumping back to 'stat' if condition is true
if(count <= 'Z')
goto start;
return 0;
}
Output
22. C program to print 1, 11, 31, 61, ... 10 times using goto statement
#include<stdio.h>
int main()
{
int count,a,diff;
start: //label
printf(" %d ",a);
a=a+diff;
diff=diff+10;
count++;
if(count<=10)
goto start;
return 0;
}
Output
23.Program to print Square and Cube of all numbers from 1 to N using goto in C
#include<stdio.h>
int main()
{
int count,n;
count=1;
start:
printf("num: %4d, Square: %4d, Cube: %4d\n",count,(count*count),(count*count*count));
count++;
if(count<=n)
goto start;
return 0;
}
Output
#include<stdio.h>
int main()
{
//declare variable
int count,t,r;
//Read value of t
printf("Enter number: ");
scanf("%d",&t);
count=1;
start:
if(count<=10)
{
//Formula of table
r=t*count;
printf("%d*%d=%d\n",t,count,r);
count++;
goto start;
}
return 0;
}
Output
Enter number: 10
10*1=10
10*2=20
10*3=30
10*4=40
10*5=50
10*6=60
10*7=70
10*8=80
10*9=90
10*10=100
#include <stdio.h>
int main()
{
int count,n;
//read value of N
printf("Enter value of n: ");
scanf("%d",&n);
start: //label
printf("%d ",count);
count--;
if(count>=1)
goto start;
return 0;
}
Output
First run:
Enter value of n: 10
10 9 8 7 6 5 4 3 2 1
Second run:
Enter value of n: 100
100 99 98 97 96 95 94 93 92 91 90 89 88 87 86 85
84 83 82 81 80 79 78 77 76 75 74 73 72 71 70 69
68 67 66 65 64 63 62 61 60 59 58 57 56 55 54 53
52 51 50 49 48 47 46 45 44 43 42 41 40 39 38 37
36 35 34 33 32 31 30 29 28 27 26 25 24 23 22 21 20
19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
#include <stdio.h>
int main()
{
int num; /*to store number*/
int i; /*loop counter*/
return 0;
}
Output
for(i=1;i<=10;i++){
printf("%d\n",(num*i));
}
#include <stdio.h>
int main()
{
int i,j; /*Here, we will use i for outer loop counter
and j for inner loop counter*/
int num;
Output
1 2 3 4 5 6 7 8 9 10
2 4 6 8 10 12 14 16 18 20
3 6 9 12 15 18 21 24 27 30
4 8 12 16 20 24 28 32 36 40
5 10 15 20 25 30 35 40 45 50
6 12 18 24 30 36 42 48 54 60
7 14 21 28 35 42 49 56 63 70
8 16 24 32 40 48 56 64 72 80
9 18 27 36 45 54 63 72 81 90
10 20 30 40 50 60 70 80 90 100
11 22 33 44 55 66 77 88 99 110
12 24 36 48 60 72 84 96 108 120
13 26 39 52 65 78 91 104 117 130
14 28 42 56 70 84 98 112 126 140
15 30 45 60 75 90 105 120 135 150
16 32 48 64 80 96 112 128 144 160
17 34 51 68 85 102 119 136 153 170
18 36 54 72 90 108 126 144 162 180
19 38 57 76 95 114 133 152 171 190
20 40 60 80 100 120 140 160 180 200
Number is ZERO.
Number is POSITIVE.
Number is NEGATIVE.
Number is POSITIVE.
Bye Bye!!!
29.Factorial program in C
Simple program - without using User Define Function
#include <stdio.h>
int main()
{
int num,i;
long int fact;
printf("\nFactorial of %d is = %ld",num,fact);
return 0;
}
Output
Factorial of 7 is = 5040
User Define Function
#include <stdio.h>
for(i=n;i>=1;i--)
fact= fact * i;
return fact;
}
int main()
{
int num;
return 0;
}
Output
Factorial of 7 is = 5040
Using Recursion
This program will read the value of N and calculate sum from 1 to N, first N natural numbers means
numbers from 1 to N and N will be read through user.
2 #include <stdio.h>
3 int main()
4 {
5 int n,i;
6 int sum;
8 scanf("%d",&n);
9 sum=0;
10 for(i=1;i< n;i++)
11
sum+=i;
12
printf("\nSum is = %d",sum);
13
return 0;
14
}
Sum is = 4950
This program will read the value of N and print all prime numbers from 1 to N. The logic behind
implement this program - Run loop from 1 to N and check each value in another loop, if the value is
divisible by any number between 2 to num-1 (or less than equal to num/2) - Here num is the value to
check it is prime of not.
2 #include <stdio.h>
4 int i;
5 int flg=0;
8 for(i=2;i<(num-1);i++)
9 {
10 if(num%i==0){
11
flg=1;
12
break;
13
}
14
}
15
if(flg) return 0;
16
else return 1;
17
}
18
Another Code:
19
int main()
20
{
21
int i,n;
22
printf("Enter the value of N: ");
23
scanf("%d",&n);
24
printf("All prime numbers are from 1 to %d:\n",n);
25
for(i=1;i<=n;i++)
26
{
27
if(checkPrime(i))
28
printf("%d,",i);
29
}
30
return 0;
31
}
1,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,
32. C program to check numbers are EVEN or ODD from 1 to N.
This program will read value of N and print all numbers from 1 to N with EVEN and ODD after checking
the numbers whether they are EVEN or ODD.
2 #include <stdio.h>
5 if(num%2 == 0)
6 return 1;
7 else
8 return 0;
9 }
10 Another Code:
11 int main()
12 {
13 int i,n;
15 scanf("%d",&n);
16 for(i=1;i<=n;i++)
17 {
18 if(checkEven(i))
19 printf("%4d [EVEN]\t",i);
20 else
21 printf("%4d [ODD]\t",i);
22
}
23
return 0;
24
}
1,153,370,371,407,
34. Print Square, Cube and Square Root using C program
/*C program to print square, cube and square root of all numbers from 1 to N.*/
#include <stdio.h>
#include <math.h>
int main()
{
int i,n;
return 0;
}
Output
#include <stdio.h>
int main()
{
int i,n;
return 0;
}
Output
/*C program to print all upper case and lower case alphabets.*/
#include <stdio.h>
int main()
{
char i;
return 0;
}
Output
#include <stdio.h>
int main()
{
int age;
int cnt_baby=0,cnt_school=0,cnt_adult=0;
int count=0;
while(count<15)
{
printf("Enter age of person [%d]: ",count+1);
scanf("%d",&age);
//increase counter
count++;
}
Output
As we know a string can be printed using printf("%s",string_variable) but in this program we will print
string one by one character using loop. Here loop is running from 0 (access first character from character
array) to Null (Null is the last character from character array - to terminate the string.)
In this program we will print all VOWEL and CONSONANT characters from entered string separately,
logic behind to implement this program too easy, you have to just check if characters are vowels print
them as vowels and not print them as consonants.
40. C program to count upper case, lower case and special characters in a string.
In this program, we will count upper case, lower case and special characters. Logic behind to implement
this program is too easy, just check characters is alphabet or not, if it is alphabet check for lower and
upper case characters, if characters are not alphabets count them as special characters.
1 /*C program to count upper case, lower case and special characters in a string.*/
2
3 #include<stdio.h>
4
5 int main()
6 {
7 char text[100];
8 int i;
9 int countL,countU,countS;
10
11 printf("Enter any string: ");
12 gets(text);
13
14 //here, we are printing string using printf
15 //without using loop
16 printf("Entered string is: %s\n",text);
17
18 //count lower case, upper case and special characters
19 //assign 0 to counter variables
20 countL=countU=countS=0;
21
22 for(i=0;text[i]!='\0';i++)
23 {
24 //check for alphabet
25 if((text[i]>='A' && text[i]<='Z') || (text[i]>='a' && text[i]<='z'))
26 {
27 if((text[i]>='A' && text[i]<='Z'))
28 {
29 //it is upper case alphabet
30 countU++;
31 }
32 else
33 {
34 //it is lower case character
35 countL++;
36 }
37 }
38 else
39 {
40 //character is not an alphabet
41 countS++; //it is special character
42 }
43 }
44
45 //print values
46 printf("Upper case characters: %d\n",countU);
47 printf("Lower case characters: %d\n",countL);
48 printf("Special characters: %d\n",countS);
49
50 return 0;
51 }
Enter any string: Hello Friends, I am Mike.
Entered string is: Hello Friends, I am Mike.
Upper case characters: 4
Lower case characters: 15
Special characters: 6
In this program, we will convert string in upper case and lower case. First we will read a string and then
convert string in upper case and after printing the string that is converted into upper case, we will convert
it in lower case and print the lower case. Logic behind to implement this program - Just check the
alphabets if they are in upper case add the value 32 [0x20 - hex value] to convert the character into lower
case otherwise subtract 32 [0x20 - hex value] to convert the character into upper case.
Because difference between in the ascii codes of upper case and lower case characters are 32 [0x20 - hex
value].
In this program, we will toggle all character’s case of string. Logic behind to implement this program is -
Just convert lower case character in upper case and upper case character in lower case but first check that
character is alphabet or not, if it is alphabet then do it.
#include <stdio.h>
#include <string.h>
int main()
{
//Declare Variables
char string[10][30]; //2D array for storing strings
int i, n;
Output
#include <stdio.h>
#define MAX 100
int main()
{
char text[MAX]={0};
int loop,j;
return 0;
}
Output
#include <stdio.h>
#define MAX 100
//function prototypes
/*
This function will return o if 'ch' is vowel
else it will return 1
*/
char isVowel(char ch);
/*
This function will eliminate/remove all vowels
from a string 'buf'
*/
void eliminateVowels(char *buf);
//read string
printf("Enter string: ");
scanf("%[^\n]s",str); //to read string with spaces
//eliminate vowles
eliminateVowels(str);
printf("After eliminating vowels string: %s\n",str);
return 0;
}
//function definitions
while(buf[i]!='\0')
{
if(isVowel(buf[i])==0)
{
//shift other character to the left
for(j=i; buf[j]!='\0'; j++)
buf[j]=buf[j+1];
}
else
i++;
}
Output
#include <stdio.h>
#define MAX_WORDS 10
int main()
{
char text[100]={0}; // to store string
int cnt[MAX_WORDS]={0}; //to store length of the words
int len=0,i=0,j=0;
//read string
printf("Enter a string: ");
scanf("%[^\n]s",text); //to read string with spaces
while(1)
{
if(text[i]==' ' || text[i]=='\0')
{
//check NULL
if(text[i]=='\0')
{
if(len>0)
{
cnt[j++]=len;
len=0;
}
break; //terminate the loop
}
cnt[j++]=len;
len=0;
}
else
{
len++;
}
i++;
}
printf("Words length:\n");
for(i=0;i<j;i++)
{
printf("%d, ",cnt[i]);
}
printf("\b\b \n"); //to remove last comma
return 0;
}
Output
int main()
{
//read string
printf("Enter a string: ");
scanf("%[^\n]s",text); //to read string with spaces
while(1)
{
if(text[i]==' ' || text[i]=='\0')
{
//check NULL
if(text[i]=='\0')
{
if(len>0)
{
cnt[j++]=len;
len=0;
}
break; //terminate the loop
}
cnt[j++]=len;
len=0;
}
else
{
len++;
}
i++;
}
printf("Words length:\n");
for(i=0;i<j;i++)
{
printf("%d, ",cnt[i]);
}
printf("\b\b \n"); //to remove last comma
return 0;
}
Output
Enter a string: Hi there how are you?
Words length:
2, 5, 3, 3, 4
48.Program to find occurrence of a character in an input string in C
#include <stdio.h>
#define MAX 100
int main()
{
char str[MAX]={0};
char ch;
int count,i;
//input a string
printf("Enter a string: ");
scanf("%[^\n]s",str); //read string with spaces
return 0;
}
Output
First run
Enter a string: Hello world!
Enter a character: l
'l' found 3 times in "Hello world!"
Second run
Enter a string: Hello world!
Enter a character: x
'x' found 0 times in "Hello world!"
49.Program to capitalize first character of each word in a string in C
#include <stdio.h>
#define MAX 100
int main()
{
char str[MAX]={0};
int i;
//input string
printf("Enter a string: ");
scanf("%[^\n]s",str); //read string with spaces
return 0;
}
Output
First run:
Enter a string: HELLO FRIENDS HOW ARE YOU?
Capitalize string is: Hello Friends How Are You?
Second run:
Enter a string: hello friends how are you?
Capitalize string is: Hello Friends How Are You?
Third run:
Enter a string: 10 ways to learn programming.
Capitalize string is: 10 Ways To Learn Programming.
50.Program to create, read and print an array of strings in C
#include <stdio.h>
#define MAX_STRINGS 10
#define STRING_LENGTH 50
int main()
{
//declaration
char strings[MAX_STRINGS][STRING_LENGTH];
int loop,n;
printf("Enter strings...\n");
for(loop=0; loop<n; loop++)
{
printf("String [%d] ? ",loop+1);
printf("\nStrings are...\n");
for(loop=0; loop<n; loop++)
{
printf("[%2d]: %s\n",loop+1,strings[loop]);
}
return 0;
}
Output
Strings are...
[ 1]: Hello friends, How are you?
[ 2]: I hope, you all are fine!
[ 3]: By the way, what are you doing?
[ 4]: I hope you're doing good.
[ 5]: Bye Bye!!!
51.Program to compare two strings using pointers in C
#include <stdio.h>
int main()
{
//declare string variables
char str1[MAX]={0};
char str2[MAX]={0};
//read strings
printf("Enter string 1: ");
scanf("%[^\n]s",pStr1);
printf("Enter string 2: ");
getchar(); //read & ignore extra character
scanf("%[^\n]s",pStr2);
//print strings
printf("string1: %s\nstring2: %s\n",pStr1,pStr2);
//comparing strings
for(loop=0; (*(pStr1+loop))!='\0'; loop++)
{
if(*(pStr1+loop) != *(pStr2+loop))
{
flag=0;
break;
}
}
if(flag)
printf("Strings are same.\n");
else
printf("Strings are not same.\n");
return 0;
}
Output
First run:
Enter string 1: IncludeHelp.com
Enter string 2: Include.com
string1: IncludeHelp.com
string2: Include.com
Strings are not same.
Second run:
Enter string 1: includehelp.com
Enter string 2: includehelp.com
string1: includehelp.com
string2: includehelp.com
Strings are same.
#include <stdio.h>
int main()
{
char str[30],ch;
int ind[10],loop,j;
j=0;
for(loop=0; str[loop]!='\0'; loop++)
{
if(str[loop]==ch)
ind[j++]=loop;
}
printf("Input string is: %s\n",str);
printf("Indexes: ");
for(loop=0; loop<j; loop++)
printf("%d \t",ind[loop]);
return 0;
}
Output
Bitwise Operators
#include <stdio.h>
/*function declaration
* name : getBinary
* Desc : to get binary value of decimal number
* Parameter : int -integer number
* return : void
*/
void getBinary(int);
int main()
{
int num=0;
printf("Enter an integer number :");
scanf("%d",&num);
printf("\nBinary value of %d is =",num);
getBinary(num);
return 0;
}
Output
#include <stdio.h>
int main()
{
unsigned char data=0x0A;
data^=(1<<1);
data^=(1<<2);
return 0;
}
Output
1
/* C Program to demonstrate example right shift (>>) operator.*/
2
#include <stdio.h>
3
4
int main()
5
{
6
7
unsigned int num= 0xff;
8
9
printf("\nValue of num = %04X before right shift.",num);
10
11
/*shifting 2 bytes right*/
12
num = (num>>2);
13
printf("\nValue of num = %04X after right shift.",num);
14
return 0;
15
}
16
#include <stdio.h>
int main()
{
char str[100]={0};
int length;
return 0;
}
/*function definition...*/
int stringLength(char* txt)
{
int i=0,count=0;
while(txt[i++]!='\0'){
count+=1;
}
return count;
}
Output
#include <stdio.h>
/*Function declaration*/
int getSubString(char *source, char *target,int from, int to);
int main()
{
char text [100]={0};
char text1[50] ={0};
int from,to;
if(getSubString(text,text1,from,to)==0)
printf("Substring is: %s\n",text1);
else
printf("Function execution failed!!!\n");
return 0;
}
/*Function definition*/
int getSubString(char *source, char *target,int from, int to)
{
int length=0;
int i=0,j=0;
//get length
while(source[i++]!='\0')
length++;
if(from<0 || from>length){
printf("Invalid \'from\' index\n");
return 1;
}
if(to>length){
printf("Invalid \'to\' index\n");
return 1;
}
for(i=from,j=0;i<=to;i++,j++){
target[j]=source[i];
}
return 0;
}
Output
First run:
Second run:
Enter a string: This is IncludeHelp
Enter from index: 5
Enter to index: 50
String is: This is IncludeHelp
Invalid 'to' index
Function execution failed!!!
60. program to copy one string to another (implementation of strcpy) in C
#include <stdio.h>
/********************************************************
* function name :stringCpy
* Parameter :s1,s2 : string
* Description : copies string s2 into s1
********************************************************/
void stringCpy(char* s1,char* s2);
int main()
{
char str1[100],str2[100];
stringCpy(str2,str1);
Output
#include <stdio.h>
/********************************************************
* function name :stringLwr, stringUpr
* Parameter :character pointer s
* Description
stringLwr - converts string into lower case
stringUpr - converts string into upper case
********************************************************/
void stringLwr(char *s);
void stringUpr(char *s);
int main()
{
char str[100];
printf("Enter any string : ");
scanf("%[^\n]s",str);//read string with spaces
stringLwr(str);
printf("String after stringLwr : %s\n",str);
stringUpr(str);
printf("String after stringUpr : %s\n",str);
return 0;
}
Output
#include <stdio.h>
#include <string.h>
//function declaration
int myStrStr(char * str, char * sub);
int main()
{
char str[100] ={0};
char strTocheck[10] ={0};
if(myStrStr(str,strTocheck))
printf("String \"%s\" found in \"%s\"\n",strTocheck,str);
else
printf("String \"%s\" not found in \"%s\"\n",strTocheck,str);
return 0;
}
//function definition
int myStrStr(char * str, char * sub)
{
int flag = 0;
int i=0,len1=0,len2=0;
len1 = strlen(str);
len2 = strlen(sub);
while(str[i] != '\0')
{
if(str[i] == sub[0])
{
if((i+len2)>len1)
break;
if(strncmp(str+i,sub,len2)==0)
{
flag = 1;
break;
}
}
i++;
}
return flag;
}
Output
First run:
Enter complete string: Hello how are you?
Enter string to check: how
String "how" found in "Hello how are you?"
Second run:
Enter complete string: Hello how are you?
Enter string to check: we
String "we" not found in "Hello how are you?"
Third run:
Enter complete string: Hello how are you?
Enter string to check: how are you?
String "how are you?" found in "Hello how are you?"
63. Program to compare two strings without using library function in C
#include <stdio.h>
#include <ctype.h>
/********************************************************
* function name :stringCmp, stringCmpi
* Parameter :char* s1,char* s2
* Return :0- success, 1- fail
* Description
stringCmp - compares two strings
stringCmpi - compares two string (ignoring case)
********************************************************/
int stringCmp (char *s1,char *s2);
int stringCmpi(char *s1,char *s2);
int main()
{
char str1[100],str2[100];
if(!stringCmp(str1,str2))
printf("\n stringCmp :String are same.");
else
printf("\n stringCmp :String are not same.");
if(!stringCmpi(str1,str2))
printf("\n stringCmpi :String are same.");
else
printf("\n stringCmpi :String are not same.");
printf("\n");
return 0;
}
Output
#include <stdio.h>
#include <string.h>
#define MAX_SIZE 100
/********************************************************
* function name :stringCat
* Parameter :char* s1,char* s2
* Return :void
* Description :Concatenate two strings
********************************************************/
void stringCat (char *s1,char *s2);
int main()
{
char str1[MAX_SIZE],str2[MAX_SIZE];
stringCat(str1,str2);
printf("\nAfter concatenate strings are :\n");
printf("String 1: %s \nString 2: %s",str1,str2);
printf("\n");
return 0;
}
len=strlen(s1);
for(i=0;i< strlen(s2); i++)
{
s1[len+i]=s2[i];
}
s1[len+i]='\0'; /* terminates by NULL*/
}
Output
In this program, we will learn how to reverse a string without using library function?
Here, we are declaring two strings (character arrays), first string will store the input string and other will
store the reversed string.
#include <stdio.h>
#include <string.h>
int main()
{
char str[100],revStr[100];
int i,j;
return 0;
}
Output
Hello
Guys
This
is
test
string.
68. C program to count upper case and lower case characters in a string.
1 /*C program to count upper case and lower case characters in a string.*/
2 #include <stdio.h>
3
4 int main()
5 {
6 char str[100];
7 int countL,countU;
8 int counter;
9
10 //assign all counters to zero
11 countL=countU=0;
12
13 printf("Enter a string: ");
14 gets(str);
15
16 for(counter=0;str[counter]!=NULL;counter++){
17
18 if(str[counter]>='A' && str[counter]<='Z')
19 countU++;
20 else if(str[counter]>='a' && str[counter]<='z')
21 countL++;
22 }
23
24 printf("Total Upper case characters: %d, Lower Case characters: %d",countU,countL);
25
26 return 0;
27 }
In this C program, we are going to learn how to count digits, spaces, special characters and alphabets?.
Given a string and we have to count digits, spaces, special characters and alphabets using C program.
int main()
{
char str[100];
int countDigits,countAlphabet,countSpecialChar,countSpaces;
int counter;
for(counter=0;str[counter]!=NULL;counter++)
{
return 0;
}
Output
Digits: 5
Alphabets: 16
Spaces: 6
Special Characters: 5
70. Decimal to Binary Conversion using C program
#include <stdio.h>
int main()
{
int number,cnt,i;
int bin[32];
return 0;
}
Output
First Run:
Second Run:
2 #include <stdio.h>
3 int main()
4 {
5 int n;
8 scanf("%d",&n);
9 /*Reversing Number*/
10 revNumber=0;
11 while(n>0)
12 {
13
15 revNumber=(revNumber*10)+dig;
16 n=n/10;
17 }
19 return 0;
20 }
3 #include <stdio.h>
5 /* function: reverseNum
7 */
10 {
11 int sum=0,rem;
12 while(num > 0)
13 {
14 rem=num%10;
15 sum=(sum*10)+rem;
16 num=num/10;
17 }
18
19 return sum;
20 }
21
22 int main()
23 {
24 int n;
26 scanf("%d",&n);
27
29 return 0;
30 }
#include <stdio.h>
int main()
{
int n;
int dig, sum,pro;
while(n>0)
{
dig=n%10; /*get digit*/
sum+=dig;
pro*=dig;
n=n/10;
}
return 0;
}
#include <stdio.h>
/* function: sumDigits
to calculate sum of all digits.
*/
return sum;
}
/* function: proDigits
to calculate product of all digits.
*/
return pro;
}
int main()
{
int n;
printf("\nEnter an integer number :");
scanf("%d",&n);
Output
2 #include <stdio.h>
3 int main()
4 {
5 int tally;
6 int number;
9 scanf("%d",&number);
11 {
12 if(number%tally ==0)
13
{
14
flag=1;
15
break;
16
}
17
}
18
if(flag==0)
19
printf("\n %d is a prime number.",number);
20
else
21
printf("\n %d is not a prime number.",number);
22
23
return 0;
24
}
3 #include <stdio.h>
7 {
9 int tally;
10
12 {
13 if(num%tally ==0)
14 {
15 flag=1;
16 break;
17 }
18 }
19
20 if(flag==0)
22 else
24 }
25
26 int main()
27 {
28 int number;
29
31 scanf("%d",&number);
32
33
34 if(isPrime(number))
36 else
38
39 return 0;
40 }
First run:
Second run:
2 #include <stdio.h>
3 int main()
4 {
7 scanf("%d", &number);
8 tempNumber=number;
9 while(tempNumber!=0)
10 {
11 rem=tempNumber%10;
12
revNumber=revNumber*10+rem;
13
tempNumber/=10;
14
}
15
/* checking if number is equal to reverse number */
16
if(revNumber==number)
17
printf("%d is a palindrome.", number);
18
else
19
printf("%d is not a palindrome.", number);
20
21
return 0;
22
}
3 #include <stdio.h>
7 {
8 int tempNumber=num;
9 int dig,revNumber;
10
12 revNumber=0;
13 while(num>0)
14 {
15 dig=num%10;
16 revNumber=(revNumber*10)+dig;
17 num/=10;
18 }
19
20 if(revNumber==tempNumber)
22 else
24 }
25
26 int main()
27 {
28 int number;
29
31 scanf("%d", &number);
32
33 if(isPalindrome(number))
35 else
37
38 return 0;
39 }
First run:
12321 is a palindrome.
Second run:
#include <stdio.h>
int main()
{
int number, sum=0, rem=0,tempNumber;
tempNumber=number;
while(tempNumber!=0)
{
rem=tempNumber%10;
sum=sum + (rem*rem*rem);
tempNumber/=10;
}
return 0;
}
#include <stdio.h>
/*function to check Armstrong Number*/
int isArmstrong(int num)
{
int tempNumber=num;
int rem,sum;
if(sum==num)
return 1; /*Armstrong Number*/
else
return 0; /*Not an Armstrong Number*/
}
int main()
{
int number;
if(isArmstrong(number))
printf("%d is an Armstrong number.",number);
else
printf("%d is not an Armstrong number.",number);
return 0;
}
Output
First run:
Enter an integer number: 153
153 is an Armstrong number.
Second run:
Enter an integer number: 167
167 is not an Armstrong number.
2 #include <stdio.h>
5 {
6 int count=0;
7 while(num>0)
8 {
9
10 count++;
11 num/=10;
12 }
13 return count;
14 }
15
16 int main()
17 {
18 int num,tNum,cnt;
19
21 scanf("%d",&num);
22
23 cnt=countDigits(num);
24
26
27 return 0;
28 }
29
#include <stdio.h>
int main()
{
int num,tNum,digit,cnt;
int rem;
cnt=0;
tNum=num;
while(tNum>0)
{
rem=tNum%10;
if(rem==digit)
cnt++;
tNum/=10;
}
return 0;
}
#include <stdio.h>
cnt=0;
while(num>0)
{
rem=num%10;
if(rem==dig)
cnt++;
num/=10;
}
return cnt;
}
int main()
{
int num, digit, cnt;
cnt=findOccurrence(num,digit);
return 0;
}
Output
2 #include <stdio.h>
3 int main()
4 {
5 int num,loop;
6 int sum;
8 scanf("%d",&num);
9 sum=0;
10 for(loop=1; loop<num;loop++)
11 {
12 if(num%loop==0)
13
14
15
16
sum+=loop;
17
}
18
if(sum==num)
19
printf("%d is a perfect number.",num);
20
else
21
printf("%d is not a perfect number.",num);
22
23
return 0;
24
}
25
26
27
28
3 #include <stdio.h>
7 {
8 int loop,sum=0;
9
10 for(loop=1; loop<num; loop++)
11 {
12 if(num%loop==0)
13 sum+=loop;
14 }
15
16 if(sum==num)
18 else
20 }
21
22
23 int main()
24 {
25 int num,loop;
26 int sum;
27
29 scanf("%d",&num);
30
31 if(isPerfect(num))
33 else
35
36 return 0;
37 }
First Run:
6 is a perfect number.
Second Run:
Third Run:
#include <stdio.h>
int main()
{
int num;
int tempNum,flag;
tempNum=num;
flag=0;
/*check power of two*/
while(tempNum!=1)
{
if(tempNum%2!=0){
flag=1;
break;
}
tempNum=tempNum/2;
}
if(flag==0)
printf("%d is a number that is the power of 2.",num);
else
printf("%d is not the power of 2.",num);
return 0;
}
#include <stdio.h>
/*function definition*/
int isPowerOf2(int number)
{
while(number!=1)
{
if(number%2!=0)
return 0;
number=number/2;
}
return 1;
}
int main()
{
int num;
printf("Enter an integer number: ");
scanf("%d",&num);
if(isPowerOf2(num))
printf("%d is a number that is the power of 2.",num);
else
printf("%d is not the power of 2.",num);
return 0;
}
Output
First Run:
Enter an integer number: 32
32 is a number that is the power of 2.
Second Run:
Enter an integer number: 36
36 is not the power of 2.
2 #include <stdio.h>
3 #include <math.h>
5 int main()
6 {
7 int num;
8 int iVar;
9 float fVar;
10
12 scanf("%d",&num);
13
14 fVar=sqrt((double)num);
15 iVar=fVar;
16
17 if(iVar==fVar)
19 else
21
22 return 0;
23 }
24
2 #include <stdio.h>
3 #include <math.h>
4 /*function definition*/
6 {
7 int iVar;
8 float fVar;
9 fVar=sqrt((double)number);
10 iVar=fVar;
11 if(iVar==fVar)
12 return 1;
13 else
14 return 0;
15 }
16
17 int main()
18 {
19 int num;
21 scanf("%d",&num);
22
23 if(isPerfectSquare(num))
26
27 else
29
30 return 0;
31 }
32
33
First Run:
16 is a perfect square.
Second Run:
Half, Full, Incremented and Decrement Stars Series, Pyramid Pattern programs
Program - 1
Program - 2
Program - 3
Program - 5
Number Pyramid Programs - Half, Full Incremented and Decrement Series programs
Program - 6
Program - 7
Program - 8
Program - 10
Program - 11
1 /*
2 C program to print following pyramid
3 123454321
4 1234321
5 12321
6 121
7 1
8 */
9
10 #include <stdio.h>
11
12 int main()
13 {
14 int i,j;
15 int space=0;
16
17 /*Run parent loop*/
18 for(i=5; i>=1; i--)
19 {
20 /*Print space*/
21 for(j=1; j<=space; j++)
22 printf(" ");
23
24
25 /*Run loop to print first part of row*/
26 for(j=1; j<=i; j++)
27 printf("%d",j);
28
29 /*Run loop to print second part of row*/
30 for(j=i-1; j>=1; j--)
31 printf("%d",j);
32
33 printf("\n");
34 space++;
35 }
36
37 return 0;
38 }
123454321
1234321
12321
121
1
Program - 12
1 /*
2 C program to print character pyramid as given below:
3 A
4 BC
5 DEF
6 GHIJ
7 KLMNO
8 */
9
10 #include <stdio.h>
11
12 int main()
13 {
14 int i,j;
15 char ch='A';
16
17 for(i=1;i<=5;i++)
18 {
19 for(j=1;j<=i;j++)
20 {
21 printf("%c ",ch++);
22 }
23 printf("\n");
24 }
25
26 return 0;
27 }
A
BC
DEF
GHIJ
KLMNO
Program - 13
1 /*
2 C program to print character pyramid as given below:
3 A
4 BC
5 DEF
6 GHIJ
7 KLMNO
8 */
9
10 #include <stdio.h>
11
12 int main()
13 {
14 int i,j;
15 char ch;
16
17 for(i=1;i<=5;i++)
18 {
19 ch='A';
20 for(j=1;j<=i;j++)
21 {
22 printf("%c ",ch++);
23 }
24 printf("\n");
25 }
26
27 return 0;
28 }
A
AB
ABC
ABCD
ABCDE
Program - 14
1 /*
2 C program to print following character pyramid:
3 ABCDEDCBA
4 ABCD DCBA
5 ABC CBA
6 AB BA
7 A A
8 */
9
10 #include <stdio.h>
11
12 int main()
13 {
14 int i,j;
15 char CH='E';
16 int space=1;
17
18 /*loop for row*/
19 for(i=1; i<=5; i++)
20 {
21 /*first part of the row*/
22 for(j='A'; j<=CH; j++)
23 printf("%c",j);
24
25 /*remove last character of first part of first row*/
26 if(i==1)
27 printf("\b");
28
29 /*spaces between first, second part of the row*/
30 for(j=1; j<space; j++)
31 printf(" ");
32
33 /*second part of the row*/
34 for(j=CH; j>='A'; j--)
35 printf("%c",j);
36
37 printf("\n");
38 CH--;
39 space++;
40 }
41
42 return 0;
43 }
ABCDEDCBA
ABCD DCBA
ABC CBA
AB BA
A A
Program - 15
1 /*
2 C program to print following pyramid:
3 A
4 BAB
5 CBABC
6 DCBABCD
7 EDCBABCDE
8 */
9
10 #include <stdio.h>
11
12 int main()
13 {
14 int i,j;
15 char CH='A';
16 int space=4;
17
18 /*loop for row*/
19 for(i=1; i<=5; i++)
20 {
21 /*put space*/
22 for(j=1; j<=space; j++)
23 printf(" ");
24
25 /*first part of the row*/
26 for(j=CH; j>='A'; j--)
27 printf("%c",j);
28
29 /*second part of the row*/
30 for(j='B'; j<=CH; j++)
31 printf("%c",j);
32
33 printf("\n");
34 CH++;
35 space--;
36 }
37
38 return 0;
39 }
A
BAB
CBABC
DCBABCD
EDCBABCDE
Program - 16
1 /*
2 C program to print following pyramid
3 1A2B3C4D5E
4 1A2B3C4D
5 1A2B3C
6 1A2B
7 1A
8 */
9
10 #include <stdio.h>
11
12 int main()
13 {
14 int i,j,k;
15
16 /*Run parent loop*/
17 for(i=5; i>=1; i--)
18 {
19 for(j=1, k='A'; j<=i; j++,k++)
20 {
21 printf("%d%c",j,k);
22 }
23
24 printf("\n");
25 }
26
27 return 0;
28 }
1A2B3C4D5E
1A2B3C4D
1A2B3C
1A2B
1A
Program - 17
1 /*
2 C program to print following pyramid
3 A
4 ABA
5 ABCBA
6 ABCDCBA
7 ABCDEDCBA
8 */
9
10 #include <stdio.h>
11
12 int main()
13 {
14 int i,j;
15 int space=4;
16 char CH='A';
17
18 /*Run parent loop*/
19 for(i=1; i<=5; i++)
20 {
21 /*Print space*/
22 for(j=1; j<=space; j++)
23 printf(" ");
24
25
26 /*Run loop to print first part of row*/
27 for(j=1; j<=i; j++)
28 printf("%c",CH+j-1);
29
30 /*Run loop to print second part of row*/
31 for(j=i-1; j>=1; j--)
32 printf("%c",CH+j-1);
33
34 printf("\n");
35 space--;
36 }
37
38 return 0;
39 }
A
ABA
ABCBA
ABCDCBA
ABCDEDCBA
87. Sum of series programs/examples in C programming language
Sum of Series Programs in C - This section contains programs to solve (get sum) of different mathematic
series using C programming.
1. 1+2+3+4+..N<
2. 1^2+2^2+3^2+4^2+..N^2
3 #include<stdio.h>
5 int main()
6 {
7 int i,N,sum;
11 scanf("%d",&N);
12
14 sum=0;
15
18 sum= sum+ i;
19
21
23
24 return 0;
25 }
3 #include<stdio.h>
5 int main()
6 {
7 int i,N;
12 scanf("%d",&N);
13
14 /*set sum by 0*/
15 sum=0;
16
18 for(i=1;i<=N;i++)
20
22
24
25 return 0;
26 }
4 */
6 #include<stdio.h>
10 {
11 int i;
14
15 /*multiply num*num-1*num-2*..1*/
17 fact= fact*i;
18
19 /*return factorial*/
20 return fact;
21 }
22
23 int main()
24 {
25 int i,N;
26 float sum;
27
30 scanf("%d",&N);
31
33 sum=0.0f;
34
36 for(i=1;i<=N;i++)
38
40
42
43 return 0;
44 }
4 */
6 #include<stdio.h>
8 int main()
9 {
10 int i,N;
11 float sum;
12
15 scanf("%d",&N);
16
18 sum=0.0f;
19
21 for(i=1;i<=N;i++)
23
25
27
28 return 0;
29 }
4 */
6 #include<stdio.h>
7 #include<math.h>
9 int main()
10 {
11 int i,N;
12 float sum;
13 int count;
14
15
18 scanf("%d",&N);
19
21 sum=0.0f;
22
24 count=1;
25 for(i=1;i<=N;i++)
26 {
28 count+=2;
29 }
30
32
34
35 return 0;
36 }
4 */
5
6 #include<stdio.h>
8 /*function declarations*/
11
12 int main()
13 {
14 int number1,number2;
15 int sum;
16 float avg;
17
19 scanf("%d",&number1);
20
22 scanf("%d",&number2);
23
24 /*function calling*/
25 sum=sumTwoNum(number1,number2);
26 avg=averageTwoNum(number1,number2);
27
30
31 return 0;
32 }
33
34 /*function definitions*/
35 /*
36 * Function : sumTwoNum
39 */
41 {
43 int sum;
44 sum=x+y;
45 return sum;
46 }
47 /*
48 * Function : averageTwoNum
51 */
53 {
55 float average;
56 return ((float)(x)+(float)(y))/2;
57 }
Enter the first integer number: 100
Enter the second integer number: 201
Number1: 100, Number2: 201
Sum: 301, Average: 150.500000
2) C program to print Table of an Integer Number using User Define Functions.
1 /*
4 */
6 #include<stdio.h>
8 /*function declaration*/
9 void printTable(int);
10
11 int main()
12 {
13 int number;
14
16 scanf("%d",&number);
17
18 printf("Table of %d is:\n",number);
19 printTable(number);
20
21 return 0;
22 }
23
24 /* function definition...
25 * Function : printTable
26 * Argumenets : int- to pass number for table
28 */
30 {
31 int i;
32
34 printf("%5d\n",(num*i));
35 }
3) C program to find Sum of all Array Elements by passing array as an argument using User Define
Functions.
1 /*
4 */
5
6 #include<stdio.h>
10 /*function declaration*/
11
13
14 int main()
15 {
16 int N,i,sum;
17 int arr[MAX_ELEMENTS];
18
20 scanf("%d",&N);
21
22 if(N>MAX_ELEMENTS)
23 {
25 return 0;
26 }
27 else if(N<0)
28 {
30 return 0;
31 }
32
36 {
38 scanf("%d",&arr[i]);
39 }
40
41 /*function calling*/
42 sum=sumOfElements(arr,N);
43
45
46 return 0;
47 }
48
49 /* function definition...
50 * Function : sumOfElements
53 */
54
56 {
57 int sum,i;
58 sum=0;
59
61 {
62 sum += x[i];
63 }
64
65 return sum;
66 }
First Run:
Enter total number of elements(1 to 100): 5
Enter array elements:
Enter element 1: 11
Enter element 2: 22
Enter element 3: 33
Enter element 4: 44
Enter element 5: 55
Second Run:
Enter total number of elements(1 to 100): 120
You can't input larger than MAXIMUM value
Third Run:
Enter total number of elements(1 to 100): -10
You can't input NEGATIVE or ZERO value.
4) C program to find Length of the String by passing String/ Character Array as an Argument using User
Define Functions.
1 /*
4 */
6 #include<stdio.h>
8 /*function declaration*/
10
11 int main()
12 {
13 char text[100];
14 int length;
15
17 scanf("%[^\n]s",text);
18 /*we can also use gets(text) - To read complete text untill '\n'*/
19
20 length=stringLength(text);
21
24
25 return 0;
26 }
27
28 /* function definition...
29 * Function : stringLength
32 */
34 {
35 int len=0;
36
39
40 /*return len*/
41 return len;
42 }
5) C program to find Total Amount of purchased Items by Passing Structure as an Argument using User
Define Functions.
1 /*
6 #include<stdio.h>
8 /*declaration of structure*/
9 struct Item
10 {
11 char itemName[30];
12 int quantity;
14 };
15
16 /*function declaration*/
18
19 int main()
20 {
24
26 scanf("%[^\n]s",IT.itemName);
27 /*we can also use gets(IT.itemName) - To read complete text untill '\n'*/
28
31
33 scanf("%d",&IT.quantity);
34
35
37 tAmount=getTotalAmount(IT);
38
41 return 0;
42 }
43
44 /* function definition...
45 * Function : getTotalAmount
48 */
50 {
53
55
56 return (item.totalAmount);
57 }
#include <stdio.h>
int main()
{
int arr[10];
readArray(arr,10);
printArray(arr,10);
return 0;
}
Output
Enter elements :
Enter arr[0] : 1
Enter arr[1] : 2
Enter arr[2] : 3
Enter arr[3] : 4
Enter arr[4] : 5
Enter arr[5] : 6
Enter arr[6] : 7
Enter arr[7] : 8
Enter arr[8] : 9
Enter arr[9] : 10
Elements are :
arr[0] : 1
arr[1] : 2
arr[2] : 3
arr[3] : 4
arr[4] : 5
arr[5] : 6
arr[6] : 7
arr[7] : 8
arr[8] : 9
arr[9] : 10
#include <stdio.h>
int main()
{
int arr[10];
int sum,product,i;
return 0;
}
Output
Enter elements :
Enter arr[0] : 11
Enter arr[1] : 22
Enter arr[2] : 3
Enter arr[3] : 4
Enter arr[4] : 5
Enter arr[5] : 66
Enter arr[6] : 7
Enter arr[7] : 8
Enter arr[8] : 9
Enter arr[9] : 10
#include <stdio.h>
/** funtion : readArray()
input : arr ( array of integer ), size
to read ONE-D integer array from standard input device (keyboard).
**/
void readArray(int arr[], int size)
{
int i =0;
return product;
}
int main()
{
int arr[10];
readArray(arr,10);
return 0;
}
Output
Enter elements :
Enter arr[0] : 11
Enter arr[1] : 22
Enter arr[2] : 3
Enter arr[3] : 4
Enter arr[4] : 5
Enter arr[5] : 66
Enter arr[6] : 7
Enter arr[7] : 8
Enter arr[8] : 9
Enter arr[9] : 10
92. Find Smallest and Largest elements from One Dimensional Array using C program
Enter arr[0] : 11
Enter arr[1] : 22
Enter arr[2] : 33
Enter arr[3] : 44
Enter arr[4] : 55
Enter arr[5] : 66
Enter arr[6] : 77
Enter arr[7] : 66
Enter arr[8] : 56
Enter arr[9] : 56
int main()
{
int arr[10];
readArray(arr,10);
replaceEvenOdd(arr,10);
return 0;
}
Output
Enter elements :
Enter arr[0] : 1
Enter arr[1] : 2
Enter arr[2] : 3
Enter arr[3] : 4
Enter arr[4] : 4
Enter arr[5] : 3
Enter arr[6] : 4
Enter arr[7] : 5
Enter arr[8] : 6
Enter arr[9] : 7
Before replacement :
Elements are :
arr[0] : 1
arr[1] : 2
arr[2] : 3
arr[3] : 4
arr[4] : 4
arr[5] : 3
arr[6] : 4
arr[7] : 5
arr[8] : 6
arr[9] : 7
After replacement :
Elements are :
arr[0] : 1
arr[1] : 0
arr[2] : 1
arr[3] : 0
arr[4] : 0
arr[5] : 1
arr[6] : 0
arr[7] : 1
arr[8] : 0
arr[9] : 1
Enter elements :
Enter arr[0] : 12
Enter arr[1] : 23
Enter arr[2] : 34
Enter arr[3] : 45
Enter arr[4] : 56
Enter elements :
Enter arr[0] : 11
Enter arr[1] : 22
Enter arr[2] : 33
Enter arr[3] : 44
Enter arr[4] : 55
Elements are :
arr[0] : 12
arr[1] : 23
arr[2] : 34
arr[3] : 45
arr[4] : 56
arr[5] : 11
arr[6] : 22
arr[7] : 33
arr[8] : 44
arr[9] : 55
95.Add and Subtract elements of Two One Dimensional Array using C program
#include<stdio.h>
#define MAX 20
/* function : readArray()
to read array elements.
*/
/* function : printArray()
to print array elements.
*/
void printArray(int a[],int size)
{
int i;
for(i=0;i < size; i++)
printf("%5d",a[i]);
}
/* function : addArray(),
to add elements of two arrays.
*/
void addArray(int a[],int b[],int c[],int size)
{
int i;
for(i=0; i< size;i++)
c[i]=a[i]+b[i];
}
/* function : subArray(),
to subtract elements of two arrays.
*/
void subArray(int a[],int b[],int c[],int size)
{
int i;
for(i=0; i< size;i++)
c[i]=a[i]-b[i];
}
int main()
{
int A[MAX],B[MAX],ADD[MAX],SUB[MAX];
int i,n;
/* add Arrays*/
addArray(A,B,ADD,n);
/* subtract two Arrays*/
subArray(A,B,SUB,n);
printf("\n\n");
return 0;
}
Output
first run
45 find @ 3 position.
second run
Enter elements :
Enter arr[0] : 12
Enter arr[1] : 10
Enter arr[2] : 67
Enter arr[3] : 88
Enter arr[4] : 10
Before Sorting
Elements are :
arr[0] : 12
arr[1] : 10
arr[2] : 67
arr[3] : 88
arr[4] : 10
After Sorting
Elements are :
arr[0] : 10
arr[1] : 10
arr[2] : 12
arr[3] : 67
arr[4] : 88
11 22 33 44 55 66
66 55 44 33 22 11
55 44 33 22 11
99. C program to swap adjacent elements of a one dimensional array.
1 /*C program to swap adjacent elements of an one dimensional array.*/
3 #include <stdio.h>
5 int main()
6 {
7 int arr[MAX],n,i;
8 int temp;
11 scanf("%d",&n);
12
14 if(n%2 !=0)
15 {
17 return 1;
18 }
22 {
24 scanf("%d",&arr[i]);
25 }
26 //swap adjacent elements
28 {
29 temp = arr[i];
30 arr[i] = arr[i+1];
31 arr[i+1]= temp;
32 }
33
36 {
37 printf("%d\n",arr[i]);
38 }
39 return;
40 }
First Run:
Second Run:
Enter element 1: 10
Enter element 2: 20
Enter element 3: 30
Enter element 4: 40
Enter element 5: 50
Enter element 6: 60
Enter element 7: 70
Enter element 8: 80
Enter element 9: 90
20
10
40
30
60
50
80
70
100
90
3 #include <stdio.h>
5 int main()
6 {
7 int arr[MAX],n,i;
8 int num,count;
11 scanf("%d",&n);
12
15 for(i=0;i< n;i++)
16 {
18 scanf("%d",&arr[i]);
19 }
20
22 scanf("%d",&num);
23
25 count=0;
26 for(i=0;i< n;i++)
27 {
28 if(arr[i]==num)
29 count++;
30 }
32 return 0;
33 }
Enter element 1: 10
Enter element 2: 10
Enter element 3: 20
Enter element 4: 30
Enter element 5: 10
Occurrence of 10 is: 3
2 #include <stdio.h>
4 int main()
5 {
6 int arr[MAX],n,i,j;
7 int temp;
10 scanf("%d",&n);
11
14 for(i=0;i< n;i++)
15 {
17 scanf("%d",&arr[i]);
18 }
19
20 //sort array
21 for(i=0;i< n;i++)
22 {
23 for(j=i+1;j< n;j++)
24 {
25 if(arr[i]>arr[j])
26 {
27 temp =arr[i];
28 arr[i] =arr[j];
29 arr[j] =temp;
30 }
31 }
32 }
33
35 for(i=0;i< n;i++)
36 {
37 printf("%d\n",arr[i]);
38 }
39 return 0;
40 }
Enter total number of elements: 5
100
200
300
800
999
2 #include <stdio.h>
4 int main()
5 {
6 int arr[MAX],n,i,j;
7 int temp;
10 scanf("%d",&n);
11
12 //read array elements
14 for(i=0;i< n;i++)
15 {
17 scanf("%d",&arr[i]);
18 }
19
20 //sort array
21 for(i=0;i< n;i++)
22 {
23 for(j=i+1;j< n;j++)
24 {
25 if(arr[i]< arr[j])
26 {
27 temp =arr[i];
28 arr[i] =arr[j];
29 arr[j] =temp;
30 }
31 }
32 }
33
35 for(i=0;i< n;i++)
36 {
37 printf("%d\n",arr[i]);
38 }
39 return 0;
40 }
999
800
300
200
100
2 #include <stdio.h>
4 int main()
5 {
6 int arr[MAX],n,i,j;
7 int num,countDel;
8
9
11 scanf("%d",&n);
12
15 for(i=0;i< n;i++)
16 {
18 scanf("%d",&arr[i]);
19 }
20
22 scanf("%d",&num);
23
24 //delete elements
25 countDel=0;
26 for(i=0;i< n;i++)
27 {
28 if(arr[i]==num)
29 {
30 countDel++;
32 for(j=i;j< n;j++){
33 arr[j]=arr[j+1];
34 }
35 }
36 }
37 if(countDel)
39 else
41
43 for(i=0;i<(n-countDel);i++)
44 {
45 printf("%d\n",arr[i]);
46 }
47 return 0;
48 }
First Run:
Enter element 1: 10
Enter element 2: 20
Enter element 3: 10
Enter element 4: 30
Enter element 5: 10
Enter element 6: 40
Enter element 7: 10
Enter element 8: 50
Enter element 9: 60
Enter element 10: 70
20
30
40
50
60
70
Second Run:
Enter element 1: 10
Enter element 2: 20
Enter element 3: 10
Enter element 4: 30
Enter element 5: 10
Enter element 6: 40
Enter element 7: 10
Enter element 8: 50
Enter element 9: 60
90 not found.
2 #include <stdio.h>
4 int main()
5 {
6 int arr[MAX],revArr[MAX],i,j,n;
10 scanf("%d",&n);
11
14 for(i=0;i< n;i++)
15 {
17 scanf("%d",&arr[i]);
18 }
19
22 {
23 revArr[j]=arr[i];
24 }
25
27 for(i=0;i< n;i++)
28 {
29 printf("%d\n",revArr[i]);
30 }
31 return 0;
32 }
Enter element 1: 10
Enter element 2: 20
Enter element 3: 30
Enter element 4: 40
Enter element 5: 50
50
40
30
20
10
105. Program to find first repeated element in C
#include <stdio.h>
int main()
{
int arr[5];
int i,j,n=5;
int ind,ele; //to store index & element
printf("\n");
ind=-1;
//check first repeated element
for(i=0; i<n; i++)
{
for(j=i+1; j<n; j++)
{
if(arr[i]==arr[j])
{
ele=arr[j];
ind=j;
break;
}
}
}
if(ind!=-1)
printf("%d repeated @ %d index\n",ele,ind);
else
printf("There is no repeated element\n");
return 0;
}
Output
Firsr run:
Enter element 1: 10
Enter element 2: 20
Enter element 3: 30
Enter element 4: 20
Enter element 5: 40
Array elements are: 10 20 30 20 40
20 repeated @ 3 index
Second run:
Enter element 1: 10
Enter element 2: 20
Enter element 3: 30
Enter element 4: 40
Enter element 5: 50
Array elements are: 10 20 30 40 50
There is no repeated element
106.C Program to read and print a RxC Matrix, R and C must be input by User
This program will read a two dimensional array (Matrix), number of rows (R) and number of columns (C)
will be read through the User.
#include <stdio.h>
#define MAXROW 10
#define MAXCOL 10
int main()
{
int matrix[MAXROW][MAXCOL];
int i,j,r,c;
printf("\nMatrix is :\n");
for(i=0;i< r;i++)
{
for(j=0;j< c;j++)
{
printf("%d\t",matrix[i][j]);
}
printf("\n"); /*new line after row elements*/
}
return 0;
}
Output
Matrix is :
1 1 1
2 2 2
3 3 3
107.C Program to read a matrix and find sum, product of all elements of two dimensional (matrix)
array
This program will read a matrix and prints sum and product of all elements of the two dimensional array.
#include <stdio.h>
#define MAXROW 10
#define MAXCOL 10
int main()
{
int matrix[MAXROW][MAXCOL];
int i,j,r,c;
int sum,product;
Output
1 2 3 SUM : 6
4 5 6 SUM : 15
7 8 9 SUM : 24
Transpose Matrix is :
1 4
2 5
3 6
First Run:
Second Run:
6
11
16
111.Program to find sum and subtraction of two matrices.
1 #include <stdio.h>
2
3 #define MAXROW 10
4 #define MAXCOL 10
5
6
7 /*User Define Function to Read Matrix*/
8 void readMatrix(int m[][MAXCOL],int row,int col)
9 {
10 int i,j;
11 for(i=0;i< row;i++)
12 {
13 for(j=0;j< col;j++)
14 {
15 printf("Enter element [%d,%d] : ",i+1,j+1);
16 scanf("%d",&m[i][j]);
17 }
18 }
19 }
20
21 /*User Define Function to Read Matrix*/
22 void printMatrix(int m[][MAXCOL],int row,int col)
23 {
24 int i,j;
25 for(i=0;i< row;i++)
26 {
27 for(j=0;j< col;j++)
28 {
29 printf("%d\t",m[i][j]);
30 }
31 printf("\n");
32 }
33 }
34
35 int main()
36 {
37 int a[MAXROW][MAXCOL],b[MAXROW][MAXCOL],result[MAXROW][MAXCOL];
38 int i,j,r1,c1,r2,c2;
39
40
41 printf("Enter number of Rows of matrix a: ");
42 scanf("%d",&r1);
43 printf("Enter number of Cols of matrix a: ");
44 scanf("%d",&c1);
45
46 printf("\nEnter elements of matrix a: \n");
47 readMatrix(a,r1,c1);
48
49
50
51 printf("Enter number of Rows of matrix b: ");
52 scanf("%d",&r2);
53 printf("Enter number of Cols of matrix b: ");
54 scanf("%d",&c2);
55
56 printf("\nEnter elements of matrix b: \n");
57 readMatrix(b,r2,c2);
58
59
60 /*sum and sub of Matrices*/
61 if(r1==r2 && c1==c2)
62 {
63 /*Adding two matrices a and b, and result storing in matrix result*/
64 for(i=0;i< r1;i++)
65 {
66 for(j=0;j< c1;j++)
67 {
68 result[i][j]=a[i][j]+b[i][j];
69 }
70 }
71
72 /*print matrix*/
73 printf("\nMatrix after adding (result matrix):\n");
74 printMatrix(result,r1,c1);
75
76 /*Subtracting two matrices a and b, and result storing in matrix result*/
77 for(i=0;i< r1;i++)
78 {
79 for(j=0;j< c1;j++)
80 {
81 result[i][j]=a[i][j]-b[i][j];
82 }
83 }
84
85 /*print matrix*/
86 printf("\nMatrix after subtracting (result matrix):\n");
87 printMatrix(result,r1,c1);
88
89 }
90 else
91 {
92 printf("\nMatrix can not be added, Number of Rows & Cols are Different");
93 }
94 return 0;
95 }
12 24 36
48 60 72
84 96 108
10 20 30
40 50 60
70 80 90
112. Program to find multiplication of two matrices.
1 #include <stdio.h>
2
3 #define MAXROW 10
4 #define MAXCOL 10
5
6
7 /*User Define Function to Read Matrix*/
8 void readMatrix(int m[][MAXCOL],int row,int col)
9 {
10 int i,j;
11 for(i=0;i< row;i++)
12 {
13 for(j=0;j< col;j++)
14 {
15 printf("Enter element [%d,%d] : ",i+1,j+1);
16 scanf("%d",&m[i][j]);
17 }
18 }
19 }
20
21 /*User Define Function to Read Matrix*/
22 void printMatrix(int m[][MAXCOL],int row,int col)
23 {
24 int i,j;
25 for(i=0;i< row;i++)
26 {
27 for(j=0;j< col;j++)
28 {
29 printf("%d\t",m[i][j]);
30 }
31 printf("\n");
32 }
33 }
34
35 int main()
36 {
37 int a[MAXROW][MAXCOL],b[MAXROW][MAXCOL],result[MAXROW][MAXCOL];
38 int i,j,r1,c1,r2,c2;
39 int sum,k;
40
41
42 printf("Enter number of Rows of matrix a: ");
43 scanf("%d",&r1);
44 printf("Enter number of Cols of matrix a: ");
45 scanf("%d",&c1);
46
47 printf("\nEnter elements of matrix a: \n");
48 readMatrix(a,r1,c1);
49
50
51
52 printf("Enter number of Rows of matrix b: ");
53 scanf("%d",&r2);
54 printf("Enter number of Cols of matrix b: ");
55 scanf("%d",&c2);
56
57 printf("\nEnter elements of matrix b: \n");
58 readMatrix(b,r2,c2);
59
60
61 if(r1==c2)
62 {
63 /*Multiplication of two matrices*/
64 for(i=0;i< r1;i++)
65 {
66 for(j=0;j< c1;j++)
67 {
68 sum=0;
69 for(k=0;k< r1;k++)
70 {
71 sum=sum + (a[i][k]*b[k][j]);
72 }
73 result[i][j]=sum;
74 }
75 }
76
77 /*print matrix*/
78 printf("\nMatrix after multiplying elements (result matrix):\n");
79 printMatrix(result,r1,c1);
80
81
82 }
83 else
84 {
85 printf("\nMultiplication can not be done.");
86 }
87
88
89 return 0;
90 }
14 14 14
32 32 32
50 50 50
#include <stdio.h>
#define ROW 3
#define COL 3
int main() {
int matrix[ROW][COL] = {{2,3,4},{4,5,6},{6,7,8}};
return 0;
}
Output
C program to read and print an Employee’s Details using Structure - In this program, we will read
employee’s details like name, employee id and salary using structure and then print the entered values.
#include <stdio.h>
/*structure declaration*/
struct employee{
char name[30];
int empId;
float salary;
};
int main()
{
/*declare structure variable*/
struct employee emp;
Output
Enter details :
Name ?:Mike
ID ?:1120
Salary ?:76543
n this program, we will learn how to declare a structure with different types of variables, declare and
initialization of structure variable? Here we are not reading values from keyboard; we are assigning the
values to the structure members at the time of structure variable declaration.
#include <stdio.h>
/*structure declaration*/
struct employee{
char name[30];
int empId;
float salary;
};
int main()
{
/*declare and initialization of structure variable*/
struct employee emp={"Mike",1120,76909.00f};
Output
Name: Mike
Id: 1120
Salary: 76909.000000
116.C program to demonstrate example of Nested Structure
In this program, we will learn how to declare, initialize Nested Structure (Structure within Structure)?
How to assign values/read values and access the Nested Structure members?
Here, in this example - we will create a structure dateOfBirth which will be declared inside the
structure student.
struct student{
char name[30];
int rollNo;
struct dateOfBirth{
int dd;
int mm;
int yy;
}DOB; /*created structure varoable DOB*/
};
int main()
{
struct student std;
return 0;
}
Output
Name : Mike
RollNo : 101
Date of birth : 14/03/92
117.C program to demonstrate example of structure pointer (structure with pointer)*/
#include <stdio.h>
struct item
{
char itemName[30];
int qty;
float price;
float amount;
};
int main()
{
return 0;
}
Output
Name: Pen
Price: 5.500000
Quantity: 15
Total Amount: 82.500000
118./*C program to demonstrate example of structure pointer
using user define function*/
#include <stdio.h>
struct item
{
char itemName[30];
int qty;
float price;
float amount;
};
}
int main()
{
return 0;
}
Output
Name: Pen
Price: 5.500000
Quantity: 15
Total Amount: 82.500000
119.C program to declare, initialize an UNION, example of UNION
#include <stdio.h>
// union declaration
union pack{
char a;
int b;
double c;
};
int main()
{
// assign value to each member one by one other it will replace last value
p.a='A';
printf("\nValue of a:%c",p.a);
p.b=10;
printf("\nValue of b:%d",p.b);
p.c=12345.6790;
printf("\nValue of c:%f",p.c);
return 0;
}
Output
#include <stdio.h>
struct student
{
char name [30];
int marks[ 5];
int total;
float perc;
};
int main()
{
struct student std;
int i;
printf("Enter marks:\n");
std.total=0;
for(i=0;i< 5;i++){
printf("Marks in subject %d ?: ",i+1);
scanf("%d",&std.marks[i]);
std.total+=std.marks[i];
}
std.perc=(float)((float)std.total/(float)500)*100;
return 0;
}
Output
Name: Mike
Total: 461
Percentage: 92.20
121.C program to add two distances in feet and inches using structure
#include <stdio.h>
struct distance{
int feet;
int inch;
};
void addDistance(struct distance d1,struct distance d2){
struct distance d3;
d3.feet= d1.feet + d2.feet;
d3.inch= d1.inch + d2.inch;
int main()
{
struct distance d1,d2;
printf("Enter first distance in feet & inches:");
scanf("%d%d",&d1.feet, &d1.inch);
Output
#include<stdio.h>
union tagname
{
unsigned int a;
unsigned char s[4];
};
int main()
{
char i; //for loop counter
printf("\n");
return 0;
}
Output
#include <stdio.h>
//function declarations
struct FirstStruct TakeUserInput(void);
void DisplayOutput(struct FirstStruct Input);
int main()
{
//create a structure to get a return from TakeUserInput function
//Now use the DisplayOutput to print the input
DisplayOutput(TakeUserInput());
return 0;
}
//This function returns a structure after storing the user input into it
struct FirstStruct TakeUserInput(void)
{
Output
Enter a number: 10
Enter a number again: 20
30
124.Calculate party expenses using C program
#include <stdio.h>
#define MAX 50 //maximum items entry
//structure definition
typedef struct item_details{
char itemName[30]; //item name
int quantity; //item quantity
float price; //per item price
float totalAmount; //total amount = quantity*price
}item;
int main(){
item thing[MAX]; //structure variable
int i,choice;
int count=0;
float expenses=0.0f;
i=0;
//infinite loop
do{
printf("Enter item details [%2d]:\n",i+1);
printf("Item? ");
fgets(thing[i].itemName,30,stdin);
printf("Price? ");
scanf("%f",&thing[i].price);
printf("Quantity? ");
scanf("%d",&thing[i].quantity);
thing[i].totalAmount=(float)thing[i].quantity*thing[i].price;
expenses += thing[i].totalAmount;
i++; //increase loop counter
count++;//increase record counter
getchar();
}while(choice==1);
Output
#include <stdio.h>
int main()
{
int num; /*declaration of integer variable*/
int *pNum; /*declaration of integer pointer*/
return 0;
}
Output
int main()
{
int num1,num2;
return 0;
}
Output
Since, we cannot change the value of a constant but using pointer we can change it, in this program we
are doing the same. Here, we have an integer constant and changing its value using pointer.
#include <stdio.h>
int main()
{
const int a=10; //declare and assign constant integer
int *p; //declare integer pointer
p=&a; //assign address into pointer p
return 0;
}
Output
return 0;
}
Output
return 0;
}
Output
#include <stdio.h>
struct student{
char name[30];
int roll;
float perc;
};
int main()
{
struct student std; //structure variable
struct student *ptr; //pointer to student structure
return 0;
}
Output
Entered details:
Name:Mike
RollNo: 101
Percentage: 89.70
131.C program to print size of different types of pointer variables.
1 /*C program to print size of different types of pointer variables.*/
2 #include <stdio.h>
4 int main()
5 {
11 return 0;
12 }
2 #include <stdio.h>
4 int main()
5 {
9
10 p1=&a; //assign address of a
12
14
18
20 *p1=200;
21 printf("\nValue of a: %d",*p1);
23 **p2=200;
24 printf("\nValue of a: %d",**p2);
25
26 return 0;
27 }
Value of a: 200
Value of a: 200
133. C program to demonstrate example of array of pointers.
1 /*C program to demonstrate example of array of pointers.*/
2 #include <stdio.h>
3
4 int main()
5 {
7 int a,b,c;
11 int *ptr[3];
12
14 ptr[0]= &a;
15 ptr[1]= &b;
16 ptr[2]= &c;
17
19 a=100;
20 b=200;
21 c=300;
22
25
27 *ptr[0] +=10;
28 *ptr[1] +=10;
29 *ptr[2] +=10;
30 printf("After adding 10\nvalue of a: %d, b: %d, c: %d\n",*ptr[0],*ptr[1],*ptr[2]);
31
32 return 0;
33 }
After adding 10
In this program we will create memory for int, char and float variables at run time using malloc() function
and before exiting the program we will release the memory allocated at run time by using free() function.
4 #include <stdio.h>
5 #include <stdlib.h>
7 int main()
8 {
9 int *iVar;
10 char *cVar;
11 float *fVar;
12
14
15 iVar=(int*)malloc(1*sizeof(int));
16 cVar=(char*)malloc(1*sizeof(char));
17 fVar=(float*)malloc(1*sizeof(float));
18
20 scanf("%d",iVar);
21
23 scanf(" %c",cVar);
24
26 scanf("%f",fVar);
27
29
31 free(iVar);
32 free(cVar);
33 free(fVar);
34
35 return 0;
36 }
In this program we will create memory for text string at run time using malloc() function, text string will
be inputted by the user and displayed. Using free() function we will release the occupied memory.
4 #include <stdio.h>
5 #include <stdlib.h>
7 int main()
8 {
9 int n;
10 char *text;
11
13 scanf("%d",&n);
14
16 text=(char*)malloc(n*sizeof(char));
17
20 gets(text);
21
22 printf("Inputted text is: %s\n",text);
23
24 /*Free Memory*/
25 free(text);
26
27 return 0;
28 }
3) C program to read a one dimensional array, print sum of all elements along with inputted array
elements using Dynamic Memory Allocation.
In this program we will allocate memory for one dimensional array and print the array elements along
with sum of all elements. Memory will be allocated in this program using malloc() and released
using free().
5 #include <stdio.h>
6 #include <stdlib.h>
8 int main()
9 {
10 int *arr;
11 int limit,i;
12 int sum=0;
13
15 scanf("%d",&limit);
16
18 arr=(int*)malloc(limit*sizeof(int));
19
20 if(arr==NULL)
21 {
23 return 0;
24 }
25
26 printf("Enter %d elements:\n",limit);
28 {
30 scanf("%d",(arr+i));
31 /*calculate sum*/
32 sum=sum + *(arr+i);
33 }
34
38
39
41
42 return 0;
43 }
4) C program to read and print the student details using structure and Dynamic Memory Allocation.
In this program we will create a structure with student details and print the inputted details. Memory to
store and print structure will be allocated at run time by using malloc() and released by free().
4 #include <stdio.h>
5 #include <stdlib.h>
7 /*structure declaration*/
8 struct student
9 {
10 char name[30];
11 int roll;
12 float perc;
13 };
14
15 int main()
16 {
18
21
22 if(pstd==NULL)
23 {
25 return 0;
26 }
27
30 gets(pstd->name);
32 scanf("%d",&pstd->roll);
34 scanf("%f",&pstd->perc);
35
38
39 return 0;
40 }
5) C program to read and print the N student details using structure and Dynamic Memory Allocation.
In this program we will create a structure with N number of student details and print the inputted details.
Memory to store and print structure will be allocated at run time by using malloc() and released by free().
4 #include <stdio.h>
5 #include <stdlib.h>
7 /*structure declaration*/
8 struct student
9 {
10 char name[30];
11 int roll;
12 float perc;
13 };
14
15 int main()
16 {
18 int n,i;
19
21 scanf("%d",&n);
22
25
26 if(pstd==NULL)
27 {
29 return 0;
30 }
31
34 {
40 scanf("%d",&(pstd+i)->roll);
42 scanf("%f",&(pstd+i)->perc);
43 }
44
47 {
49 }
50
51 return 0;
52 }
#include <stdio.h>
#include <string.h>
int main()
{
//Declare Variables
char string[10][30]; //2D array for storing strings
int i, n;
Output
#include <stdio.h>
int main()
{
unsigned char text[]="1234";
int intValue;
intValue=((text[0]-0x30)*1000)+((text[1]-0x30)*100)+((text[2]-0x30)*10)+((text[3]-0x30));
printf("Integer value is: %d",intValue);
}
Output
#include <stdio.h>
#include <stdlib.h>
int main()
{
unsigned char text[]="1234";
int intValue;
intValue=atoi(text);
printf("Integer value is: %d",intValue);
return 0;
}
Output
#include <stdio.h>
int main()
{
unsigned char text[]="1234";
int intValue;
sscanf(text,"%04d",&intValue);
printf("Integer value is: %d",intValue);
return 0;
}
Output