Pages

Wednesday, May 15, 2024

How can we return multiple values from a function in C/C++?

In C or C++, we cannot return multiple values from a function directly. In this section, we will see how to use some trick to return more than one value from a function.

We can return more than one values from a function by using the method called “call by address”, or call by reference. In the invoker function, we will use two variables to store the results, and the function will take pointer type data. So we have to pass the address of the data.

In this example, we will see how to define a function that can return quotient and remainder after dividing two numbers from one single function.

Example Code

#include<stdio.h>
void div(int a, int b, int *quotient, int *remainder) {
   *quotient = a / b;
   *remainder = a % b;
}
main() {
   int a = 76, b = 10;
   int q, r;
   div(a, b, &q, &r);
   printf("Quotient is: %d\nRemainder is: %d\n", q, r);
}

..

Output

Quotient is: 7
Remainder is: 6
#include<stdio.h>
void sum(int a, int b, int *s , int *m){
	*s = a+b;
	*m = a*b;
}

void main(){
	int s,m;
	
	sum(5,5,&s,&m);
	printf("%d", s);
	printf("%d", m);
}
..

Sunday, March 3, 2024

C Exercises: Display first 10 natural numbers

 

C Exercises: Display first 10 natural numbers

C For Loop: Exercise-1 with Solution

Write a program in C to display the first 10 natural numbers.

Visual Presentation:

Display first 10 natural numbers

Sample Solution:

C Code:

#include <stdio.h>  // Include the standard input/output header file.
int main() {
    int i;

    // Print a message indicating the intention of the program
    printf("The first 10 natural numbers are:\n");

    // Loop through the first 10 natural numbers and print each one
    for (i = 1; i <= 10; i++) {
        printf("%d ", i);
    }

    // Return 0 to indicate successful execution
    return 0;
}


Sample Output:

The first 10 natural numbers are:                                                                               
1 2 3 4 5 6 7 8 9 10  

Explanation:

for (i = 1; i <= 10; i++) {
  printf("%d ", i);
}

In the above for loop, the variable i is initialized to 1, and the loop will continue as long as i is less than or equal to 10. In each iteration of the loop, the printf function will print the value of i to the console, followed by a space character.

Finally, the loop will increment the value of i by 1, and the process will repeat until the condition i<=10 is no longer true.

So, when this code runs, it will output the numbers 1 through 10 separated by spaces.

Flowchart:

Flowchart: Display first 10 natural numbers

Step wise execution of C program :

  • Green color means line that has just executed
  • Red color means next line to excute
  • Stack : a stack is an abstract data type that serves as a collection of elements.
StepsCodeStackOutput
Step-1
1 int main(){
2 int i;
3 printf("The first 10 natural numbers are:\n");
4 for (i=1;i<=10;i++)
5 {      
6 printf("%d ",i);
7 }
8 printf("\n");
9 return 0;
10 }
  

main
i(int): ?

Step-2
1 int main(){
2 int i;
3 printf("The first 10 natural numbers are:\n");
4 for (i=1;i<=10;i++)
5 {      
6 printf("%d ",i);
7 }
8 printf("\n");
9 return 0;
10 }
  
main
i(int): ?
Step-3
1 int main(){
2 int i;
3 printf("The first 10 natural numbers are:\n");
4 for (i=1;i<=10;i++)
5 {      
6 printf("%d ",i);
7 }
8 printf("\n");
9 return 0;
10 }
  
main
i(int): ?
The first 10 natural numbers are:
Step-4
1 int main(){
2 int i;
3 printf("The first 10 natural numbers are:\n");
4 for (i=1;i<=10;i++)
5 {      
6 printf("%d ",i);
7 }
8 printf("\n");
9 return 0;
10 }
  
main
i(int): 1
The first 10 natural numbers are:
Step-5
1 int main(){
2 int i;
3 printf("The first 10 natural numbers are:\n");
4 for (i=1;i<=10;i++)
5 {      
6 printf("%d ",i);
7 }
8 printf("\n");
9 return 0;
10 }
  
main
i(int): 1
The first 10 natural numbers are:
1
Step-6
1 int main(){
2 int i;
3 printf("The first 10 natural numbers are:\n");
4 for (i=1;i<=10;i++)
5 {      
6 printf("%d ",i);
7 }
8 printf("\n");
9 return 0;
10 }
  
