Coding Practice

Write a C program to split a number and then add all factorial digit.

Split a number and then add all factorial digit

3. Take 3 digit number from user. Split the digits of the number using a user defined function splitter(). Find the factorial of each digit using a user defined function find_factorial(). Finally, print the sum of the factorials using anther user defined function calculate_sum().

Sample Output
Input Number: 123
Output : 9


Process returned 0 (0x0)   execution time : 1.911 s
Press any key to continue.
Source Code
#include<stdio.h>
 
int arr[100], factorialSum = 0;
 
int calculate_sum(int n)
{
    factorialSum = factorialSum + n;
}
 
int find_factorial(int f)
{
    int fact = 1;
 
    for(int i = 1; i <= f; i++)
    {
        fact = fact * i;
    }
    calculate_sum(fact);
}
 
int splitter(int number)
{
    int i = 0, digit = 0;
    while(number != 0)
    {
        arr[i++] = number % 10;
        number = number/10;
        digit++;
    }
    return digit;
}
 
int main()
{
    int number, digit;
 
    printf("Input Number: ");
    scanf("%d", &number);
 
    digit = splitter(number);
 
    for(int i = 0; i < digit; i++)
    {
        find_factorial(arr[i]);
    }
 
    printf("Output : %d\n\n", factorialSum);
    return 0;
}
Sample Output
Input Number: 125
Output : 123


Process returned 0 (0x0)   execution time : 5.352 s
Press any key to continue.

Write a C Program to check twin string or not

Twin String

2. Two strings s1 and s2 will be called “Twin String” if the following conditions are matched: If one of the strings have p distinct vowels, the other will contain the missing 5-p vowels from the vowel set, v = {a, e, i, o, u}

Both of them will be of same length.

Sample Input

Output

S1: national

S2: umbrella

Twin String

S1: education

S2: notorious

Twin String

S1: bangladesh

S2: pakistan

Not Twin String

Sample Output
S1: national
S2: umbrella

Twin String


Process returned 0 (0x0)   execution time : 23.699 s
Press any key to continue.
Source Code
#include<stdio.h>
 
int main()
{
    int i, j, k = 0, length1, length2, vowel = 0, a = 0, e = 0, iV = 0, o = 0, u = 0;
    char s1[30], s2[30];
 
    printf("S1: ");
    gets(s1);
 
    printf("S2: ");
    gets(s2);
 
    for(i = 0; s1[i] != '\0'; i++);
    length1 = i;
 
    for(i = 0; s2[i] != '\0'; i++);
    length2 = i;
 
    if(length1 == length2)
    {
        for(i = 0; i < length1; i++)
        {
            if(s1[i] == 'a' && a == 0)
            {
                a = 1;
                vowel++;
            }
            else if(s1[i] == 'e' && e == 0)
            {
                e = 1;
                vowel++;
            }
            else if(s1[i] == 'i' && iV == 0)
            {
                iV = 1;
                vowel++;
            }
            else if(s1[i] == 'o' && o == 0)
            {
                o = 1;
                vowel++;
            }
            else if(s1[i] == 'u' && u == 0)
            {
                u = 1;
                vowel++;
            }
            if(s2[i] == 'a' && a == 0)
            {
                a = 1;
                vowel++;
            }
            else if(s2[i] == 'e' && e == 0)
            {
                e = 1;
                vowel++;
            }
            else if(s2[i] == 'i' && iV == 0)
            {
                iV = 1;
                vowel++;
            }
            else if(s2[i] == 'o' && o == 0)
            {
                o = 1;
                vowel++;
            }
            else if(s2[i] == 'u' && u == 0)
            {
                u = 1;
                vowel++;
            }
        }
    }
 
    if(vowel == 5)
    {
        printf("\nTwin String");
    }
    else
    {
        printf("\nNot Twin String");
    }
    printf("\n\n");
    
    return 0;
}
Sample Output-1
S1: bangladesh
S2: pakistan

