Example program passing structure as function argument C C++

Coding in C

#include<stdio.h>
struct student
{
    char grade;
    int marks;
    float per;
};
void display(struct student);
int main()
{
    struct student s;
    printf("Enter grade  : ");
    scanf("%c",&s.grade);
    printf("Enter marks  : ");
    scanf("%d",&s.marks);
    printf("Enter Percent: ");
    scanf("%f",&s.per);
    display(s);
    return 0;
}
void display(struct student s)
{
    printf("Grade  = %c\n",s.grade);
    printf("Marks  = %d\n",s.marks);
    printf("Percent= %f\n",s.per);
}

Coding in C++

#include<iostream>
using namespace std;
struct student
{
    char grade;
    int marks;
    float per;
};
void display(struct student);
int main()
{
    struct student s;
    cout<<"Enter grade  :  ";
    cin>>s.grade;
    cout<<"Enter marks  :  ";
    cin>>s.marks;
    cout<<"Enter percent:  ";
    cin>>s.per;
    display(s);
    return 0;
}
void display(struct student s)
{
    cout<<"Grade    = "<<s.grade<<endl;
    cout<<"Marks    = "<<s.marks<<endl;
    cout<<"Percent  = "<<s.per<<endl;
}

Output

Share it to:

Example program passing array as function argument in C C++

Coding in C

#include<stdio.h>
void display(int[],int);
int main()
{
    int a[5],i=0;
    printf("Enter values: ");
    for(i=0;i<5;i++)
    scanf("%d",&a[i]);
    display(a,5);
    return 0;
}
void display(int a[], int n)
{
    int i=0,sum=0;
    for(i=0;i<n;i++)
    {
        printf("Element = %d\n",a[i]);
        sum=sum+a[i];
    }
    printf("Sum = %d",sum);
}

Coding in C++

#include<iostream>
using namespace std;
void display(int[],int);
int main()
{
    int a[5],i=0;
    cout<<"Enter values: ";
    for(i=0;i<5;i++)
    cin>>a[i];
    display(a,5);
    return 0;
}
void display(int a[], int n)
{
    int i=0,sum=0;
    for(i=0;i<n;i++)
    {
        cout<<"Element = "<<a[i]<<endl;
        sum=sum+a[i];
    }
    cout<<"Sum = "<<sum;
}

Output

Example program for passing array as function argument in C
Share it to:

C C++ program to differentiate between structure and union

Coding in C

#include<stdio.h>

struct s
{
    char c;
    int i;
    float f;
}sv;

union u
{
    char c;
    int i;
    float f;
}uv;

int main()
{
    printf("Size of sv = %d\n",sizeof(sv));
    printf("Size of uv = %d\n",sizeof(uv));
    printf("Addresses of sv = %u %u %u\n",&sv.c,&sv.i,&sv.f);
    printf("Addresses of uv = %u %u %u\n",&uv.c,&uv.i,&uv.f);
    return 0;
}

Coding in C++

#include<iostream>
using namespace std;

struct s
{
    char c;
    int i;
    float f;
}sv;

union u
{
    char c;
    int i;
    float f;
}uv;

int main()
{
    cout<<"Size of sv = "<<sizeof(sv)<<endl;
    cout<<"Size of uv = "<<sizeof(uv)<<endl;
    cout<<"Addresses of sv = "<<&sv.c<<" "<<&sv.i<<" "<<&sv.f<<endl;
    cout<<"Addresses of uv = "<<&uv.c<<" "<<&uv.i<<" "<<&uv.f<<endl;
    return 0;
}

Output

C C++ program to differentiate between structure and union
Share it to:

Pointer to structure example program in C C++

Coding in C

#include<stdio.h>

struct student
{
    char grade;
    int marks;
    float percent;
};

int main()
{
    struct student std;
    struct student *ptr;
    ptr=&std;
    printf("Enter Grade   : ");
    scanf("%c",&std.grade);
    printf("Enter Marks   : ");
    scanf("%d",&std.marks);
    printf("Enter Percent : ");
    scanf("%f",&std.percent);
    printf("The Grade is  : %c",ptr->grade);
    printf("\nThe Marks is  : %d",ptr->marks);
    printf("\nThe Percent is: %f",ptr->percent);
    return 0;
}

Coding in C++

#include<iostream>
using namespace std;

struct student
{
    char grade;
    int marks;
    float percent;
};

int main()
{
    struct student std;
    struct student *ptr;
    ptr=&std;
    cout<<"Enter Grade    : ";
    cin>>std.grade;
    cout<<"Enter Marks    : ";
    cin>>std.marks;
    cout<<"Enter Percent  : ";
    cin>>std.percent;
    cout<<"The Grade is   : "<<ptr->grade<<endl;
    cout<<"The Marks is   : "<<ptr->marks<<endl;
    cout<<"The Percent is : "<<ptr->percent<<endl;
    return 0;
}

Output

Pointer to structure example program in C C++
Share it to:

Pointer to array example program in C C++

Coding in C

#include<stdio.h>
int main()
{
    int arr[5]={3,9,1,4,8},i;
    int *ptr;
    ptr=&arr[0];
    for(i=0;i<5;i++)
    {
        printf("Element = %d Address = %u\n",*ptr,ptr);
        ptr++;
    }
    return 0;
}

Coding in C++