main
i(int): 2
The first 5 natural numbers are:
1
Step-7
1 int main(){
2 int i;
3 printf("The first 10 natural numbers are:\n");
4 for (i=1;i<=10;i++)
5 {      
6 printf("%d ",i);
7 }
8 printf("\n");
9 return 0;
10 }
  
main
i(int): 2
The first 5 natural numbers are:
1 2
Step-8
1 int main(){
2 int i;
3 printf("The first 10 natural numbers are:\n");
4 for (i=1;i<=10;i++)
5 {      
6 printf("%d ",i);
7 }
8 printf("\n");
9 return 0;
10 }
  
main
i(int): 3
The first 5 natural numbers are:
1 2
Step-9
1 int main(){
2 int i;
3 printf("The first 10 natural numbers are:\n");
4 for (i=1;i<=10;i++)
5 {      
6 printf("%d ",i);
7 }
8 printf("\n");
9 return 0;
10 }
  
main
i(int): 3
The first 5 natural numbers are:
1 2 3
Step-10
1 int main(){
2 int i;
3 printf("The first 10 natural numbers are:\n");
4 for (i=1;i<=10;i++)
5 {      
6 printf("%d ",i);
7 }
8 printf("\n");
9 return 0;
10 }
  
main
i(int): 4
The first 10 natural numbers are:
1 2 3
Step-11
1 int main(){
2 int i;
3 printf("The first 10 natural numbers are:\n");
4 for (i=1;i<=10;i++)
5 {      
6 printf("%d ",i);
7 }
8 printf("\n");
9 return 0;
10 }
  
main
i(int): 4
The first 10 natural numbers are:
1 2 3 4
Step-12
1 int main(){
2 int i;
3 printf("The first 10 natural numbers are:\n");
4 for (i=1;i<=10;i++)
5 {      
6 printf("%d ",i);
7 }
8 printf("\n");
9 return 0;
10 }
  
main
i(int): 5
The first 10 natural numbers are:
1 2 3 4
Step-13
1 int main(){
2 int i;
3 printf("The first 10 natural numbers are:\n");
4 for (i=1;i<=10;i++)
5 {      
6 printf("%d ",i);
7 }
8 printf("\n");
9 return 0;
10 }
  
main
i(int): 5
The first 10 natural numbers are:
1 2 3 4 5
Step-14
1 int main(){
2 int i;
3 printf("The first 10 natural numbers are:\n");
4 for (i=1;i<=10;i++)
5 {      
6 printf("%d ",i);
7 }
8 printf("\n");
9 return 0;
10 }
  
main
i(int): 6
The first 10 natural numbers are:
1 2 3 4 5
Step-15
1 int main(){
2 int i;
3 printf("The first 10 natural numbers are:\n");
4 for (i=1;i<=10;i++)
5 {      
6 printf("%d ",i);
7 }
8 printf("\n");
9 return 0;
10 }
  
main
i(int): 6
The first 10 natural numbers are:
1 2 3 4 5 6
Step-16
1 int main(){
2 int i;
3 printf("The first 10 natural numbers are:\n");
4 for (i=1;i<=10;i++)
5 {      
6 printf("%d ",i);
7 }
8 printf("\n");
9 return 0;
10 }
  
main
i(int): 7
The first 10 natural numbers are:
1 2 3 4 5 6
Step-17
1 int main(){
2 int i;
3 printf("The first 10 natural numbers are:\n");
4 for (i=1;i<=10;i++)
5 {      
6 printf("%d ",i);
7 }
8 printf("\n");
9 return 0;
10 }
main
i(int): 7
The first 10 natural numbers are:
1 2 3 4 5 6 7
Step-18
1 int main(){
2 int i;
3 printf("The first 10 natural numbers are:\n");
4 for (i=1;i<=10;i++)
5 {      
6 printf("%d ",i);
7 }
8 printf("\n");
9 return 0;
10 }
  
main
i(int): 8
The first 10 natural numbers are:
1 2 3 4 5 6 7
Step-19
1 int main(){
2 int i;
3 printf("The first 10 natural numbers are:\n");
4 for (i=1;i<=10;i++)
5 {      
6 printf("%d ",i);
7 }
8 printf("\n");
9 return 0;
10 }
  