Not Twin String


Process returned 0 (0x0)   execution time : 11.287 s
Press any key to continue.
Sample Output-2
S1: abaca
S2: acada

Not Twin String


Process returned 0 (0x0)   execution time : 10.481 s
Press any key to continue.

Find how many numbers in a array are greater than the average and then reverse the arrays numbers

1. Take n numbers in an array. Do the following operations
a. Find how many numbers in the array are greater than the average value of the array elements.
b. Reverse the array without using any library function.

Sample Input

Output

Array element: 4

10 20 30 40

Numbers greater than average value: 2

Reversed array: 40 30 20 10

Array element: 5

1 2 3  4 65

Numbers greater than average value: 1

Reversed array: 65 4 3 2 1

Sample Output
Array element: 4
10
20
30
40

Numbers greater than average value: 2
Reversed array: 40 30 20 10


Process returned 0 (0x0)   execution time : 15.815 s
Press any key to continue.
Source Code
#include<stdio.h>

int main()
{
    int i, size, arr[50], sum = 0, avg, count = 0;

    printf("Array element: ");
    scanf("%d", &size);

    for(i = 0; i < size; i++)
    {
        scanf("%d", &arr[i]);
        sum = sum + arr[i];
    }

    avg = sum / size;

    for(i = 0; i < size; i++)
    {
        if(avg > arr[i])
        {
            count++;
        }
    }
    printf("\nNumbers greater than average value: %d\n", count);

    printf("Reversed array: ");
    for(i = size - 1; i >= 0; i--)
    {
        printf("%d ", arr[i]);
    }
    printf("\n\n");

    return 0;
}
Sample Output
Array element: 5
1
2
3
4
65

Numbers greater than average value: 4
Reversed array: 65 4 3 2 1


Process returned 0 (0x0)   execution time : 15.311 s
Press any key to continue.

Complex Variables and Quadratic Equations

 1.

(a) Find the modulus, argument and express following complex number in Euler form:

            (i) 𝒛 = 𝟏 − √𝟑𝒊

            (ii) z = ((1+i)/(1−i ))7

(b) Find poles and order of the function 𝑓(𝑧) = 1/(z3-5z2+8z-4)

(c) Sketch and describe the region of the following inequalities:

             (i) |𝒛 − 𝟏 − 𝒊| ≤ 𝟐

             (ii) 𝟐 < |𝒛 − 𝟑| < 4

Rubrics for Question-1:

 

Marks

Level

Descriptions

5

Excellent

Gives a complete response with a clear, coherent and suitable explanation including strong arguments; identifies all the important elements of the problem with proper examples

4

Very Good

Gives a complete response including strong arguments; identifies all the important elements of the problem without examples.

3

Good

Completes the problem but the explanation may be muddled; argumentation may be incomplete; may not include examples.

2

Average

Completes the problem with some minor computational errors, may include wrong examples.

1

Poor

Description is not understandable; may make major computational errors, include wrong examples.

2.

(a) Find the center and radius of the circle |𝑧 + 1 − 2𝑖| = √π

(b) By using De Moivre’s theorem, compute all roots of the complex function 𝒛𝟏𝟐 = (−√𝟑 − 𝒊).

(c) Prove that |𝑧 + 4𝑖| + |𝑧 − 4𝑖| = 10 represents an ellipse

Rubrics for Question-2:

 

Marks

Level

Descriptions

5

Excellent

Gives clear explanations with appropriate diagrams (if necessary); identifies all the important elements of the problem

4

Very Good

Gives clear explanations without appropriate diagrams; understands the underlying mathematical ideas shortly

3

Good

Completes the problem but the explanation may be muddled; diagram may be inappropriate or unclear, understands the underlying mathematical ideas shortly

2

Average

Completes the problem with some minor computational errors and mathematical ideas is not clearly stated.

1

Poor

Unable to indicate which information is appropriate to the problem.

3.

