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

C programming

Focus academy for career enhancement


Question 1

Write a code to find the count of odd digits in the


given number.

Example
Input : 250617
Output : 3

Focus academy for career enhancement


LOGIC

• Get a number
• If it is not 0, separate last digit and increment
the count
• Then remove the last digit and calculate
• display count

Focus academy for career enhancement


#include <stdio.h>
void main()
{
int nodd = 0,neven = 0, num = 250617, digit;

while (num> 0)
{
digit = num % 10;
if (digit % 2 == 1)
nodd++;
else neven++;
num /= 10;
}
printf("%d“, nodd);
}
Focus academy for career enhancement
Question 2
Write a code to check given number is
palindrome or not. Palindrome number is a
number that is equal to it mirror image.

Example
Input: 28582 Output: Yes
Input: 28128 Output: No

Focus academy for career enhancement


LOGIC
• Get the number from user
• Hold the number in temporary variable
• Reverse the number
• Compare the temporary number with reversed
number
• If both numbers are same, print palindrome number
• Else print not palindrome number

Focus academy for career enhancement


#include <stdio.h>
int main()
{
int rev = 0, rem, num = 28582;
int original = num;
if ( original == rev)
printf(“Yes”);
while( num>0 )
else
{
printf(“No”);
rem = num%10;
rev = rev*10 + rem;
return 0;
num /= 10;
}
}

Focus academy for career enhancement


Question 3

Write a code to find the factorial of the given


number.

Example
Input: 5
Output: 120

Focus academy for career enhancement


LOGIC

• Get the number


• If it is >=1, then multiply the element by the
previous number recursively
• Print the final answer

Focus academy for career enhancement


#include <stdio.h>
int main()
{
int n, factorial = 1;
printf("Enter an integer: ");
scanf("%d", &n);
for(i=1; i<=n; ++i)
{
factorial *= i; // factorial = factorial*i;
}
printf("%d”, factorial);
}
return 0;
}

Focus academy for career enhancement


Question 4

Get the nth number in Fibonacci series.


Example:
Input:
9
Output:
0, 1, 1, 2, 3, 5, 8, 13, 21

Focus academy for career enhancement


#include<stdio.h>
int fib(int n)
{
if (n <= 1)
return n;
return fib(n-1) + fib(n-2);
}

int main ()
{
int n = 9;
printf("%d", fib(n));
return 0;
}

Focus academy for career enhancement


Question 5

Write a program to print the following pattern.


Input: 4
Output:

Focus academy for career enhancement


#include <stdio.h>
int main()
{
int i, j, N;
printf("Enter number of rows: "); // Input number of rows from user
scanf("%d", &N);
for(i=1; i<=N; i++) // Iterate over each row
{
for(j=1; j<=N; j++) //Iterate over each column
{
if(i==1 || i==N || j==1 || j==N)
{
printf("* "); // Print star for 1st, Nth row and column
}
else
{ printf(“ "); }
}
printf("\n"); // Move to the next line/row
}
return 0;
}

Focus academy for career enhancement


Question 6

Write a program to find the second largest


number from the given 3 numbers
Example:
Input: 127 35 69
Output: 69

Focus academy for career enhancement


#include <stdio.h>
int main()
{
int n1, n2, n3;

printf("Enter three different numbers: ");


scanf("%d %d %d", &n1, &n2, &n3);

if( n1>=n2 && n1>=n3 )


printf("%d is the largest number.", n1);

if( n2>=n1 && n2>=n3 )


printf("%d is the largest number.", n2);

if( n3>=n1 && n3>=n2 )


printf("%d is the largest number.", n3);

return 0;
}

Focus academy for career enhancement


#include<stdio.h>

void main()
{
int a,b,c;

printf(“Enter any three numbers:”);


scanf(“%d %d %d”,&a, &b, &c);

if(a>b&&a>c)
if(b>c)
printf(“%d is the second largest no.”, b); if(c>a&&c>b)
else if(b>a)
printf(“%d is the second largest no.”, c); printf(“second largest no. is %d”, b);
else
if(b>a&&b>c) printf(“second largest no. is %d”, a);
if(a>c)
printf(“the second largest no. is % d”, a); }
else
printf(” the second largest no. is %d”, c);

Focus academy for career enhancement


Question 7

Write a program to count vowels, consonants


digits and spaces.
Example:
Input: 2 apples are red
Output: Vowels: 5
Consonants: 7
White spaces: 3
Digits: 1

Focus academy for career enhancement


#include <stdio.h>
int main()
{
char line[150];
int i, vowels, consonants, digits, spaces; vowels = consonants = digits = spaces =
0;

printf("Enter a line of string: ");


scanf("%[^\n]", line);

for(i=0; line[i]!='\0'; ++i)


{
if(line[i]=='a' || line[i]=='e' || line[i]=='i' || line[i]=='o' || line[i]=='u' ||
line[i]=='A' || line[i]=='E' || line[i]=='I' || line[i]=='O' || line[i]=='U')
{
++vowels;
}

Focus academy for career enhancement


else if((line[i]>='a'&& line[i]<='z') || (line[i]>='A'&& line[i]<='Z'))
{
++consonants;
}
else if(line[i]>='0' && line[i]<='9')
{
++digits;
}
else if (line[i]==' ')
{
++spaces;
}
}
printf("Vowels: %d“, vowels);
printf("\n Consonants: %d", consonants);
printf("\n Digits: %d", digits);
printf("\n White spaces: %d", spaces);
return 0;
}

Focus academy for career enhancement


Question 8

Write a code for addition of 2 complex number


using structure.
Example:
Input : 7 + i2
2 + i3
Output : 9 + i5

Focus academy for career enhancement


#include<stdio.h>
struct comp{
int real;
int imag;
};
struct comp comp1,comp2;
struct comp sum_complex(struct comp complex1,struct comp complex2){
struct comp temp;
temp.real = complex1.real + complex2.real;
temp.imag = complex1.imag + complex2.imag;
return temp;
}
int main(){
struct comp result;
printf("Enter Complex Number 1: ");
scanf("%d%d",&comp1.real, &comp1.imag);
printf("Enter Complex Number 2: ");
scanf("%d%d",&comp2.real,&comp2.imag);
result = sum_complex(comp1,comp2);
printf("The sum is %d + i%d\n\n", result.real,result.imag);
return 0;
}
Focus academy for career enhancement
Question 9 - Logic Pyramid
Identify the logic behind the series
6 28 66 120 190 276....

The numbers in the series should be used to create a Pyramid. The base of
the Pyramid will be the widest and will start converging towards the top
where there will only be one element. Each successive layer will have one
number less than that on the layer below it. The width of the Pyramid is
specified by an input parameter N. In other words there will be N numbers on
the bottom layer of the pyramid.

The Pyramid construction rules are as follows


1. First number in the series should be at the top of the Pyramid
2. Last N number of the series should be on the bottom-most layer of the
Pyramid, with Nth number being the right-most number of this layer.
3. Numbers less than 5-digits must be padded with zeroes to maintain the
sanctity of a Pyramid when printed. Have a look at the examples below to get
a pictorial understanding of what this rule actually means.

Focus academy for career enhancement


Sample Input & Output

Input Output

00006
2 00028 00066

00006
3 00028 00066
00120 00190 00276

Given Series : 6 28 66 120 190 276....

Focus academy for career enhancement


Find the logic in a given number series

Create a expression to implement this series


i.e: k * (2k – 1)

K’s initial value is 2 & incremented by 2 in


each iteration

Use printf to print this series in a specified


pyramid format

Focus academy for career enhancement


#include <stdio.h>
int main(void)
{
Input:
int i, n, j, k=2;
2
scanf("%d", &n);
for(i=1; i<=n; i++) Output:
00006
{ 00028 00066
for(j=0;j<i;j++)
{
printf("%10.5d",(k*(2*k-1))); //to print in pyramid form
k=k+2;
}
printf("\n");
}
return 0; }
Focus academy for career enhancement
Question 10

Write a code to print the length of the string


without using string handling function.
Example:
Input : apple
Output : 5

Focus academy for career enhancement


#include <stdio.h>

void main()
{
char string[50];
int i, length = 0;

printf("Enter a string \n");


gets(string);
/* keep going through each character of the string till its end */
for (i = 0; string[i] != '\0'; i++)
{
length++;
}
printf("The length of a string is the number of characters in it \n");
printf("So, the length of %s = %d\n", string, length);
}
Focus academy for career enhancement
Question 11

Write a program to print pyramid Sequence.


Example:
Input : 3
Output:

1
222
33333
Focus academy for career enhancement
#include <stdio.h>
int main()
{
int row, c, n, temp;
scanf("%d",&n); // enter the number of rows
temp = n;
for ( row = 1 ; row <= n ; row++ )
{
for ( c = 1 ; c < temp ; c++ )
{
printf(" ");
}
temp--;
for ( c = 1 ; c <= 2*row - 1 ; c++ )
{
printf(“%d “, row);
}
printf("\n");
} return 0; }

Focus academy for career enhancement


Question 14

Write a program to find the common terms of


the given two matrices.

Focus academy for career enhancement


Question 15

Write a program to find the difference between


the largest and the smallest elements in the
given matrix.

Focus academy for career enhancement


Question 16

Write a code to print the number of occurrences


of a given character in the given statement.
Example:
Input : o
Nothing is impossible in the world.
Output: 3.

Focus academy for career enhancement


Qn.17 :
Find whether the given numbers are strong
numbers or not

145 = 1!+4!+5!

Input : 145
Output : Yes

Input : 544
Output : No

Focus academy for career enhancement


1) Initialize sum of factorials as 0.
2) For every digit d, do following
a) Add d! to sum of factorials.
3) If sum factorials is same as given
number, return true.
4) Else return false.

