• (Part 4 of this series of Q-A is here: 16 marks, Q-A, structures in C, Unit 4. )
    16 marks Q-A on loops (Unit 1), Nov/Dec 2022, Anna Univ, CS 3251 (Programming in C)
    Pls see the images below.


    Anna University Past Years Exam Q & A

    CS3251 : Programming in C

    Nov / Dec 2022, 16 Marks, Unit 1

    Explain the looping statement in C with suitable examples.


    • Loops help us to easily execute a block of code repeatedly.
    • The code is repeatedly executed as long as the loop condition is true.
    • Loops help to make the program shorter, easier to read, and easier to maintain.

    There are 3 loops in C:

    • for
    • while
    • do-while

    for loop:
    • Initialization, checking of condition, and updation is written compactly in a single line.
    // Prints numbers from 1 to 5.
    #include <stdio.h>
    int main()
    {
    int i;
    for (i = 1; i <= 5; i++)
    {
    printf("%d\n", i);
    }
    return 0;
    }
    output:
    1
    2
    3
    4
    5

    while loop
    • Condition is checked before executing the statements within the loop body.
    // Prints numbers from 1 to 5.
    #include <stdio.h>
    int main()
    {
    int i = 1;
    while (i <= 5)
    {
    printf("%d\n", i);
    i++;
    }
    return 0;
    }
    output:
    1
    2
    3
    4
    5

    do-while loop
    • condition is checked after executing the statements within the loop body.
    • The loop body necessarily gets executed at least once.
    // Prints numbers from 1 to 5.
    #include <stdio.h>
    int main()
    {
    int i = 1;
    do
    {
    printf("%d\n", i);
    i++;
    } while (i <= 5);
    return 0;
    }
    output:
    1
    2
    3
    4
    5

    (Next part of this series is here: Part 6, Functions (Unit 3), 16 marks Q&A)

  • सर्वे भवन्तु सुखिनः
    सर्वे सन्तु निरामयाः
    सर्वे भद्राणि पश्यन्तु
    मा कश्चिद् दुःखभाग्भवेत

    Sarve bhavantu sukhinaḥ
    Sarve santu nirāmayāḥ
    Sarve bhadrāṇi paśyantu
    Mā kaścid duḥkha-bhāg bhavet

    May all be happy!
    May all be free from disease!
    May all behold auspiciousness!
    May not sorrow come upon anyone!

  • Anna University exam prep
    CS 3251 – Programming in C
    Past years Questions and Answers – Part 4. (See Part 3 here: 2 marks Q-A on Unit 1. )
    Here, 16 marks Q-A on struct (Unit 4) from Nov/Dec 2022. Pls see the images below.


    CS3251 – Programming in C
    16 Marks – Nov / Dec 2022, Unit 4, topic – structures in C
    Question

    What is a structure? Create a structure with data members of various types and declare two structure variables. Write a program to read data into these and print the same. Justify the need for structured data type.


    Answer

    A struct is a user-defined data type in C.
    It groups together variables of different data types under a single name.

    #include <stdio.h>
    // Student data struct
    struct Student
    {
    int rollNum; // Stores roll number
    char name[20]; // Stores student name
    char grade; // Stores grade
    };
    int main()
    {
    struct Student s1, s2; // Two variables of type struct Student
    // Reading data into the struct variables
    // Details of 1st student
    printf("Enter details for 1st student:\n");
    printf("Enter roll number: ");
    scanf("%d", &s1.rollNum);
    printf("Enter name: ");
    scanf("%s", s1.name);
    printf("Enter grade: ");
    scanf(" %c", &s1.grade);
    // Details of 2nd student
    printf("Enter details for 2nd student:\n");
    printf("Enter roll number: ");
    scanf("%d", &s2.rollNum);
    printf("Enter name: ");
    scanf("%s", s2.name);
    printf("Enter grade: ");
    scanf(" %c", &s2.grade);
    // Displaying the data from the struct variables
    printf("\nStudent 1 details: \n");
    printf("Roll number: %d\n", s1.rollNum);
    printf("Name: %s\n", s1.name);
    printf("Grade: %c\n", s1.grade);
    printf("\nStudent 2 details: \n");
    printf("Roll number: %d\n", s2.rollNum);
    printf("Name: %s\n", s2.name);
    printf("Grade: %c\n", s2.grade);
    // End of program
    return 0;
    }

    output:
    Student 1 details:
    Roll number: 12
    Name: Suresh
    Grade: A
    Student 2 details:
    Roll number: 23
    Name: Ganesh
    Grade: B

    Need for Structured Data Type
    • Groups related data together under a single name. (Student’s name, roll number, and grade can be stored in a single variable.)
    • Represents real-world entities (which contain attributes of different types). Examples: student, employee, book, etc.
    • Makes programs organized and makes records easier to handle.

    Tip:
    Note the blank space before %c in the scanf() statement for reading the grade:
    scanf(” %c”, &s1.grade);

    The blank space before %c is necessary. Otherwise, we will get erratic behaviour.
    Why so?

    • There is another scanf() statement before this one to read a string (name).
    • A newline character (\n) gets entered for the string scanf() call.
    • Then there is another scanf() call for reading the grade. If we don’t put a blank space before %c in the scanf() call, the newline character (\n) left in the input buffer will be read instead of the actual grade.

    Part 5 of this series on Anna Univ Q-A is here: Loops in C, Unit 1, 16 marks, Nov/Dec 2022.

  • Today we shall learn about functions. It is a very simple and very useful concept. Pls see the images below.


    Function is just a name that we give to a block of code.
    And whenever we want to execute that code, instead of writing the entire code, we can simply call the function by its name, and the code will get executed.

    Ler’s see an example:

    #include <stdio.h>
    int main()
    {
    int a = 10, b = 5;
    printf("Sum = %d\n", a + b); // display sum
    printf("Thanks\n");
    printf("Have a nice day\n"); // display thank you message
    printf("Difference = %d\n", a - b); // display difference
    printf("Thanks\n");
    printf("Have a nice day\n"); // display thank you message
    printf("Product = %d\n", a * b); // display product
    printf("Thanks\n");
    printf("Have a nice day\n"); // display thank you message
    return 0;
    }
    output:
    Sum = 15
    Thanks
    Have a nice day
    Difference = 5
    Thanks
    Have a nice day
    Product = 50
    Thanks
    Have a nice day

    After each arithmetic computation, we are printing thank you messages. The code for that is repeated multiple times in our program.

    printf("Thanks\n");
    printf("Have a nice day\n");
    ...
    printf("Thanks\n");
    printf("Have a nice day\n");
    ...
    printf("Thanks\n");
    printf("Have a nice day\n");

    Same code repeated multiple times!


    Wouldn’t it be nice if we could write the code for that just once, give it some name or label, and whenever we want, we can call it by its name?

    No need to write the same code again and again!


    void ThankYouMessage()
    {
    printf("Thanks\n");
    printf("Have a nice day\n");
    }

    What we have written above is known as function definition.
    We’re defining a function containing the code we saw earlier.
    We’ve given the function the name ThankYouMessage(). (We can give any appropriate name that we like.)
    The function doesn’t return anything. Hence, ‘void’ is written as the return type of the function.

    When we call ThankYouMessage();, it will print “Thanks” and “Have a nice day”.


    Program without function calls (same code is repeated multiple times)

    #include <stdio.h>
    int main()
    {
    int a = 10, b = 5;
    printf("Sum = %d\n", a + b);
    printf("Thanks\n");
    printf("Have a nice day\n");
    printf("Difference = %d\n", a - b);
    printf("Thanks\n");
    printf("Have a nice day\n");
    printf("Product = %d\n", a * b);
    printf("Thanks\n");
    printf("Have a nice day\n");
    return 0;
    }

    Same program, written using function calls. (Neater, and code is not repeated multiple times)

    #include <stdio.h>
    void ThankYouMessage()
    {
    printf("Thanks\n");
    printf("Have a nice day\n");
    }
    int main()
    {
    int a = 10, b = 5;
    printf("Sum = %d\n", a + b);
    ThankYouMessage();
    printf("Difference = %d\n", a - b);
    ThankYouMessage();
    printf("Product = %d\n", a * b);
    ThankYouMessage();
    return 0;
    }

    output:
    Sum = 15
    Thanks
    Have a nice day
    Difference = 5
    Thanks
    Have a nice day
    Product = 50
    Thanks
    Have a nice day

    Both programs produce the same output.


    This is function definition.
    void ThankYouMessage()
    {
    printf("Thanks\n");
    printf("Have a nice day\n");
    }

    Just because we defined a function doesn’t mean that the code within it gets executed.
    In order to execute that code, we need to call the function by its name.
    We’ve done that in lines 14, 17 and 20 of the above program.

    Whenever we call a function, the code within the function definition gets executed.

    How to call a function?
    Write the function name, followed by parentheses, followed by semicolon.
    That’s the way to call a function.
    For example, ThankYouMessage(); —> This is a function call.


    Another example of a program using functions:
    Program with a greet function.

    #include <stdio.h>
    void greet() // function definition
    {
    printf("Good morning\n");
    printf("Jai Sriman Narayana\n");
    }
    int main()
    {
    greet(); // function call
    greet(); // another function call
    printf("Thank you\n");
    return 0;
    }
    output:
    Good morning
    Jai Sriman Narayana
    Good morning
    Jai Sriman Narayana
    Thank you
    notes:
    • The above program has a greet function.
    • The name of the function is greet().
    • It is defined at the beginning of the program before main().
    • It is called two times within main().
    • Note: A function is defined once, but can be called multiple times.
    • Each time that the function is called, the computer runs the code written within the function definition.

    Summary:

    • Function is just a name that we give to a block of code.
    • Whenever we want to run that code, we simply call the function by its name.
    • A function is defined once, and can be called many times as we want.
    • Functions help to make the program neater, easier to understand, and easier to maintain.
  • Recursion lesson – continued.
    (For Part 2 of this series, pls see here: Recursion Tutorial Part 2.)
    In this lesson, we’ll use recursion to write a function to compute the n-th term of the Fibonacci series. Pls see the images below.

    Pls see the above images. The same content is given in text format below.

    • We’ll see how we can break down the problem into smaller problems of the same type. That gives us the recursive formula.
    • And, in order to ensure that the recursion eventually stops, we need to figure out an appropriate base case.

    Q. What if we forget to have a base case?
    Answer.
    The solution will not work. The recursive computations will never. A base case is needed in order to terminate the recursion.

    Q. Once we have the recursive formula and the base case, how do we translate that into C code?
    Answer.

    • We will define a recursive function. We’ll give it an appropriate name which indicates what it does.
    • In the function definition, we’ll code the base case.
    • We’ll also code the recursive formula. (We’ll call the same function on a smaller input.)
    • In the main() function, we’ll call the recursive function on the appropriate input.

    Fibonacci series

    Fibonacci series is sequence of numbers where each term is the sum of the previous two terms.
    For instance, the 10th term of the Fibonacci series is the sum of the 9th term of the series and the 8th term of the series.
    Similarly, the 9th term of the series is the sum of the 8th term and the 7th term.

    The first two terms of the series are fixed as 0 and 1.
    That is, the 1st term of the Fibonacci series is 0. And the 2nd term of the Fibonacci series is 1.

    • Let Fib(n) denote the n-th term of the Fibonacci series.
    • Fib(1) is 0, and Fib(2) is 1.
    • Fib(3) is Fib(1) + Fib(2), which is 1 + 0, which is 1.
    • Fib(4) is Fib(3) + Fib(2), which is 1 + 1, which is 2.
    • Fib(5) is Fib(4) + Fib(3), which is 2 + 1, which is 3.
    • Fib(6) is Fib(5) + Fib(4), which is 3 + 2, which is 5.
    • Fib(7) is Fib(6) + Fib(5), which is 5 + 3, which is 8.
    • And so on, we can compute each Fibonacci term as the sum of the previous two terms.

    Q. Let’s say we want to find Fib(20). How will we proceed to write a program for it?
    Answer.
    We can see that Fib(n) is Fib(n-1) + Fib(n-2). This gives us the recursive formula.
    And we know that, Fib(1) is 0, and Fib(2) is 1. These give us our base cases.

    Using the recursive formula and base cases, we can easily define a recursive function. That will enable us to compute any Fibonacci term we want.


    //Program to find n-th term of Fibonacci series, recursively
    #include <stdio.h>
    int Fib(int n) //recursive function; returns n-th term of Fib series
    {
    if (n==1)
    return 0; //base case: 1st term of Fib series is 0.
    if (n==2)
    return 1; //another base case: 2nd term of Fib series is 1.
    return Fib(n-1) + Fib(n-2); //recursive formula.
    }
    int main()
    {
    int n; //which Fib term to find
    printf("Pls enter which Fibonacci term you want to find: ");
    scanf("%d", &n);
    printf("Term %d of Fib series is %d.", n, Fib(n)); //call the function.
    return 0;
    }
    Sample output:
    Pls enter which Fibonacci term you want to find: 4
    Term 4 of Fib series is 2.
    Sample output:
    Pls enter which Fibonacci term you want to find: 8
    Term 8 of Fib series is 13.

    Q. Why do we need two base cases? What if we just have one base case: Fib(1) is 0.
    Answer.
    The solution will not work. The recursive function will never terminate.
    For instance, let’s assume there’s a call to Fib(3). It will in turn call Fib(2) and Fib(1).
    Fib(2) will in turn call Fib(1) and Fib(0).
    Now, Fib(1) is known to us through the base case, but Fib(0) is not.
    Fib(0) will in turn call Fib(-1) and Fib(-2), and so on, this will keep on going.
    So, in order to ensure that the recursion terminates, we need two base cases.


    Recap: what we learnt in this lesson-

    • We saw how recursion can be used to compute the nth term of the Fibonacci series.
    • We also saw that there can multiple base cases, and not just one.

    (For Part 1 of the tutorial on recursion in C, pls see here: Recursion tutorial – Part 1)
    (For posts related to C Programming, pls see here: C Programming.)

  • (For part 1 of this tutorial on recursion, pls click here: Recursion – Part 1.)
    In this lesson, we shall look at a few more simple problems on recursion, and their solutions.
    Problems covered in this lesson:

    • Find sum of numbers from 1 to n.
    • Find n! (n factorial)
    • Find 2^n (2 raised to the power n)
    • Find a^n (a raised to the power n)


    What is recursion?
    Recursion is the technique of breaking down a problem into similar problems of smaller size, and then again breaking down those smaller problems into still more smaller problems, and so on. That is, we solve a problem by expressing it in terms of a smaller problem of the same type.

    Two things to keep in mind in designing recursive solutions –

    • What is the recursive formula? (How can we express the problem in terms of a smaller problem of the same type?)
    • What is the base case? (When should the recursion end? What is that small problem for which we directly know the answer?)
    Q.1. Compute the sum 1 + 2 + … + n, recursively.

    Solution.
    Let Sum(n) denote the sum n + (n-1) + … + 1.

    • Recursive formula: Sum(n) is n + Sum(n-1).
    • Base case: Sum(1) is 1.
    // Program to compute 1 + 2 + ... + n, recursively.
    #include <stdio.h>
    int Sum(int n) //recursive function to find n + (n-1) + ... + 1
    {
    if (n == 1) //base case
    return 1;
    return n + Sum(n - 1); //recursive formula
    }
    int main()
    {
    int m;
    printf("How many terms in the series? ");
    scanf("%d", &m); //get input from user.
    printf("The desired sum is %d", Sum(m)); //call the function
    return 0;
    }
    Sample output:
    How many terms in the series? 4
    The desired sum is 10

    Q.2. Compute n! (n factorial) recursively.

    Solution.

    • Recursive formula: n! is n x (n-1)!
    • Base case: 1! is 1.
    //Program to compute n! recursively.
    #include <stdio.h>
    int factorial(int n) //compute n! recursively
    {
    if (n == 1) //base case
    return 1; //1! is 1.
    return n * factorial(n - 1); //recursive formula; n! is n x (n-1)!
    }
    int main()
    {
    int m;
    printf("What factorial do you want to find? ");
    scanf("%d", &m);
    printf("%d! is %d", m, factorial(m)); //display the result.
    return 0;
    }
    Sample output:
    What factorial do you want to find? 5
    5! is 120

    Q.3. Compute 2^n recursively.

    Solution.

    • Recursive formula: 2^n is 2 x 2^(n-1).
    • Base case: 2^1 is 2.
    //Program to find 2^n using recursion.
    #include <stdio.h>
    int powerOf2(int n) //compute 2^n recursively.
    {
    if (n == 1) //base case
    return 2; //2^1 is 2.
    return 2 * powerOf2(n - 1); //recursive formula; 2^n is 2 x 2^(n-1).
    }
    int main()
    {
    int n;
    printf("What power of 2 do you want to find? ");
    scanf("%d", &n);
    printf("2^%d is %d", n, powerOf2(n));
    return 0;
    }
    sample output:
    What power of 2 do you want to find? 4
    2^4 is 16

    Q.4. Compute a^n recursively.

    Solution.

    • Recursive formula: a^n is a x a^(n-1).
    • Base case: a^1 is a.
    #include <stdio.h>
    int power(int a, int n) //computes a^n recursively
    {
    if (n == 1)
    return a; //base case. a^1 is a.
    return a * power(a, n - 1); //recursive formula. a^n is a x a^(n-1).
    }
    int main()
    {
    int a, n; //a is the base, and n is the exponent.
    printf("Enter the base: ");
    scanf("%d", &a);
    printf("Enter the exponent: ");
    scanf("%d", &n);
    printf("%d^%d is %d", a, n, power(a, n)); //call the function.
    return 0;
    }
    sample output:
    Enter the base: 3
    Enter the exponent: 4
    3^4 is 81

    In this lesson, we saw a few problems on recursion. identified the recursive formula and base case for those problems, and translated that into C code.
    (For the next part of this tutorial on recursion, pls see here: Recursion – Part 3.)
    (For more on C Programming, you can click here: Posts on C Programming)

  • This lesson is an introduction to recursion. (See here for more posts on C Programming.)


    In this lesson, we shall get introduced to recursion in C. Pls see the above images for learning through illustrative images.
    Recursion is a nice technique, which helps in solving complex problems using just few lines of code. Just as loops allow us to solve complex-looking problems using just few lines of code, recursion also accomplishes the same.

    In recursion, we express the problem in terms of smaller subproblems of the same type. And then, in turn, the smaller subproblems are expressed in terms of still more smaller subproblems of the same type. By solving the small subproblems, the big problem gets solved.

    A recursive function is a function which calls itself.


    Two things to keep in mind when designing recursive function –
    What is the recursive formula? (That is, how to express the problem in terms of smaller subproblems of the same type?)
    – What is the base case? (That is, when should the recursion terminate? For what case do we directly know the answer?)

    Example. Design a recursive function to compute 5 + 4 + 3 + 2 + 1.

    Solution.
    We can see that 5 + 4 + 3 + 2 + 1 is the same as 5 + (4 + 3 + 2 + 1).
    Again, 4 + 3 + 2 + 1 is the same as 4 + (3 + 2 + 1).

    (We’re trying to break down the problem to be solved into smaller subproblems of the same type.)

    Let Sum(n) denote the sum n + (n-1) + (n-2) + … + 1.
    So, Sum(5) is 5 + 4 + 3 + 2 + 1. Sum(4) is 4 + 3 + 2 + 1. And so on.

    We can write: Sum(5) is 5 + Sum(4).
    Similarly, we have that: Sum(4) is 4 + Sum(3).
    Similarly, Sum(3) is 3 + Sum(2).
    Similarly, Sum(2) is 2 + Sum(1),
    Sum(1) is simply directly 1.

    In general, we have that: Sum(n) is equal to n + Sum(n-1).
    This is the recursive formula. (We’ve expressed the problem (Sum(n)) in terms of a smaller subproblem of the same type (Sum(n-1)).

    And, Sum(1) is 1.
    This is the base case.

    Let’s try to code this logic:

    #include <stdio.h>
    int AddSeries(int n) //recursive function to find n + (n-1) + ... + 1
    {
    return n + AddSeries(n-1);
    }
    int main()
    {
    int answer = AddSeries(3);
    printf("The sum is %d. \n", answer);
    return 0;
    }

    The above recursive function has a big problem – The function execution will never terminate.
    AddSeries(3) will call AddSeries(2), which will in turn call AddSeries(1), which will call AddSeries(0), which will call AddSeries(-1), and so on.
    Solution – In the function body, we need to add a base case, to ensure that the recursion stops at some point.
    As we already saw earlier, a base case is that AddSeries(1) is 1.

    Here’s the modified correct code:

    #include <stdio.h>
    int AddSeries(int n) //recursive function to find n + (n-1) + ... + 1
    {
    if (n == 1) //base case
    return 1;
    return n + AddSeries(n-1); //recursive formula
    }
    int main()
    {
    int answer = AddSeries(3); //call the function with argument as 3.
    printf("The sum is %d. \n", answer);
    return 0;
    }
    output:
    The sum is 6.

    Explanation of the code: (How does the program work?)
    – In Line 13, AddSeries() function is called with argument 3. So, we now execute the AddSeries() code with value of n as 3.
    – The condition in Line 5 is false (3 is not equal to 1). Hence, we skip Line 6, and go to Line 8. In Line 8, there is a call to AddSeries(2). So, now the AddSeries() function code again starts getting executed. This time argument is 2.
    – The condition in Line 5 is false.. (2 is not equal to 1.) We go to Line 8.. Now, there is a call to AddSeries(1). So, now the AddSeries() code again starts getting executed, this time with argument 1.
    – The condition in Line 5 is true. Therefore, AddSeries(1) returns the value 1 (Line 6).
    – AddSeries(1) was called in Line 8 of the AddSeries(2) execution. So, we go there. AddSeries(2) returns 2 + 1, which is 3.
    – AddSeries(2) was called by Line 8 of AddSeries(3). So, now we go back there. So, now AddSeries(3) returns 3 + AddSeries(2), which is 3 + 3, which is 6.
    – AddSeries(3) was called in Line 13 of the program. AddSeries(3) returns the value 6. This is stored in the variable answer, which is then displayed in Line 14. This completes the program.

    Summary:

    • Recursion is a technique in which we solve a problem by expressing it in terms of smaller subproblems of the same type.
    • A recursive function is a function which calls itself.
    • Two things to keep in mind when designing recursive solutions –
      – What is the recursive formula? (That is, how to express the problem in terms of smaller subproblems of the same type?)
      – What is the base case? (That is, when should the recursion stop? For what case do we directly know the answer?)

    (For the next part of this tutorial on recursion, pls see here: Recursion – Part 2.)



  • We shall look at using for loop to find the sum of different number series.
    (For the previous lesson, click here: for loop tutorial – Part 2)


    Q.1. Design a loop to display the number series: 3, 7, 11, 15, 19, 23.

    Solution.
    We’ll make the loop counter start at 3, and have it go till 23. We’ll make it go in steps of 4.
    Each time, within the loop, we’ll simply print the loop counter value.

    for (int i = 3; i <= 23; i += 4) //loop goes from 3 to 23, in steps of 4.
    {
    printf("%d\n", i); //print the value of i
    }
    output:
    3
    7
    11
    15
    19
    23

    Q.2. Design a for loop to compute the sum: 3 + 7 + 11 + 15 + 19 + 23.

    Solution.
    This is very similar to the above problem.
    We’ll have a sum variable, and initialize it to 0.
    We’ll make the loop counter go from 3 to 23, in steps of 4, just as before.
    Earlier, we were printing the value of the loop counter. In this problem, instead of printing, we’ll keep adding the loop counter to our sum variable.

        int sum = 0; 
        for (int i = 3; i <= 23; i += 4) //loop goes from 3 to 23, in steps of 4. 
        {
            sum += i; //increment sum by i 
        }
        printf("The desired sum is %d.", sum); //print the result 
    output:
    The desired sum is 78.

    Q.3. Design a loop to find the sum of the 1st 10 terms of this series: 3 + 7 + 11 + …. .

    Solution.
    3 is 4×1 – 1.
    7 is 4×2 – 1.
    11 is 4×3 – 1, and so on.
    So, in general, the i-th term is 4xi – 1.

    We’ll initialize a variable called sum to 0.
    We need to add 10 terms. So, we’ll make the loop counter i go from 1 to 10. Each time, within the loop, we’ll increment sum by (4*i – 1).

    int sum = 0;
    for (int i = 1; i <= 10; i ++) //loop goes from 1 to 10.
    {
    sum += (4*i - 1); //increment sum by (4*i - 1)
    }
    printf("The desired sum is %d.", sum); //print the result
    output :
    The desired sum is 210.

    Q.4. Design a loop to compute the sum of the 1st n terms of the series: 3 + 7 + 11 + … . (n is input from the user.)

    Solution.
    It’s quite the same as above. Simply, instead of 10, we’ll make the loop run till n.

        int sum = 0;
        int n; //how many terms are there in the series? 
        
        printf("How many terms are there in the series? ");
        scanf("%d", &n); 
        
        for (int i = 1; i <= n; i ++) //loop goes from 1 to n. 
        {
            sum += (4*i - 1); //increment sum by (4*i - 1)
        }
        printf("The desired sum is %d.", sum); //print the result 
    sample output 1:
    How many terms are there in the series? 2
    The desired sum is 10.
    sample output 2:
    How many terms are there in the series? 5
    The desired sum is 55.

    Q.5. Design a loop to find the sum: 1 + 2 + 3 + … (till n terms). (Here n is an input from the user.)

    Solution.
    This is similar to the above problem.
    We’ll make the loop go from 1 to n, and in each iteration, increment our sum variable by the loop counter.

    int sum = 0;
    int n; //how many terms are there in the series?
    printf("How many terms are there in the series? ");
    scanf("%d", &n);
    for (int i = 1; i <= n; i ++) //loop goes from 1 to n.
    {
    sum += i; //increment sum by i
    }
    printf("The desired sum is %d.", sum); //print the result
    sample output 1:
    How many terms are there in the series? 3
    The desired sum is 6.
    sample output 2:
    How many terms are there in the series? 5
    The desired sum is 15.

    In this lesson, we looked at using for loop to compute the sum of different number series. (For more on C Programming, you can see here.)

  • बालिका शब्दरूपम्
    विभक्तिःएकवचनम्द्विवचनम्बहुवचनम्
    प्रथमाबालिकाबालिकेबालिकाः
    द्वितीयाबालिकाम्बालिकेबालिकाः
    तृतीयाबालिकयाबालिकाभ्याम्बालिकाभिः
    चतुर्थीबालिकायैबालिकाभ्याम्बालिकाभ्यः
    पञ्चमीबालिकायाःबालिकाभ्याम्बालिकाभ्यः
    षष्ठीबालिकायाःबालिकयोःबालिकानाम्
    सप्तमीबालिकायाम्बालिकयोःबालिकासु
    सम्बोधनम्हे बालिके!हे बालिके!हे बालिकाः!

    Other examples of आकारान्त स्त्रीलिङ्ग words having similar shabd roop – सीता , विद्या, कन्या, कथा, पूजा , etc.


    Examples of प्रथमा विभक्तिः

    SanskritHindiEnglish
    बालिका पठति।लड़की पढ़ती है।The girl studies.
    बालिका गच्छति।लड़की जाती है।The girl goes.
    बालिका गायति।लड़की गाती है।The girl sings.
    बालिके पठतः।दो लड़कियाँ पढ़ती हैं।The two girls study.
    बालिके गच्छतः।दो लड़कियाँ जाती हैं।The two girls go.
    बालिके गायतः।दो लड़कियाँ गाती हैं।The two girls sing.
    बालिकाः पठन्ति।लड़कियाँ पढ़ती हैं।The girls study.
    बालिकाः गच्छन्ति।लड़कियाँ जाती हैं।The girls go.
    बालिकाः गायन्ति।लड़कियाँ गाती हैं।The girls sing.

    Related post – बालक शब्दरूपम्

  • Qualities of sadhus

    तितिक्षव: कारुणिका: सुहृद: सर्वदेहिनाम् ।
    अजातशत्रव: शान्ता: साधव: साधुभूषणा: ॥
    (Srimad Bhagavatam 3.25.21)

    Bhagavan Sri Kapila tells about qualities of sadhu-s or saintly persons.

    तितिक्षव – sadhus are very tolerant and forgiving.
    कारुणिका – sadhus are very kind, compassionate, merciful .
    सुहृद: सर्वदेहिनाम् – They are sincere well-wishers to all beings.
    अजातशत्रव – They do not harbour enmity or inimical feelings towards anyone.
    शान्ता – They are peaceful at heart.
    साधव: साधुभूषणा: – These are the beautiful ornaments of sadhus.

Is this your new site? Log in to activate admin features and dismiss this message
Log In