Вы находитесь на странице: 1из 28

C Programs

1 C Program to find the given number is even or odd

Source File: C0001

4 C program to print the first n prime numbers.

5 C program to swap two integer numbers( Try to find at least 8 different logic’s)

15 C program to find the biggest and smallest among three numbers

16 C program to print Hello world without using semicolon (;) anywhere in the program.

17 C program to print semicolon without using semicolon anywhere in the program.

18 C Program to print whether the given number is a power of 2 or not (without using loops).

20 C Program to find the given number is strong number or not.

1
21 C Program to print all the strong numbers in given range.

23 C program to find the given number is a self prime or not.

24 C Program to print the all the self prime numbers in given range.

30 Write a C Program to find the given string is palindrome or not. **

31 Write a C Program to find the power of given number without power Function.

32 Write a C Program to Add Two Integer numbers without using '+' symbol

33 Write a C Program to Subtract two integer numbers without using '-' Symbol

36 Print a given Matrix in a spiral form

Ex:
I/P: 1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
Output: 1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10

37 Given an array, Print the greatest element on right side, for elements having no greater element
print -1
Ex: For the input array [4,3,2,5,25,1]
Next grater element follows:

4 ---> 5

2
3 ---> 5
2 ---> 5
5 ---> 25
25 ---> -1
1 ---> 2

38 Given sorted array of integers, rearrange the array alternatively, I.e first element should be
maximum value, second minimum value, third 2nd max, fourth 2nd min and so on
Input: 1,2,3,4,5,6,7
Out Put: 7,1,6,2,5,3,4

39 Given an array of 0's and 1's . Return the index of of a zero turning which will produce a
longest continuous 1's
Ex: 1 0 1 0 1 1 0 1 1 1 1 1 0 1
Here turning the last 0 to 1 will form 5 ones. This is the longest of all

40 Given an array of integers write a function that returns true if there is a triple (a,b,c) that
satisfies a2+b2=c2
Ex: a[]={3,1,4,6,5}
True
a[]={10,4,6,12,5}
False

41 Finding sub string present in the string or not using string functions and without using string
functions

42 Your given a two integer numbers a and b you have to find the last digit of ab

43 Number of squares in a n*n

44 Check the given date is valid or not. Read the date from user
Need to take care of leap year

45 Write a program to evaluate the expression in the form following

123+546
9874-62
58-87
458/854
120%15

3
46 There will be multiple input sets. Input for each set will consist of two lines. The first line will
contain an integer in the range 2...20 indicating the number of columns used. The next line is a
string of up to 200 lower case letters. The last input set is followed by a line containing a single
0, indicating end of input.

5
Out Put:
toioy
hpknn
eleai
rahsg
econh
semot
nlewx

48 Write a C program to remove the duplicate values in an array

49 Write a function to return the length of a string without using in-build functions

50 Reverse every word in a string

Input: this is c program.
Output: sith si c margorp

51 Write a function that returns the angle between the hour hand and the minute hand of a clock.

52 Write a function that takes a string consisting of numerical characters and returns all possible
alpha characters of a string of the same length as input that correspond to the keypad of a
typical telephone.

53 Switch every pair of a word in a string

Input: ab cd ef gh
Output: cd ab gh ef

54 Reverse the order of words in a string

Input: ab cd ef gh

4
Output: gh ef cd ab

55 Write a function that compares two strings and returns third string containing only the letters
which are not present in both the strings

57 Write a function to zero all duplicate values in an integer array

58 Write a function that compares two strings and returns third string containing only the letters
that appear in both (union need to take care of positions)

59 For array of size n, print all pairs that added to K( Sorted and unsorted arrays)

60 Write a function to reverse a string

61 Suppose you have an array of 1001 integers. The integers are in random order, but you know
each of the integers is between 1 to 1000. In addition each number appear only once in the
array, except one number which occurs twice. Assumes that you can access element of array
only once. Describe the algorithm to find the repeated number.

62 Multiple by 8 without using multiplication or addition.

63 Implement a function that take an array and returns one with the unique elements in itEx: (1, 1,
3, 3, 3, 5, 5, 5, 9, 9, 9, 9) -> (1, 3, 5, 9).