Focus academy for career enhancement


Qn.18:
Write a C Program to remove all Characters
in Second String which are present in First
String

• Input :
First string : Hello Second string : el
Output : Ho

Focus academy for career enhancement


1) Initialize:
res_ind = 0
ip_ind = 0

2) We can use Boolean array to know only if character is present


in second string.
count[‘e’] = 1
count[‘l’] = 1

3) Process each character of the input string and if count of that


character is 0 then only add the character to the resultant
string.
str = “Ho”

4) Put a ‘\0′ at the end of the string.


Focus academy for career enhancement
Qn.19:
Write a C program to remove the duplicate
element in an array

Input :
Enter array size : 5
Enter 5 array element : 11 13 11 12 13
Original array is : 11 13 11 12 13
Output :
New array is : 11 13 12

Focus academy for career enhancement


• Read elements in an array, say arr
• To find duplicate elements in a given array we need two loops. Run
an outer loop from 0 to N(where N is size of the array). This loop is
used to select each element of array and check next subsequent
elements for duplicates using another nested loop.
• Run another inner loop to find first duplicate of current element
i.e. arr[i]. Run an inner loop from i + 1 to N.
• Inside the inner loop check for duplicate element. If a duplicate
element is found then remove the current duplicate element. Also
if a duplicate element is found the decrement size of the array i.e. N
= N - 1.