(a) Verify that the Cauchy-Riemann equations are satisfied for the function 𝒇(𝒛) = 𝒆^𝒙+𝒊y

(b) An unbiased coin is tossed 20 times. Find the probability of

             (i) Just 5 heads

             (ii) At least one head or at best two head

(c) Find residues of the function 𝑓(𝑧) = (z + 22)/(z - 3)^3

Rubrics for Question-3:

Marks

Level

Descriptions

5

Excellent

A high level of mathematical thinking which includes exceptional skills and appropriate mathematical tools and techniques in the resolution of problems in task(s).

4

Very Good

Apply excepted methods and mathematical tools and techniques in the resolution of problems in task(s) with minor computational errors.

3

Good

Illustrates the essential elements but some ideas are missing, a limited variety of tools and techniques used to resolve the situation presented in the task(s).

2

Average

Demonstrates the essential elements with limited variety of

tools and techniques. Lack of mathematical concepts leads to wrong outputs.

1

Poor

Contains    irrelevant    responses    that    have    no    valid relationship to the task(s). Lack of mathematical concepts and unsuccessful attempt to justify results.

4.

(a) By using Cauchy’s integral formula, evaluate ∮ 𝒛^𝟐𝒅𝒛/(𝒛 − 𝒛^2)(z + i) , where C is the circle |𝒛| = 𝟐 described in the positive sense.

(b) Evaluate the contour integral ∫𝟐𝝅0 𝟏d𝜽(𝟓 − 𝟒𝐜𝐨𝐬 𝜽).

(c) Determine whether the function 𝑢 = 𝑒^𝑥 (sin 𝑥 + cos 𝑦) is harmonic or not.

Rubrics for Question-4: 

Marks

Level

Descriptions

5

Excellent

A high level of mathematical thinking which includes exceptional skills and appropriate mathematical tools and techniques in the resolution of problems in task(s).

4

Very Good

Apply excepted methods and mathematical tools and techniques in the resolution of problems in task(s) with minor computational errors.

3

Good

Illustrates the essential elements but some ideas are missing, a limited variety of tools and techniques used to resolve the situation presented in the task(s).

2

Average

Demonstrates the essential elements with limited variety of

tools and techniques. Lack of mathematical concepts leads to wrong outputs.

1

Poor

Contains    irrelevant    responses    that    have    no    valid

relationship to the task(s). Lack of mathematical concepts and unsuccessful attempt to justify results.


Write a C Program to check if a Substring is present in the given String

Check String to Substring

3. Write a C Program to check if a Substring is present in the given String.

Sample Input Sample Output
String: Bangladesh
Substring: lad
Match found
String: Paragraph
Substring: rag
Match found
String: America
Substring: rice
Not Match found

Sample Output
String: Bangladesh
Substring: lad

Match found


Process returned 0 (0x0)   execution time : 11.043 s
Press any key to continue.
Source Code
#include<stdio.h>
#include<string.h>
 
int main()
{
    int i, j, temp, count = 0, count2 = 0;
    char str[50], subString[20];
 
    printf("String: ");
    gets(str);
 
    printf("Substring: ");
    gets(subString);
 
    while (str[count] != '\0')
    {
        count++;
    }
    while (subString[count2] != '\0')
    {
        count2++;
    }
 
    for (i = 0; i <= count - count2; i++)
    {
        for (j = i; j < i + count2; j++)
        {
            temp = 1;
            if (str[j] != subString[j - i])
            {
                temp = 0;
                break;
            }
        }
        if (temp == 1)
            break;
    }
    if (temp == 1)
    {
        printf("\nMatch found\n\n");
    }
    else
    {
        printf("\nNo match found\n\n");
    }
    return 0;
}
Sample Output
String: America
Substring: rice

No match found


Process returned 0 (0x0)   execution time : 12.346 s
Press any key to continue.

You're given all numbers between 1, 2, 3, 4 … n except one. Your task is to find the missing number

Find the Missing Number