66 Write a C program to print the fibonacci numbers in given range

67 Given 3 sorted arrays A[],B[],C[]. Find the three numbers array such that A[i]+B[j]=C[K]

68 Your given an array of both positive and negative integers and required to find the subarray
with largest sum.

N & N-1

70 Write a function to multiply with 7

N=(N<<3)-N

71 Given two strings S1 and S2. Delete from S2 all those characters which occurs in S1. And also
create clean S2.

5
72 Write a function which reads the sentence from user and converts all lower case to upper case
letters.

73 Write a function which reads the sentence from user and make first letter of every word to
upper case

75 Write a program to find the 2's complement of a number

76 Write a program to find the next prime number after a given number

77 Given a string with spaces like “Hello World” remove all the spaces. There can be multiple
spaces in the string. This should be done without using extra string or array

78 Given a string and two indexes I and J remove all the characters from I to J both included from
the string.
Ex: Hello, 2,3
output: heo

79 Given a string S1 and S2 and position I, replace string S1 from position I with string S2.
Ex: Hello, UM 2
Output: HUMlo

80 Sort a given number (of binary format) in descending order.

Ex: read the decimal number and convert into its equivalent binary then sort it
Binary: 001011011
sorted binary: 111110000

83 Write a program to remove all digits in a given string or sentence

84 Write a program to expand the string as follows. Repeat each character and expand the string
Ex: string and number
Input: abc 2
output: aabbcc

85 Write a program to expand the string as follows. Repeat whole string two times one after
another.
Ex: string and number
Input: abc 2

6
Output: abcabc

86 Given a string as input , convert the string to encoded string where a=1,b=2,..z=26

87 Write a program to print the prime numbers in given range in reverse order and skip every
alternative prime number.
Example: print all prime numbers between 0-15
output: 13 7 3 1

88 Write a c program to convert base 2 string to base 4 string.

89 Write a program to add and multiplication of two matrices. Read the rank of matrix from user.

90 Write a C program to convert the currency into text.

Ex:
Input: Rs 11,59,475
Output: Eleven lakhs fifty nine thousand four hundred seventy five rupees

91 Write a program to delete all vowels from given sentence. OR Write a program delete all
consonants from given string.

93 Write a program to count number of words, lines in a given sentence.

94 Write a program to swap two strings and two arrays. Need to take care of length of those.

95 Write a program to find LCM and HCF/GCD of two numbers. Try to optimize the code.

96 Write a program to convert the following to among them self. There are 16 combinations.
Binary
Decimal
Octal

99 Write a program to find the inverse and transport of a matrix

100 Write a program to find the largest and smallest number in an array

101 Write a program to find the sum of unique numbers in an array.

Ex: consider an array of numbers 2,4,6,9,2,7,8,4 as input so the output will be

7
30(6+9+7+8)

102 Given two numbers as input and the function should return count of the numbers
between them (including them) which are divisible by smaller number.
example: consider 3 and 25 as input then it should return 8 (3,6,9,12,15,18,21,24)
TIP: Answer should be optimal

103 Given a number say 213, consider all the possible combinations of the number such as
123,132,213,231,312,321 and arrange them in the dictionary (sorted order). write a function
MIN_RANK that returns the index of the given number here it is 3.
Note: Rotate and reverse OR inserting each letter in array by %

104 Given a string,find out which character appears more in the string.(if there are multiple possible
answers print the alphabet with less ASCII value)
Example:
Input: ababcdefffghijkl
Output: f

105 Given two numbers separated by comma in string form,write a function to find out which
number is bigger
Example:
Input:1334,456
Output: 1334

106 Rotate a given array number of times.

Note:Don't use extra array
Example:
Input:2 3 4 5 6
2
Output: 5 6 2 3 4

107 Given a string,count the frequency of a given word in the string

Example:
Input:I am the best of the best
best
Output: 2

108 Given an unsorted integer array of length N,Find the maximum negative chain array.
Example:
Input:1 -2 3 6 -4 -2 -1 14 2 15
Output:-4 -2 -1(3 element chain is largest in this case)

8
109

110

111

1 Implementation of Single Linked List

i. Insertion
ii. Deletion
iii. Display
iv. Search
v. Reverse Linked List
vi. Find middel node of Linked List
Source File: DS0001

2 Implement Stack using Linked List

i. Push
ii. Pop
iii. Display
Source File: DS0002

3 Implement Queue using Linked List

i. Insert
ii. Delete
iii. Display
Source File: DS0003

4 Implementation of Double Linked List

i. Insertion
ii. Deletion
iii. Display
iv. Search
v. Reverse Linked List
vi. find middle node of Linked List
Source File: DS0004

9
5 Implementation of Bubble sort
Source File: DS0005

6 Implementation of Insertion sort

Source File: DS0006

7 Implementation of Selection sort

Source File: DS0007

8 Implementation of Merge sort

Source File: DS0008

i. Insertion
ii. Deletion
iii. Display

i. Insertion
ii. Deletion
iii. Display

18 Merge two sorted linked lists

19 Print linked list in reverse order without reversing single and double linked list

20 Swap adjacent nodes in single and double linked list

21 Swap even and odd nodes in single and double linked list

22 Remove kth Node of the linked list from ending. Don't calculate the length of linked list.

10
23 Remove kth Node from linked list. Do not calculate the length of linked list.

24 Write a program to sort a single and double linked list

25 Write a program to convert a number into linked list. It should be single linked list.
Ex: Input: 1234
Output: 1->2->3->4

26

Write a C Program to print the following read the 'N' value from user

1) 2)

1 1

1 2 1 1

1 2 3 1 1 1

1 2 3 4 1 1 1 1

1 2 3 4 5 1 1 1 1 1

3) Floyd's Triangle 4)

1
1
2 2
2 3
3 3 3
4 5 6
4 4 4 4
7 8 9 10
5 5 5 5 5
11 12 13 14 15

11
5) 1 6) 1

1 2 1 1

1 2 3 1 1 1

1 2 3 4 1 1 1 1

1 2 3 4 5 1 1 1 1 1

7) 1 8) 1

2 3 2 2

4 5 6 3 3 3

7 8 9 10 4 4 4 4

11 12 13 14 15 5 5 5 5 5

9) 10)

1 1

1 2 1 1

1 2 3 1 1 1

1 2 3 4 1 1 1 1

1 2 3 4 5 1 1 1 1 1

11) 12)

1 1

2 3 2 2

4 5 6 3 3 3

7 8 9 10 4 4 4 4

11 12 13 14 15 5 5 5 5 5

12
13) 14)

1 2 3 4 5 1 1 1 1 1

1 2 3 4 1 1 1 1

1 2 3 1 1 1

1 2 1 1

1 1

15) 16)

11 12 13 14 15 15 14 13 12 11

7 8 9 10 10 9 8 7

4 5 6 6 5 4

2 3 3 2

1 1

17) 18)

5 5 5 5 5 1 2 3 4 5

4 4 4 4 1 2 3 4

3 3 3 1 2 3

2 2 1 2

1 1

19) 20)

1 1 1 1 1 11 12 13 14 15

1 1 1 1 7 8 9 10

1 1 1 4 5 6

1 1 2 3

1 1

13
21) 22)

15 14 13 12 11 5 5 5 5 5

10 9 8 7 4 4 4 4

6 5 4 3 3 3

3 2 2 2

1 1

23) 24)

1 2 3 4 5 1 1 1 1 1

1 2 3 4 1 1 1 1

1 2 3 1 1 1

1 2 1 1

1 1

25) 26)

11 12 13 14 15 15 14 13 12 11

7 8 9 10 10 9 8 7

4 5 6 6 5 4

2 3 3 2

1 1

27) 28) This is inverse of pascal's triangle

5 5 5 5 5 1 4 6 4 1

4 4 4 4 1 3 3 1

3 3 3 1 2 1

2 2 1 1

1 1

14
29) 30)

1 1

1 2 1 1

1 2 3 1 1 1

1 2 3 4 1 1 1 1

1 2 3 4 5 1 1 1 1 1

1 2 3 4 1 1 1 1

1 2 3 1 1 1

1 2 1 1

1 1

31) 32)

1 1

2 3 3 2

4 5 6 6 5 4

7 8 9 10 10 9 8 7

11 12 13 14 15 15 14 13 12 11