#include<iostream>
using namespace std;
int main()
{
    int arr[5]={3,9,1,4,8},i;
    int *ptr;
    ptr=&arr[0];
    for(i=0;i<5;i++)
    {
        cout<<"Element = "<<*ptr<<"Address = "<<ptr<<endl;
        ptr++;
    }
    return 0;
}

Output

Pointer to array example program in C C++
Share it to:

Pointer Arithmetic Programs in C C++

Incrementing Pointer in C

#include<stdio.h>
int main()
{
    int num, *ptr;
    ptr=#
    printf("value of ptr = %u\n",ptr);
    ptr=ptr+1;
    printf("value of ptr = %u\n",ptr);
    ptr=ptr+1;
    printf("value of ptr = %u\n",ptr);
    return 0;
}

Incrementing Pointer in C++

#include<iostream>
using namespace std;
int main()
{
    int num, *ptr;
    ptr=#
    cout<<"value of ptr = "<<ptr<<endl;
    ptr=ptr+1;
    cout<<"value of ptr = "<<ptr<<endl;
    ptr=ptr+1;
    cout<<"value of ptr = "<<ptr<<endl;
    return 0;
}

Output:

Pointer Arithmetic Incrementing Programs in C C++

Decrementing Pointer in C

#include<stdio.h>
int main()
{
    int num, *ptr;
    ptr=#
    printf("value of ptr = %u\n",ptr);
    ptr=ptr-1;
    printf("value of ptr = %u\n",ptr);
    ptr=ptr-1;
    printf("value of ptr = %u\n",ptr);
    return 0;
}

Decrementing Pointer in C++

#include<iostream>
using namespace std;
int main()
{
    int num, *ptr;
    ptr=#
    cout<<"value of ptr = "<<ptr<<endl;
    ptr=ptr-1;
    cout<<"value of ptr = "<<ptr<<endl;
    ptr=ptr-1;
    cout<<"value of ptr = "<<ptr<<endl;
    return 0;
}

Output:

Pointer Arithmetic Decrementing Programs in C C++
Share it to:

C C++ program to generate fibonacci series using recursion

Coding in C

#include<stdio.h>
int fab(int);
int main()
{
    int n, i;
    printf("Enter n: ");
    scanf("%d",&n);
    for(i=1;i<=n;i++)
    printf("%d\n",fab(i));
    return 0;
}
int fab(int x)
{
    if(x==1)
        return 0;
    else if(x==2)
        return 1;
    else
        return fab(x-1)+fab(x-2);
}

Coding in C++

#include<iostream>
using namespace std;
int fab(int);
int main()
{
    int n, i;
    cout<<"Enter n: ";
    cin>>n;
    for(i=1;i<=n;i++)
    cout<<fab(i)<<endl;
    return 0;
}
int fab(int x)
{
    if(x==1)
        return 0;
    else if(x==2)
        return 1;
    else
        return fab(x-1)+fab(x-2);
}

Output

C C++ program to generate fibonacci series using recursion
Share it to:

C C++ program to find power of number using recursion

Coding in C

#include<stdio.h>
int power(int,int);
int main()
{
    int num,pow,c=1,res=1;
    printf("Enter number: ");
    scanf("%d",&num);
    printf("Enter power : ");
    scanf("%d",&pow);
    printf("Result = %d",power(num,pow));
    return 0;
}
int power(int num, int pow)
{
    if(pow==0)
        return 1;
    else
        return num*power(num,pow-1);
}

Coding in C++

#include<iostream>
using namespace std;
int power(int,int);
int main()
{
    int num,pow,c=1,res=1;
    cout<<"Enter number: ";
    cin>>num;
    cout<<"Enter power : ";
    cin>>pow;
    cout<<"Result = "<<power(num,pow);
    return 0;
}
int power(int num, int pow)
{
    if(pow==0)
        return 1;
    else
        return num*power(num,pow-1);
}

Output

C C++ program to find power of number using recursion
Share it to:

C C++ program generate factorial of number using recursion

Coding in C

#include<stdio.h>
int fact (int);
int main()
{
    int num,c=1,res=1;
    printf("Enter a number: ");
    scanf("%d",&num);
    printf("Factorial = %d",fact(num));
    return 0;
}
int fact(int x)
{
    if(x==0)
        return 1;
    else
        return x*fact(x-1);
    return 0;
}

Coding in C++

#include<iostream>
using namespace std;
int fact (int);
int main()
{
    int num,c=1,res=1;
    cout<<"Enter a number: ";
    cin>>num;
    cout<<"Factorial = "<<fact(num);
    return 0;
}
int fact(int x)
{
    if(x==0)
        return 1;
    else
        return x*fact(x-1);
    return 0;
}

Output

C C++ program generate factorial of number using recursion
Share it to:

C C++ program to generate table of number using recursion

Coding in C

#include<stdio.h>
void table(int,int,int);
int main()
{
    int num,c=1,res;
    printf("Enter a number: ");
    scanf("%d",&num);
    table(num,c,res);
    return 0;
}
void table(int num, int c, int res)
{
    if(c<=10)
    {
        res=num*c;
        printf("%d\n",res);
        table(num,c+1,res);
    }
}

Coding in C++

#include<iostream>
using namespace std;
void table(int,int,int);
int main()
{
    int num,c=1,res;
    cout<<"Enter a number: ";
    cin>>num;
    table(num,c,res);
    return 0;
}
void table(int num, int c, int res)
{
    if(c<=10)
    {
        res=num*c;
        cout<<res<<endl;
        table(num,c+1,res);
    }
}

Output

C C++ program to generate table of number using recursion
Share it to: