Pages

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;

}

 

Friday, August 19, 2022

C Programming

  • C Programming

  • Saturday, August 1, 2020

    C LANGUAGE PROJECT



    Basic MEDICAL STORE - PROJECT

    /* ------- Files details -------
    1. Stock_details.dat (File used to store stock item details)
    2. Sales_details.dat (File used to store sales details)
    */

    #include<stdio.h>
    #include<conio.h>
    #include<process.h>

    //Function declarations
    void main_menu();
    void insert_record();
    void sales_details();
    void sales_report();
    void stock_details();

    // Definition of main function
    void main()
    {
    clrscr();
    int c;
    char ch;
    do
    {
    main_menu();

    printf("\n\nEnter your choice (1-5):");
    scanf("%d",&c);

    // Calling funtion by switch statement
    switch(c)
    {
    case 1:
    insert_record(); break;
    case 2:
    sales_details(); break;
    case 3:
    sales_report(); break;
    case 4:
    stock_details(); break;
    case 5:
    exit(0);
    default:
    printf("Wrong choice, try again");
    }

    printf("\n\n\n\nDo you want to continue (y/n):");
    ch=getche();
    }while(ch=='y' || ch=='Y');
    getch();
    }

    //Define menu details funtcion
    void main_menu ()
    {
    clrscr();
    printf("\n\t\t\t---------------------------\n");
    printf("\t\t\t* * * Bhavesh Medico * * *");
    printf("\n\t\t\t---------------------------\n");

    printf("\n\n\n----- Insert Records -----\n");
    printf("1. New Medicine");
    printf("\n2. New Sales");

    printf("\n\n\n----- Reports -----\n");
    printf("3. Sales");
    printf("\n4. Stock");

    printf("\n\n\n----- Exit Function -----\n");
    printf("5. Exit");
    }

    //Define menu details funtcion
    void insert_record()
    {
    int m_id,price;
    char m_name[20], brand[40];
    char m_date[20], e_date[40];

    printf("Enter medicine no:");
    scanf("%d",&m_id);
    printf("Enter medicine  name:");
    scanf("%s",m_name);
    printf("Enter company/brand name:");
    scanf("%s",brand);
    printf("Enter price:");
    scanf("%d",&price);
    printf("Enter manufacturing date:");
    scanf("%s",m_date);
    printf("Enter expiry date:");
    scanf("%s",e_date);

    FILE *ob;
    ob=fopen("stock_details.dat","a");
    fprintf(ob,"\n%d",m_id);
    fprintf(ob,"\t%s",m_name);
    fprintf(ob,"\t%s",brand);
    fprintf(ob,"\t%d",price);
    fprintf(ob,"\t%s",m_date);
    fprintf(ob,"\t%s",e_date);
    fclose(ob);

    }

    //Define menu details funtcion
    void sales_details()
    {
    int price,q;
    char date[20], brand[20];
    char item_name[20], buyer_name[40];

    printf("\nAvailable stocks.....\n");
    stock_details();
    printf("Enter date (MM/DD):");
    scanf("%s",&date);
    printf("Enter customer name:");
    scanf("%s",&buyer_name);
    printf("Enter item name:");
    scanf("%s",&item_name);
    printf("Enter item price:");
    scanf("%d",&price);
    printf("Enter quantity:");
    scanf("%d",&q);

    FILE *ob;
    ob=fopen("sales_details.dat","a");
    fprintf(ob,"\n%s",date);
    fprintf(ob,"\t%s",buyer_name);
    fprintf(ob,"\t\t%s",item_name);
    fprintf(ob,"\t\t  %d",q);
    fprintf(ob,"\t\t%d",(q*price));

    fclose(ob);
    }

    //Define menu details funtcion
    void sales_report()
    {
    char ch;
    FILE *ob;
    ob=fopen("sales_details.dat","r");
    if(ob==NULL)
    {
    printf("No Records found");
    }
    else
    {
    printf("\n---------------------------------------------------------------");
    printf("\nDate\tCust.Name\tItem name\tQuant.\t     Total price");
    printf("\n---------------------------------------------------------------\n");

    while((ch=fgetc(ob))!=EOF)
    {
    printf("%c",ch);
    }
    fclose(ob);
    }
    }

    //Define menu details funtcion
    void stock_details()
    {
    char ch;
    FILE *ob;
    ob=fopen("stock_details.dat","r");
    if(ob==NULL)
    {
    printf("No Record found");
    }

    printf("\n----------------------------------------------------------");
    printf("\nCode \tName \tBrand \tPrice \tManuf.Date \tExp.Date");
    printf("\n----------------------------------------------------------\n");

    while((ch=fgetc(ob))!=EOF)
    {
    printf("%c",ch);
    }
    printf("\n----------------------------------------------------------\n");

    fclose(ob);
    }