7 8 9 10 10 9 8 7

4 5 6 6 5 4

2 3 3 2

1 1

15
33) 34)

1 1

2 2 1 2

3 3 3 1 2 3

4 4 4 4 1 2 3 4

5 5 5 5 5 1 2 3 4 5

4 4 4 4 1 2 3 4

3 3 3 1 2 3

2 2 1 2

1 1

35) 36)

1 1

1 1 2 3

1 1 1 4 5 6

1 1 1 1 7 8 9 10

1 1 1 1 1 11 12 13 14 15

1 1 1 1 7 8 9 10

1 1 1 4 5 6

1 1 2 3

1 1

16
37) 1 38) 1

3 2 2 2

6 5 4 3 3 3

10 9 8 7 4 4 4 4

15 14 13 12 11 5 5 5 5 5

10 9 8 7 4 4 4 4

6 5 4 3 3 3

3 2 2 2

1 1

39) 1 40) 1

1 2 1 1

1 2 3 1 1 1

1 2 3 4 1 1 1 1

1 2 3 1 1 1

1 2 1 1

1 1

41) 1 42) 1

2 3 2 2

4 5 6 3 3 3

7 8 9 10 4 4 4 4

11 12 13 14 15 5 5 5 5 5

7 8 9 10 4 4 4 4

4 5 6 3 3 3

2 3 2 2

1 1

17
43)Read the string from User 44)Read the string from User

H H

H E H E

H E L H E L

H E L L H E L L

H E L L O H E L L O

45)Read the string from User 46)Read the string from User

H E L L O H E L L O

H E L L H E L L

H E L H E L

H E H E

H H

47)Read the string from User 48)Read the string from User

H H E

H E H E L

H E L H E L L

H E L L H E L L O

H E L L O H E L L

H E L L H E L

H E L H E

H E H

18
49)Read the string from User 50)Read the string from User

H H E L L O

H E H E L L

H E L H E L

H E L L H E

H E L L O H

H E

H E L

H E L L

H E L L O

H E L L

H E L

H E

19
C Program logics

S.No C Program logics using standard GCC Compiller

1 int main()
{
int x = 20;
{
int x = 10;
printf ("%d\n", x);
}
printf ("%d", x);
}

Output: 10 20

int main()
{
int x ;
{
int x = 110;
printf ("%d\n", x);
}
printf ("%d", x);
}

Output: 10 & 38 (garbage Value )

2 int main()
{
int x = 5;
x = x++;
printf ("%d", x);
}
Out Put: 5

20
3 int main()
{
int x = 10;
printf ("%d %d", ++x, ++x);
}
Out Put: 12 12

4 int main()
{
char s1[] = "Hagrid";
char s2[] = "Hagrid";
int a=1,b=1;
float c,d;
double e;
e=10.5;
c=d=10.5;
if (s1==s2)
printf("\s1==s2");
else
printf ("\s1!=s2");
if(0)
printf("\n0");
if(1)
printf("\n1");
if(-1)
printf("\n-1");
if("Hagrid"=="Hagrid")
printf("\nSame");
if(1==1)
printf("\n1==1");
if(a==b)
printf("\na==b");
if(10.5==10.5)
printf("\n10.5==10.5");
if(c==d)
printf("\nc==d");
if(e==d)
printf("\ndouble e== float d");
else
printf("\ndouble e!= float e");

21
}

Output:
s1!=s2
1
-1
same
1==1
a==b
10.5=10.5
c==d
double e= floot d

5 # define VOLDEMORT he_who_must_not_be_named

int main()
{
printf ("VOLDEMORT");
}
Output: VOLDEMORT

6 int main()
{
int a = -3, b = 2, c= 0, d;
d = ++a && ++b || ++c;
printf ("a = %d, b = %d, c = %d, d = %d", a, b, c, d);
}

Output: a=-2,b=3,c=0,d=1

7 #include<stdio.h>
int main()
{
int a=10,b;
b=a++ + ++a;
printf("%d,%d,%d,%d",b,a++,a,++a);
}
Output: 12 13 14 14

22
8 #include<stdio.h>
int main()
{
int a=15,b=6;
a=(a+b)-(b=a);
printf("%d\t%d",b,a);
}
output: 15 6

