SlideShare a Scribd company logo
1 of 10
Download to read offline
Lab Assignment 4: Function and Pointer in C Programming
CS-153 Computer Programming Lab
Autumn Semester, 2016, IIT Indore
Date: 26-08-16
Note: Write following programs in C language. Also note that this assignment will be evaluated by
TA’s in the upcoming labs of next week (29-08-16 onward) for each batch.
1. Write a program to calculate area and perimeter of a circle by using two different functions. Both the
functions take radius as pass by value. Area function will return area. Perimeter function will return
perimeter.
2. Write a static memory program for 4 students with 3 subjects. Compute the total for each student and
store it into array. Input will be taken through a function. Marks calculation will be done through a
separated function.
3. Compute Fibonacci(N) for given N using recursion. Print how many calls are required for obtaining
this Nth
number in the series?
4. a. Write a program to print address and value of variable.
b. Write a program to print array values and address of an array using pointers.
5. Write a dynamic memory program for N students with M subjects. Compute the total for each student
and store it into array. Input will be taken through a function. Marks calculation will be done through
a separated function.
6. Create a structure to specify data of students given below:
Roll number, Name, Department, Course, Year of joining
Assume that there are varying numbers of students in the institute.
(a) Write a function to print names of all students who joined in a particular year.
(b) Write a function to print the data of a student whose roll number is given.
#include <stdio.h>
const float PI = 3.1415927;
float area(float radius);
float circum(float radius);
#include<stdio.h>
main()
{
float radius;
printf("Enter radius: ");
scanf("%f", &radius);
printf("Area : %.3fn", area(radius));
printf("Circumference: %.3fn", circum(radius));
getch();
}
/* return area of a circle */
float area(float radius)
{
return PI * radius * radius;
}
/* return circumference of a circle */
float circum(float radius)
{
return 2 * PI * radius;
}
#include<stdio.h>
#define SIZE 4
struct student {
char name[30];
int rollno;
int sub[3];
};
main() {
int i, j, max, count, total, n, a[SIZE], ni;
struct student st[SIZE];
printf("Enter how many students: ");
scanf("%d", &n);
/* for loop to read the names and roll numbers*/
for (i = 0; i < n; i++) {
printf("nEnter name and roll number for student %d : ", i);
scanf("%s", &st[i].name);
scanf("%d", &st[i].rollno);
}
/* for loop to read ith student's jth subject*/
for (i = 0; i < n; i++) {
for (j = 0; j <= 2; j++) {
printf("nEnter marks of student %d for subject %d : ", i, j);
scanf("%d", &st[i].sub[j]);
}
}
/* (i) for loop to calculate total marks obtained by each student*/
for (i = 0; i < n; i++) {
total = 0;
for (j = 0; j < 3; j++) {
total = total + st[i].sub[j];
}
printf("nTotal marks obtained by student %s are %dn", st[i].name,total);
a[i] = total;
}
getch();
}
#include<stdio.h>
int Fibonacci(int); //function declared
int count = 0; //global variable
int main()
{
int n, i = 0, c;
printf("Enter the length of the series or number of terms for Fibonacci Seriesn");
scanf("%d",&n);
printf("Fibonacci series:n");
for ( c = 1 ; c <=n ; c++ )
{
printf("%dn", Fibonacci(i)); //function called once
i++;
}
printf("Function called count -> %d",count);
return 0;
}
int Fibonacci(int n) //function defined
{
count++;
if ( n == 0 )
{
return 0;
}
else if ( n == 1 )
{
return 1;
}
else
{
return ( Fibonacci(n-1) + Fibonacci(n-2) ); //function called twice
}
}
#include <stdio.h>
main()
{
char a;
int x;
float p, q;
a = 'A';
x = 125;
p = 10.25, q = 18.76;
printf("%c is stored at addr %u.n", a, &a);
printf("%d is stored at addr %u.n", x, &x);
printf("%f is stored at addr %u.n", p, &p);
printf("%f is stored at addr %u.n", q, &q);
getch();
}
#include <stdio.h>
main()
{
int a[5];int i;
for(i = 0; i<=5; i++)
{
a[i]=i;
}
printdetail(a);
printarr(a);
getch();
}
printarr(int a[])
{ int i;
for(i = 0;i<=5;i++)
{
printf("value in array %dn",a[i]);
}
}
printdetail(int a[])
{int i;
for(i = 0;i<=5;i++)
{
printf("value in array %d and address is %8un",a[i],&a[i]);
}
}
# include <string.h>
# include <stdio.h>
struct student
{
char name[10];
int m[3];
int total;
}*p,*s;
main()
{
int i,j,l,n;
printf("Enter the no. of students : ");
scanf("%d",&n);
p=(struct student*)malloc(n*sizeof(struct student));
s=p;
for(i=0;i<n;i++)
{
printf("Enter a name : ");
scanf("%s",&p->name);
p-> total=0;l=0;
for(j=0;j<3;j++)
{
one:printf("Enter Marks of %d Subject : ",j+1);
scanf("%d",&p->m[j]);
if((p->m[j])>100)
{
printf("Wrong Value Entered");
goto one;
}
p->total+=p->m[j];
}
}
for(i=0;i<n;i++)
{
printf("n%st%d",s->name,s->total);
s++;
}
getch();
}
#include<stdio.h>
#include<conio.h>
#define N 5
struct students {
int rlnm;
char name[25];
char dept[25]; /* structure defined outside of main(); */
char course[25];
int year;
};
main() {
/* main() */
struct students s[N];
int i, ch;
/* taking input of 450 students in an array of structure */
for (i = 0; i < N; i++) {
printf(" Enter data of student %dtttttotal students: %dn", i +
1, N);
printf("****************************nn");
printf("enter rollnumber: ");
scanf("%d", & s[i].rlnm);
printf("nnenter name: ");
scanf(" %s", & s[i].name);
printf("nnenter department: ");
scanf("%s", & s[i].dept);
printf("nnenter course: ");
scanf("%s", & s[i].course);
printf("nnenter year of joining: ");
scanf("%d", & s[i].year);
}
/* displaying a menu */
printf("ntenter your choice: n");
printf("t**********************nn");
printf("1: enter year to search all students who took admission in
that:nn");
printf("2: enter roll number to see details of that studentnnn");
printf("your choice: ");
/* taking input of your choice */
scanf("%d", & ch);
switch (ch) {
case 1:
dispyr( & s);
/* function call to display names of students who joined in
a particular year */
break;
case 2:
disprl( & s);
/* function call to display information of a student 
whose roll number is given */
break;
default:
printf("nnerror! wrong choice");
}
getch();
}
/**
* ***************** main() ends *************
*/
dispyr(struct students *a) { /* function for displaying names of students
who took admission in a particular year */
int j, yr;
printf("nenter year: ");
scanf("%d", & yr);
printf("nnthese students joined in %dnn", yr);
for (j = 0; j < N; j++) {
if (a[j].year == yr) {
printf("n%sn", a[j].name);
}
}
return 0;
}
disprl(struct students *a) { /* function to print information of a
student whose roll number has been 
given. */
int k, rl;
printf("nenter roll number: ");
scanf("%d", & rl);
for (k = 0; k < N; k++) {
if (a[k].rlnm == rl) {
printf("nnt Details of roll number: %dn", a[k].rlnm);
printf("t***************************nn");
printf(" Name: %sn", a[k].name);
printf(" Department: %sn", a[k].dept);
printf(" Course: %sn", a[k].course);
printf("Year of joining: %d", a[k].year);
break;
} else {
printf("nRoll number you entered does not existnn");
break;
}
}
return 0;
}

More Related Content

What's hot

32 shell-programming
32 shell-programming32 shell-programming
32 shell-programming
kayalkarnan
 
C programs
C programsC programs
C programs
Minu S
 

What's hot (20)

UNIT 4-HEADER FILES IN C
UNIT 4-HEADER FILES IN CUNIT 4-HEADER FILES IN C
UNIT 4-HEADER FILES IN C
 
Constant and variacles in c
Constant   and variacles in cConstant   and variacles in c
Constant and variacles in c
 
Unit 7. Functions
Unit 7. FunctionsUnit 7. Functions
Unit 7. Functions
 
Java Simple Programs
Java Simple ProgramsJava Simple Programs
Java Simple Programs
 
第1回python勉強会
第1回python勉強会第1回python勉強会
第1回python勉強会
 
Function in c program
Function in c programFunction in c program
Function in c program
 
32 shell-programming
32 shell-programming32 shell-programming
32 shell-programming
 
File handling in C++
File handling in C++File handling in C++
File handling in C++
 
Java PRACTICAL file
Java PRACTICAL fileJava PRACTICAL file
Java PRACTICAL file
 
XilinxのxsimでSoftware Driven Verification.pdf
XilinxのxsimでSoftware  Driven Verification.pdfXilinxのxsimでSoftware  Driven Verification.pdf
XilinxのxsimでSoftware Driven Verification.pdf
 
Computer science Investigatory Project Class 12 C++
Computer science Investigatory Project Class 12 C++Computer science Investigatory Project Class 12 C++
Computer science Investigatory Project Class 12 C++
 
Python datetime
Python datetimePython datetime
Python datetime
 
Functions in c++
Functions in c++Functions in c++
Functions in c++
 
GCC RTL and Machine Description
GCC RTL and Machine DescriptionGCC RTL and Machine Description
GCC RTL and Machine Description
 
Summer Training Project.pdf
Summer Training Project.pdfSummer Training Project.pdf
Summer Training Project.pdf
 
C programs
C programsC programs
C programs
 
Files in c++
Files in c++Files in c++
Files in c++
 
Debian Linux on Zynq (Xilinx ARM-SoC FPGA) Setup Flow (Vivado 2015.4)
Debian Linux on Zynq (Xilinx ARM-SoC FPGA) Setup Flow (Vivado 2015.4)Debian Linux on Zynq (Xilinx ARM-SoC FPGA) Setup Flow (Vivado 2015.4)
Debian Linux on Zynq (Xilinx ARM-SoC FPGA) Setup Flow (Vivado 2015.4)
 
Css 3 cheat sheet
Css 3 cheat sheetCss 3 cheat sheet
Css 3 cheat sheet
 
Recursion in c
Recursion in cRecursion in c
Recursion in c
 

Similar to C- Programming Assignment 4 solution

function in in thi pdf you will learn what is fu...
function in  in thi pdf you will learn   what                           is fu...function in  in thi pdf you will learn   what                           is fu...
function in in thi pdf you will learn what is fu...
kushwahashivam413
 
Input output functions
Input output functionsInput output functions
Input output functions
hyderali123
 
A10presentationofbiologyandpshyology.pptx
A10presentationofbiologyandpshyology.pptxA10presentationofbiologyandpshyology.pptx
A10presentationofbiologyandpshyology.pptx
ranjangamer007
 

Similar to C- Programming Assignment 4 solution (20)

Unit2 C
Unit2 C Unit2 C
Unit2 C
 
Unit2 C
Unit2 CUnit2 C
Unit2 C
 
C programs
C programsC programs
C programs
 
C-programs
C-programsC-programs
C-programs
 
week-3x
week-3xweek-3x
week-3x
 
function in in thi pdf you will learn what is fu...
function in  in thi pdf you will learn   what                           is fu...function in  in thi pdf you will learn   what                           is fu...
function in in thi pdf you will learn what is fu...
 
C programs
C programsC programs
C programs
 
Introduction to Basic C programming 02
Introduction to Basic C programming 02Introduction to Basic C programming 02
Introduction to Basic C programming 02
 
Fucntions & Pointers in C
Fucntions & Pointers in CFucntions & Pointers in C
Fucntions & Pointers in C
 
Najmul
Najmul  Najmul
Najmul
 
C file
C fileC file
C file
 
7 functions
7  functions7  functions
7 functions
 
C lab programs
C lab programsC lab programs
C lab programs
 
C lab programs
C lab programsC lab programs
C lab programs
 
Input output functions
Input output functionsInput output functions
Input output functions
 
Unit 5 Foc
Unit 5 FocUnit 5 Foc
Unit 5 Foc
 
An imperative study of c
An imperative study of cAn imperative study of c
An imperative study of c
 
chapter-6 slide.pptx
chapter-6 slide.pptxchapter-6 slide.pptx
chapter-6 slide.pptx
 
C programming BY Mazedur
C programming BY MazedurC programming BY Mazedur
C programming BY Mazedur
 
A10presentationofbiologyandpshyology.pptx
A10presentationofbiologyandpshyology.pptxA10presentationofbiologyandpshyology.pptx
A10presentationofbiologyandpshyology.pptx
 

More from Animesh Chaturvedi

More from Animesh Chaturvedi (20)

Cloud Platforms & Frameworks
Cloud Platforms & FrameworksCloud Platforms & Frameworks
Cloud Platforms & Frameworks
 
Cloud platforms and frameworks
Cloud platforms and frameworksCloud platforms and frameworks
Cloud platforms and frameworks
 
Cloud service lifecycle management
Cloud service lifecycle managementCloud service lifecycle management
Cloud service lifecycle management
 
Graph Analytics and Complexity Questions and answers
Graph Analytics and Complexity Questions and answersGraph Analytics and Complexity Questions and answers
Graph Analytics and Complexity Questions and answers
 
Advance Systems Engineering Topics
Advance Systems Engineering TopicsAdvance Systems Engineering Topics
Advance Systems Engineering Topics
 
P, NP, NP-Complete, and NP-Hard
P, NP, NP-Complete, and NP-HardP, NP, NP-Complete, and NP-Hard
P, NP, NP-Complete, and NP-Hard
 
System Development Life Cycle (SDLC)
System Development Life Cycle (SDLC)System Development Life Cycle (SDLC)
System Development Life Cycle (SDLC)
 
Shortest path, Bellman-Ford's algorithm, Dijkastra's algorithm, their Java co...
Shortest path, Bellman-Ford's algorithm, Dijkastra's algorithm, their Java co...Shortest path, Bellman-Ford's algorithm, Dijkastra's algorithm, their Java co...
Shortest path, Bellman-Ford's algorithm, Dijkastra's algorithm, their Java co...
 
Minimum Spanning Tree (MST), Kruskal's algorithm and Prim's Algorithm, and th...
Minimum Spanning Tree (MST), Kruskal's algorithm and Prim's Algorithm, and th...Minimum Spanning Tree (MST), Kruskal's algorithm and Prim's Algorithm, and th...
Minimum Spanning Tree (MST), Kruskal's algorithm and Prim's Algorithm, and th...
 
C- Programming Assignment practice set 2 solutions
C- Programming Assignment practice set 2 solutionsC- Programming Assignment practice set 2 solutions
C- Programming Assignment practice set 2 solutions
 
C- Programming Assignment 3
C- Programming Assignment 3C- Programming Assignment 3
C- Programming Assignment 3
 
C - Programming Assignment 1 and 2
C - Programming Assignment 1 and 2C - Programming Assignment 1 and 2
C - Programming Assignment 1 and 2
 
System requirements engineering
System requirements engineeringSystem requirements engineering
System requirements engineering
 
Informatics systems
Informatics systemsInformatics systems
Informatics systems
 
Introduction to Systems Engineering
Introduction to Systems EngineeringIntroduction to Systems Engineering
Introduction to Systems Engineering
 
Big Data Analytics and Ubiquitous computing
Big Data Analytics and Ubiquitous computingBig Data Analytics and Ubiquitous computing
Big Data Analytics and Ubiquitous computing
 
Cloud Platforms and Frameworks
Cloud Platforms and FrameworksCloud Platforms and Frameworks
Cloud Platforms and Frameworks
 
Cloud Service Life-cycle Management
Cloud Service Life-cycle ManagementCloud Service Life-cycle Management
Cloud Service Life-cycle Management
 
Push Down Automata (PDA)
Push Down Automata (PDA)Push Down Automata (PDA)
Push Down Automata (PDA)
 
Pumping Lemma and Regular language or not?
Pumping Lemma and Regular language or not?Pumping Lemma and Regular language or not?
Pumping Lemma and Regular language or not?
 

Recently uploaded

Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Victor Rentea
 

Recently uploaded (20)

Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptx
 
Simplifying Mobile A11y Presentation.pptx
Simplifying Mobile A11y Presentation.pptxSimplifying Mobile A11y Presentation.pptx
Simplifying Mobile A11y Presentation.pptx
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
The Zero-ETL Approach: Enhancing Data Agility and Insight
The Zero-ETL Approach: Enhancing Data Agility and InsightThe Zero-ETL Approach: Enhancing Data Agility and Insight
The Zero-ETL Approach: Enhancing Data Agility and Insight
 
Design and Development of a Provenance Capture Platform for Data Science
Design and Development of a Provenance Capture Platform for Data ScienceDesign and Development of a Provenance Capture Platform for Data Science
Design and Development of a Provenance Capture Platform for Data Science
 
TEST BANK For Principles of Anatomy and Physiology, 16th Edition by Gerard J....
TEST BANK For Principles of Anatomy and Physiology, 16th Edition by Gerard J....TEST BANK For Principles of Anatomy and Physiology, 16th Edition by Gerard J....
TEST BANK For Principles of Anatomy and Physiology, 16th Edition by Gerard J....
 
WSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering Developers
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
WSO2 Micro Integrator for Enterprise Integration in a Decentralized, Microser...
WSO2 Micro Integrator for Enterprise Integration in a Decentralized, Microser...WSO2 Micro Integrator for Enterprise Integration in a Decentralized, Microser...
WSO2 Micro Integrator for Enterprise Integration in a Decentralized, Microser...
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Six Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal OntologySix Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal Ontology
 
API Governance and Monetization - The evolution of API governance
API Governance and Monetization -  The evolution of API governanceAPI Governance and Monetization -  The evolution of API governance
API Governance and Monetization - The evolution of API governance
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Modernizing Legacy Systems Using Ballerina
Modernizing Legacy Systems Using BallerinaModernizing Legacy Systems Using Ballerina
Modernizing Legacy Systems Using Ballerina
 
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 

C- Programming Assignment 4 solution

  • 1. Lab Assignment 4: Function and Pointer in C Programming CS-153 Computer Programming Lab Autumn Semester, 2016, IIT Indore Date: 26-08-16 Note: Write following programs in C language. Also note that this assignment will be evaluated by TA’s in the upcoming labs of next week (29-08-16 onward) for each batch. 1. Write a program to calculate area and perimeter of a circle by using two different functions. Both the functions take radius as pass by value. Area function will return area. Perimeter function will return perimeter. 2. Write a static memory program for 4 students with 3 subjects. Compute the total for each student and store it into array. Input will be taken through a function. Marks calculation will be done through a separated function. 3. Compute Fibonacci(N) for given N using recursion. Print how many calls are required for obtaining this Nth number in the series? 4. a. Write a program to print address and value of variable. b. Write a program to print array values and address of an array using pointers. 5. Write a dynamic memory program for N students with M subjects. Compute the total for each student and store it into array. Input will be taken through a function. Marks calculation will be done through a separated function. 6. Create a structure to specify data of students given below: Roll number, Name, Department, Course, Year of joining Assume that there are varying numbers of students in the institute. (a) Write a function to print names of all students who joined in a particular year. (b) Write a function to print the data of a student whose roll number is given.
  • 2. #include <stdio.h> const float PI = 3.1415927; float area(float radius); float circum(float radius); #include<stdio.h> main() { float radius; printf("Enter radius: "); scanf("%f", &radius); printf("Area : %.3fn", area(radius)); printf("Circumference: %.3fn", circum(radius)); getch(); } /* return area of a circle */ float area(float radius) { return PI * radius * radius; } /* return circumference of a circle */ float circum(float radius) { return 2 * PI * radius; }
  • 3. #include<stdio.h> #define SIZE 4 struct student { char name[30]; int rollno; int sub[3]; }; main() { int i, j, max, count, total, n, a[SIZE], ni; struct student st[SIZE]; printf("Enter how many students: "); scanf("%d", &n); /* for loop to read the names and roll numbers*/ for (i = 0; i < n; i++) { printf("nEnter name and roll number for student %d : ", i); scanf("%s", &st[i].name); scanf("%d", &st[i].rollno); } /* for loop to read ith student's jth subject*/ for (i = 0; i < n; i++) { for (j = 0; j <= 2; j++) { printf("nEnter marks of student %d for subject %d : ", i, j); scanf("%d", &st[i].sub[j]); } } /* (i) for loop to calculate total marks obtained by each student*/ for (i = 0; i < n; i++) { total = 0; for (j = 0; j < 3; j++) { total = total + st[i].sub[j]; } printf("nTotal marks obtained by student %s are %dn", st[i].name,total); a[i] = total; } getch(); }
  • 4. #include<stdio.h> int Fibonacci(int); //function declared int count = 0; //global variable int main() { int n, i = 0, c; printf("Enter the length of the series or number of terms for Fibonacci Seriesn"); scanf("%d",&n); printf("Fibonacci series:n"); for ( c = 1 ; c <=n ; c++ ) { printf("%dn", Fibonacci(i)); //function called once i++; } printf("Function called count -> %d",count); return 0; } int Fibonacci(int n) //function defined { count++; if ( n == 0 ) { return 0; } else if ( n == 1 ) { return 1; } else { return ( Fibonacci(n-1) + Fibonacci(n-2) ); //function called twice } }
  • 5. #include <stdio.h> main() { char a; int x; float p, q; a = 'A'; x = 125; p = 10.25, q = 18.76; printf("%c is stored at addr %u.n", a, &a); printf("%d is stored at addr %u.n", x, &x); printf("%f is stored at addr %u.n", p, &p); printf("%f is stored at addr %u.n", q, &q); getch(); }
  • 6. #include <stdio.h> main() { int a[5];int i; for(i = 0; i<=5; i++) { a[i]=i; } printdetail(a); printarr(a); getch(); } printarr(int a[]) { int i; for(i = 0;i<=5;i++) { printf("value in array %dn",a[i]); } } printdetail(int a[]) {int i; for(i = 0;i<=5;i++) { printf("value in array %d and address is %8un",a[i],&a[i]); } }
  • 7. # include <string.h> # include <stdio.h> struct student { char name[10]; int m[3]; int total; }*p,*s; main() { int i,j,l,n; printf("Enter the no. of students : "); scanf("%d",&n); p=(struct student*)malloc(n*sizeof(struct student)); s=p; for(i=0;i<n;i++) { printf("Enter a name : "); scanf("%s",&p->name); p-> total=0;l=0; for(j=0;j<3;j++) { one:printf("Enter Marks of %d Subject : ",j+1); scanf("%d",&p->m[j]); if((p->m[j])>100) { printf("Wrong Value Entered"); goto one; } p->total+=p->m[j]; } } for(i=0;i<n;i++) { printf("n%st%d",s->name,s->total); s++; } getch(); }
  • 8. #include<stdio.h> #include<conio.h> #define N 5 struct students { int rlnm; char name[25]; char dept[25]; /* structure defined outside of main(); */ char course[25]; int year; }; main() { /* main() */ struct students s[N]; int i, ch; /* taking input of 450 students in an array of structure */ for (i = 0; i < N; i++) { printf(" Enter data of student %dtttttotal students: %dn", i + 1, N); printf("****************************nn"); printf("enter rollnumber: "); scanf("%d", & s[i].rlnm); printf("nnenter name: "); scanf(" %s", & s[i].name); printf("nnenter department: "); scanf("%s", & s[i].dept); printf("nnenter course: "); scanf("%s", & s[i].course); printf("nnenter year of joining: "); scanf("%d", & s[i].year); } /* displaying a menu */ printf("ntenter your choice: n"); printf("t**********************nn"); printf("1: enter year to search all students who took admission in that:nn"); printf("2: enter roll number to see details of that studentnnn"); printf("your choice: "); /* taking input of your choice */ scanf("%d", & ch); switch (ch) { case 1: dispyr( & s); /* function call to display names of students who joined in a particular year */ break; case 2:
  • 9. disprl( & s); /* function call to display information of a student whose roll number is given */ break; default: printf("nnerror! wrong choice"); } getch(); } /** * ***************** main() ends ************* */ dispyr(struct students *a) { /* function for displaying names of students who took admission in a particular year */ int j, yr; printf("nenter year: "); scanf("%d", & yr); printf("nnthese students joined in %dnn", yr); for (j = 0; j < N; j++) { if (a[j].year == yr) { printf("n%sn", a[j].name); } } return 0; } disprl(struct students *a) { /* function to print information of a student whose roll number has been given. */ int k, rl; printf("nenter roll number: "); scanf("%d", & rl); for (k = 0; k < N; k++) { if (a[k].rlnm == rl) { printf("nnt Details of roll number: %dn", a[k].rlnm); printf("t***************************nn"); printf(" Name: %sn", a[k].name); printf(" Department: %sn", a[k].dept); printf(" Course: %sn", a[k].course); printf("Year of joining: %d", a[k].year); break; } else {
  • 10. printf("nRoll number you entered does not existnn"); break; } } return 0; }