Focus academy for career enhancement


Qn.20:
Write a C program to print string in reverse order
without using string library

Input :
Enter a string: This is a test string
Output :
Reversed String is: gnirts tset a si sihT

Focus academy for career enhancement


1) Read the string , including spaces(until
newline).
2) Find the length
3) Point an index to the end of the string
4) Copy character by character, till the pointing
index reaches to start of the string.
5) Insert ‘\0’ to the end of the string.
6) Print it.

Focus academy for career enhancement


Qn.21:
Write a C program to check if an array contains
2 elements that sum up to a given value.

Input :
arr[] = {11, 15, 6, 8, 9, 10}, x = 16
6 + 10 = 16.
Output :
True

Focus academy for career enhancement


1) Read the array, arr. Read value, sum.
2) Take two pointers i, j.
3) Require two loops. . Run an outer loop loop
from 0 to N(where N is size of the array). This
loop is used to select each element of array
and check next subsequent elements to check
whether the pair’s sum is equal to the value
required.
4) Run another inner loop to find first pair.Run an
inner loop from i + 1 to N.

Focus academy for career enhancement


Qn.22: Write a C program to check
if given strings are anagram
Anagram Words :

LISTEN – SILENT

INTEGRAL - TRIANGLE

Focus academy for career enhancement


1) Take two strings as input and store them in
the arrays array1[] and array2[] respectively.
2) In the function find_anagram() using while
statement sort both the arrays. After sorting
compare them using for loop.
3) If all the strings are equal then the two
strings are anagrams, otherwise they are
not anagrams.

Focus academy for career enhancement


Qn.23:
Write a C Program to check 1634 is an
Armstrong or not.
• A positive integer is called an Armstrong number
of order n if
• abcd... = an + bn + cn + dn + ...
• 1634 = 14 + 64 + 34 + 44

Input :
1634
Output :
Yes

Focus academy for career enhancement


1) Read the number, num.
2) Store it into a temporary variable,temp.
3) Extract each digit , then multiply it to itself 4
times, add it to a variable sum.
4) Repeat step 3 till num>0.
5) Check whether temp and sum are same. If
yes, print Yes else print No.

Focus academy for career enhancement


Qn.24: Sort Elements in
Input:
Lexicographical Order
C
C++
Java
PHP
Python
Perl
Ruby
R
JavaScript
php

Output
C
C++
Java
JavaScript
PHP
Perl
Python
R
Ruby
php

Focus academy for career enhancement


Qn.25: Find the Frequency of
Characters
Input:
This language is awesome
e

Output:
3

Focus academy for career enhancement


Qn.26: Sum of Contiguous Subarray within one
dimensional Array of Numbers which has the
Largest Sum
Input:
6
1
3
-8
9
8
7

Output:
24
3 5

Focus academy for career enhancement


Qn.27: Decimal to Binary System
using Recursion
Input:
10

Output:
1010

Focus academy for career enhancement

Вам также может понравиться