2. You're given all numbers between 1, 2, 3, 4 … n except one. Your task is to find the missing number.

Input:
First input line contains an integer n. The second line contains (n−1) numbers. Every number is distinct and between 1 and n (inclusive).

Output:
Print the missing number.

Sample Input Sample Output
5
2 4 3 1
5
10
2 10 7 9 3 6 4 8 1
5
Sample Output
10
2 10 7 9 3 6 4 8 1
Missing Number is: 5


Process returned 0 (0x0)   execution time : 20.268 s
Press any key to continue.
Source Code
#include<stdio.h>
 
int main()
{
    int i, j, sum, arrSum = 0, n;
 
    scanf("%d", &n);
    int numbers[n];
 
    for(i = 0; i < n-1; i++)
    {
        scanf("%d", &numbers[i]);
    }
 
    sum = (n * (n+1))/2;
 
    for(i = 0; i < n-1; i++)
    {
        arrSum = arrSum + numbers[i];
    }
    printf("Missing Number is: %d\n\n", sum - arrSum);
 
    return 0;
}
Sample Output
5
2 4 3 1
Missing Number is: 5


Process returned 0 (0x0)   execution time : 16.864 s
Press any key to continue.

Write a C Program to check the minimum number of turns required

Check Minimum Number of Turns Required

1. You’re given an array of n integers. You want to modify the array so that it is increasing, i.e., each component is at least as large as the previous component. On every turn, you may increase the worth of any component by one. What's the minimum number of turns required?

Input: The 1st input line contains number n: the size (limit) of the array. Then, the 2nd line contains n integers (the contents of the array).

Output: Print the minimum number of turns.

Sample Input Sample Output
5
3 2 5 1 7
5 turns
4
1 2 2 6
0 turns

Explanation: Total input numbers = 5
Turns = 3 - 2 = 1 turns
Turns = 2 - 5 = 0+1 turns
Turns = 5 - 1 = 4+0+1 turns
Turns = 1 - 7 = 0+4+0+1 turns
So, Total Turns = 5

Sample Output
5
3 2 5 1 7
5 turns


Process returned 0 (0x0)   execution time : 10.196 s
Press any key to continue.
Source Code
#include<stdio.h>
 
int main()
{
    int i, size, turn = 0;
    scanf("%d", &size);
    int numbers[size];
 
    for(i = 0; i < size; i++)
    {
        scanf("%d", &numbers[i]);
    }
 
    for(i = 1; i < size; i++)
    {
        if(numbers[i-1] > numbers[i])
        {
            turn += (numbers[i-1] - numbers[i]);
            //Or, you can also write this line. Both are same.
            //turn = turn + (numbers[i-1] - numbers[i]);
        }
    }
    printf("%d turns\n\n", turn);
 
    return 0;
}
Sample Output
4
1 2 2 6
0 turns


Process returned 0 (0x0)   execution time : 4.518 s
Press any key to continue.

Write a C program to generate the following pattern

Generate Pattern

4. Write a C program to generate the following pattern

Sample Output
Enter number of rows
9

9       9
 8     8
  7   7
   6 6
    5
   4 4
  3   3
 2     2
1       1

Process returned 0 (0x0)   execution time : 1.879 s
Press any key to continue.
Source Code
#include<stdio.h>
 
int main()
{
    int i, j, k, rows;
 
    printf("Enter number of rows\n");
    scanf("%d", &rows);
    printf("\n");
 
    for(i = rows, k = 1; i >= 1; i--, k++)
    {
        for(j = rows; j >= 1; j--)
        {
            if(i == j || j == k)
            {
                printf("%d", i);
            }
            else
            {
                printf(" ");
            }
        }
        printf("\n");
    }
    return 0;
}
Sample Output
Enter number of rows
5

5   5
 4 4
  3
 2 2
1   1

Process returned 0 (0x0)   execution time : 1.845 s
Press any key to continue.
Change Theme
X