9 int main()
{
static int ivar = 5;
printf("%d",ivar--);
if(ivar)
main();
}
Output: 5 4 3 2 1

10 int main()
{
extern int iExtern;
iExtern = 20;
printf(“%d”,iExtern);
}

11 #define clrscr() 100

int main()
{
clrscr();
printf("%d\n\t", clrscr());
}
output: 100

12 int main()
{
void *vpointer;
char cHar = 'g', *cHarpointer = "GOOGLE";
int j = 40;
vpointer = &cHar;

23
printf("%c",*(char*)vpointer);
vpointer = &j;
printf("%d",*(int *)vpointer);
vpointer = cHarpointer;
printf("%s",(char*)vpointer +3);
}
Output: G40gle

13 #define FALSE -1
#define TRUE 1
#define NULL 0
main() {
if(NULL)
puts("NULL");
else if(FALSE)
puts("TRUE");
else
puts("FALSE");
}
Output: TRUE

14 main()
{
int i =5,j= 6;
printf("%d\t%d\t%d",i+++j,i,j);
}
Output: 11 6 6

15 int main()
{
int i =0;
while(+(+i--)!= 0)
i-=i++;
printf("%d",i);
}
Output: -1

16 int main()
{
int i =3;
for(;i++=0;)

24
printf("%d",i);
}

Output: Compilation error - Lvalue required

17 int main()
{
int i = 10, j =20;
j = i ,j?(i,j)?i :j:j;
printf("%d\t%d",i,j);
}
output :
10 10

18 int main()
{
printf("%s",a);
}
Output: Goog

19 int main()
{
int I =10, j=2;
int *ip = &I ,*jp =&j;
int k = *ip/ *jp;
printf("%d",k);
}

Output: 5

int main()
{
int I =10, j=2;
int *ip = &I ,*jp =&j;
int k = *ip/*jp;
printf("%d",k);
}

Output: Error

25
20 int main()
{
char *str ="yahoo";
char *ptr =str;
char least =127;
while(*ptr++)
{
least = *ptr;
printf("\n%d",least);
}
}

Output:
97
101
111
111
0

21 int main()
{
int i =300;
char *ptr = &i;
*++ptr=2;
printf("%d",i);
}

Output: 556
How: You initially set 'i' to 300, which in memory is 2c01. You assign the memory address of 'i' to
your pointer 'ptr'. Then increment the pointer 'ptr' by 1 and assign 2 to the next memory address.
The memory address for 'i' then contains 2c02.

300 is 0x12c
556 is 0x22c

22 int main()
{
int i =257;
int *iptr =&i;
printf("%d%d",*((char*)iptr),*((char *)iptr+1));
}

26
Output: 1 1
How: The integer value 257 is stored in the memory as, 00000001 , so the individual bytes
are taken by casting it to char * and get printed.

23 #include<stdio.h>
int main()
{
int k=4,j=0;
switch(k)
{
case 3:
j=300;
case 4:
j=400;
case 5:
j=500;
}
printf ("%d\n",j);
}
OutPut: 500

24 main()
{
int c[ ]={2.8,3.4,4,6.7,5};
int j,*p=c,*q=c;
for(j=0;j<5;j++)
{
printf(" %d ",*c);
++q;
}
for(j=0;j<5;j++)
{
printf(" %d ",*p);
++p;
}
}
output: 2 2 2 2 2 2 3 4 7 5

27
How: Initially c is assigned to both the pointers. In the first loop since only q is incremented
but not c, the value 2 will be printed 5 times, In second loop p itself is incremented so the
values 2 3 4 6 5 will be print.

25 main()
{
int i=-1,j=-1,k=0,l=2,m;
m=i++&&j++&&k++||l++;
printf("%d %d %d %d %d",i,j,k,l,m);
}
Output: 0 0 1 3 1

How: Logical operations always gives a result of 1 or 0. and also logical and (&&) operator has
higher priority over the logical OR(||) operator. So the expression i++ && j++ && k++ is
evaluated first. The result of this expression is 0( -1 && -1 && 0 = 0). Now the expression 0 ||
2 which evaluates to 1. As or operator always gives 1 except for 0 || 0.

26

27

28

29

28