main
i(int): 8
The first 10 natural numbers are:
1 2 3 4 5 6 7 8
Step-20
1 int main(){
2 int i;
3 printf("The first 10 natural numbers are:\n");
4 for (i=1;i<=10;i++)
5 {      
6 printf("%d ",i);
7 }
8 printf("\n");
9 return 0;
10 }
  
main
i(int): 9
The first 10 natural numbers are:
1 2 3 4 5 6 7 8
Step-21
1 int main(){
2 int i;
3 printf("The first 10 natural numbers are:\n");
4 for (i=1;i<=10;i++)
5 {      
6 printf("%d ",i);
7 }
8 printf("\n");
9 return 0;
10 }
  
main
i(int): 9
The first 10 natural numbers are: 1 2 3 4 5 6 7 8 9
Step-22
1 int main(){
2 int i;
3 printf("The first 10 natural numbers are:\n");
4 for (i=1;i<=10;i++)
5 {      
6 printf("%d ",i);
7 }
8 printf("\n");
9 return 0;
10 }
  
main
i(int): 10
The first 10 natural numbers are:
1 2 3 4 5 6 7 8 9
Step-23
1 int main(){
2 int i;
3 printf("The first 10 natural numbers are:\n");
4 for (i=1;i<=10;i++)
5 {      
6 printf("%d ",i);
7 }
8 printf("\n");
9 return 0;
10 }
  
main
i(int): 10
The first 10 natural numbers are:
1 2 3 4 5 6 7 8 9 10
Step-24
1 int main(){
2 int i;
3 printf("The first 10 natural numbers are:\n");
4 for (i=1;i<=10;i++)
5 {      
6 printf("%d ",i);
7 }
8 printf("\n");
9 return 0;
10 }
  
main
i(int): 11
The first 10 natural numbers are:
1 2 3 4 5 6 7 8 9 10
Step-25
1 int main(){
2 int i;
3 printf("The first 10 natural numbers are:\n");
4 for (i=1;i<=10;i++)
5 {      
6 printf("%d ",i);
7 }
8 printf("\n");
9 return 0;
10 }
  
main
i(int): 11
The first 10 natural numbers are:
1 2 3 4 5 6 7 8 9 10
Exit
1 int main(){
2 int i;
3 printf("The first 10 natural numbers are:\n");
4 for (i=1;i<=10;i++)
5 {      
6 printf("%d ",i);
7 }
8 printf("\n");
9 return 0;
10 }
  
main
i(int): 11
The first 10 natural numbers are:
1 2 3 4 5 6 7 8 9 10

C Programming Code Editor:

Previous: C For Loop Exercises Home
Next: Write a C program to find the sum of first 10 natural numbers.

What is the difficulty level of this exercise?

  

Monday, February 12, 2024

Define A Structure Employee Having Elements Emp_id, Name, address , DOB. Accept Data And Reprint It. (Use Structure Within Structure) & display only whose address is in “pokhara”

 

Nested Structure

 



 

#include <stdio.h>

#include <string.h>

 

// Define a structure for date

struct Date {

    int day;

    int month;

    int year;

};

 

// Define a structure for an employee

struct Employee {

    int emp_id;

    char name[50];

    char address[100];

    struct Date dob; // Date of Birth

};

 

int main() {

    // Declare an array of employees

    struct Employee employees[5];

 

    // Accept data for each employee

    for (int i = 0; i < 5; ++i) {

        printf("Enter details for Employee %d:\n", i + 1);

        printf("Employee ID: ");

        scanf("%d", &employees[i].emp_id);

        printf("Name: ");

        scanf("%s", employees[i].name);

        printf("Address: ");

        scanf("%s", employees[i].address);

        printf("Date of Birth (DD MM YYYY): ");

        scanf("%d %d %d", &employees[i].dob.day, &employees[i].dob.month, &employees[i].dob.year);

    }

 

    // Reprint the data for employees with address in Pokhara

    printf("\nEmployee Data in Pokhara:\n");

    for (int i = 0; i < 5; ++i) {

        if (strcmp(employees[i].address, "Pokhara") == 0) {

            printf("Employee %d:\n", i + 1);

            printf("Employee ID: %d\n", employees[i].emp_id);

            printf("Name: %s\n", employees[i].name);

            printf("Address: %s\n", employees[i].address);

            printf("Date of Birth: %02d-%02d-%04d\n", employees[i].dob.day, employees[i].dob.month, employees[i].dob.year);

            printf("\n");

        }

    }

 

    return 0;

}