SlideShare une entreprise Scribd logo
1  sur  7
Télécharger pour lire hors ligne
26
Set 2 Q1 Program to calculate the standard deviation of an array of values using
function
Program
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
float calsd(float array[],int num);
int main()
{
int i,n;
float *a;
printf("Enter number of element in the array ");
scanf("%d", &n);
a= (float*) malloc(n*sizeof(float));
printf("Enter the elements in the arrayn ");
for(i=0; i < n; i++)
scanf("%f", &a[i]);
printf("nStandard Deviation = %.3f", calsd(a,n));
free (a);
return 0;
}
float calsd(float array[],int num)
{
float sum = 0.0, mean, stddev = 0.0;
int i;
for(i=0; i<num; i++)
{
sum =sum + array[i];
}
mean = sum/num;
27
for(i=0; i<num; ++i)
{
stddev = stddev + pow(array[i] - mean, 2);
stddev = sqrt(stddev/num);
}
return stddev;
}
Output
Enter number of element in the array 10
Enter the elements in the array
2
3
4
1
5
6
9
8
7
5
Standard Deviation = 0.266
28
Set 2 Q2 Program to find the factorial of a number using recursive function
Program
#include <stdio.h>
long int fact(int n);
int main()
{
int n;
printf("Enter a positive integer: ");
scanf("%d", &n);
printf("Factorial of %d = %ldn",n, fact(n));
return 0;
}
long int fact(int n)
{
if (n >= 1)
return n*fact(n-1);
else
return 1;
}
Output
Enter a positive integer: 4
Factorial of 4 = 24
29
Set 2 Q3 Recursive function to find the sum of odd numbers between a given
range
Program
#include <stdio.h>
int sumodd(int n,int m);
int main()
{
int n,m;
printf("Enter the start and end range ");
scanf("%d %d", &n,&m);
if (n%2==0)
n=n+1;
printf("Sum of odd numbers between %d and %d is %dn",n,m,
sumodd(n,m));
return 0;
}
int sumodd(int n, int m)
{
if (n<=m)
{
return n+sumodd(n+2,m);
}
else
return 0;
}
Output
Enter the start and end range 3
9
Sum of odd numbers between 3 and 9 is 24
30
Set 2 Q4 Program to find the GCD of two numbers using recursive function
Program
#include <stdio.h>
int gcd(int a, int b);
int main()
{
int a, b, g;
printf("Enter the two numbers: ");
scanf("%d %d", &a, &b);
g = gcd(a, b);
printf("The GCD of %d and %d is %d n", a, b, g);
return 0;
}
int gcd(int a, int b)
{
while (a != b)
{
if (a > b)
{
return gcd(a - b, b);
}
else
{
return gcd(a, b - a);
}
}
return a;
}
Output
Enter the two numbers: 100
50
The GCD of 100 and 50 is 50
31
Set 2 Q5 Program to reverse a text using recursive function
Program
#include <stdio.h>
#include <string.h>
void rev(char*, int, int);
int main()
{
char a[100];
printf("Enter the string: n");
gets(a);
rev(a, 0, strlen(a)-1);
printf("%sn",a);
return 0;
}
void rev(char *x, int start, int end)
{
char c;
if (start >= end)
return;
c = *(x+start);
*(x+start) = *(x+end);
*(x+end) = c;
rev(x, ++start, --end);
}
Output
Enter the string:
hello how are you
uoy era woh olleh
32
Set 2 Q6 Program to print the nth
term of a Fibonacci series using recursive
function
Program
#include <stdio.h>
int fibonacci(int a);
int main()
{
int n;
printf("Enter the nth number in fibonacci series: ");
scanf("%d", &n);
if (n < 0)
printf("Fibonacci of negative number is not possiblen");
else
printf("The %d number in fibonacci series is %d n", n, fibonacci(n));
return 0;
}
int fibonacci(int num)
{
if (num == 0)
return 0;
else if (num == 1)
return 1;
else
return(fibonacci(num - 1) + fibonacci(num - 2));
}
Output
Enter the nth number in fibonacci series: 5
The 5 number in fibonacci series is 5

Contenu connexe

Tendances

Program for pyramid
Program for pyramidProgram for pyramid
Program for pyramid
nayakq
 

Tendances (20)

C program to add n numbers
C program to add n numbers C program to add n numbers
C program to add n numbers
 
Implement a queue using two stacks.
Implement a queue using two stacks.Implement a queue using two stacks.
Implement a queue using two stacks.
 
Practical File of C Language
Practical File of C LanguagePractical File of C Language
Practical File of C Language
 
Program for pyramid
Program for pyramidProgram for pyramid
Program for pyramid
 
C Programming
C ProgrammingC Programming
C Programming
 
Data Structures Practical File
Data Structures Practical File Data Structures Practical File
Data Structures Practical File
 
Examples sandhiya class'
Examples sandhiya class'Examples sandhiya class'
Examples sandhiya class'
 
Printing different pyramid patterns of numbers,alphabets and stars using C.
Printing different pyramid patterns of numbers,alphabets and stars using C.Printing different pyramid patterns of numbers,alphabets and stars using C.
Printing different pyramid patterns of numbers,alphabets and stars using C.
 
Write a program to perform translation.
 Write a program to perform translation. Write a program to perform translation.
Write a program to perform translation.
 
Data Structures Using C Practical File
Data Structures Using C Practical File Data Structures Using C Practical File
Data Structures Using C Practical File
 
Insertion sort
Insertion sortInsertion sort
Insertion sort
 
C programming
C programmingC programming
C programming
 
Lessons learned from functional programming
Lessons learned from functional programmingLessons learned from functional programming
Lessons learned from functional programming
 
Concept of c
Concept of cConcept of c
Concept of c
 
week-4x
week-4xweek-4x
week-4x
 
SaraPIC
SaraPICSaraPIC
SaraPIC
 
Let us C (by yashvant Kanetkar) chapter 3 Solution
Let us C   (by yashvant Kanetkar) chapter 3 SolutionLet us C   (by yashvant Kanetkar) chapter 3 Solution
Let us C (by yashvant Kanetkar) chapter 3 Solution
 
week-21x
week-21xweek-21x
week-21x
 
Array
ArrayArray
Array
 
Pratik Bakane C++
Pratik Bakane C++Pratik Bakane C++
Pratik Bakane C++
 

Similaire à C programs Set 2

Chapter 8 c solution
Chapter 8 c solutionChapter 8 c solution
Chapter 8 c solution
Azhar Javed
 
C Prog - Pointers
C Prog - PointersC Prog - Pointers
C Prog - Pointers
vinay arora
 
C basics
C basicsC basics
C basics
MSc CST
 
Daapracticals 111105084852-phpapp02
Daapracticals 111105084852-phpapp02Daapracticals 111105084852-phpapp02
Daapracticals 111105084852-phpapp02
Er Ritu Aggarwal
 

Similaire à C programs Set 2 (20)

Chapter 8 c solution
Chapter 8 c solutionChapter 8 c solution
Chapter 8 c solution
 
Cpds lab
Cpds labCpds lab
Cpds lab
 
All important c programby makhan kumbhkar
All important c programby makhan kumbhkarAll important c programby makhan kumbhkar
All important c programby makhan kumbhkar
 
C Prog - Pointers
C Prog - PointersC Prog - Pointers
C Prog - Pointers
 
C programming function
C  programming functionC  programming function
C programming function
 
C lab programs
C lab programsC lab programs
C lab programs
 
C lab programs
C lab programsC lab programs
C lab programs
 
C Programming lab
C Programming labC Programming lab
C Programming lab
 
ADA FILE
ADA FILEADA FILE
ADA FILE
 
C lab
C labC lab
C lab
 
DSC program.pdf
DSC program.pdfDSC program.pdf
DSC program.pdf
 
'C' language notes (a.p)
'C' language notes (a.p)'C' language notes (a.p)
'C' language notes (a.p)
 
C Programming Exam problems & Solution by sazzad hossain
C Programming Exam problems & Solution by sazzad hossainC Programming Exam problems & Solution by sazzad hossain
C Programming Exam problems & Solution by sazzad hossain
 
C basics
C basicsC basics
C basics
 
Data Structure in C Programming Language
Data Structure in C Programming LanguageData Structure in C Programming Language
Data Structure in C Programming Language
 
Progr3
Progr3Progr3
Progr3
 
Basic c programs updated on 31.8.2020
Basic c programs updated on 31.8.2020Basic c programs updated on 31.8.2020
Basic c programs updated on 31.8.2020
 
C lab manaual
C lab manaualC lab manaual
C lab manaual
 
Data struture lab
Data struture labData struture lab
Data struture lab
 
Daapracticals 111105084852-phpapp02
Daapracticals 111105084852-phpapp02Daapracticals 111105084852-phpapp02
Daapracticals 111105084852-phpapp02
 

Plus de Koshy Geoji

73347633 milma-os
73347633 milma-os73347633 milma-os
73347633 milma-os
Koshy Geoji
 

Plus de Koshy Geoji (9)

Computer Graphics Report
Computer Graphics ReportComputer Graphics Report
Computer Graphics Report
 
C programs Set 4
C programs Set 4C programs Set 4
C programs Set 4
 
C programs Set 3
C programs Set 3C programs Set 3
C programs Set 3
 
C programs
C programsC programs
C programs
 
Vehicle detection in Aerial Images
Vehicle detection in Aerial ImagesVehicle detection in Aerial Images
Vehicle detection in Aerial Images
 
Text mining
Text miningText mining
Text mining
 
Hypothesis test based approach for change detection
Hypothesis test based approach for change detectionHypothesis test based approach for change detection
Hypothesis test based approach for change detection
 
Seminar report
Seminar reportSeminar report
Seminar report
 
73347633 milma-os
73347633 milma-os73347633 milma-os
73347633 milma-os
 

Dernier

Dernier (20)

Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
 

C programs Set 2

  • 1. 26 Set 2 Q1 Program to calculate the standard deviation of an array of values using function Program #include <stdio.h> #include <math.h> #include <stdlib.h> float calsd(float array[],int num); int main() { int i,n; float *a; printf("Enter number of element in the array "); scanf("%d", &n); a= (float*) malloc(n*sizeof(float)); printf("Enter the elements in the arrayn "); for(i=0; i < n; i++) scanf("%f", &a[i]); printf("nStandard Deviation = %.3f", calsd(a,n)); free (a); return 0; } float calsd(float array[],int num) { float sum = 0.0, mean, stddev = 0.0; int i; for(i=0; i<num; i++) { sum =sum + array[i]; } mean = sum/num;
  • 2. 27 for(i=0; i<num; ++i) { stddev = stddev + pow(array[i] - mean, 2); stddev = sqrt(stddev/num); } return stddev; } Output Enter number of element in the array 10 Enter the elements in the array 2 3 4 1 5 6 9 8 7 5 Standard Deviation = 0.266
  • 3. 28 Set 2 Q2 Program to find the factorial of a number using recursive function Program #include <stdio.h> long int fact(int n); int main() { int n; printf("Enter a positive integer: "); scanf("%d", &n); printf("Factorial of %d = %ldn",n, fact(n)); return 0; } long int fact(int n) { if (n >= 1) return n*fact(n-1); else return 1; } Output Enter a positive integer: 4 Factorial of 4 = 24
  • 4. 29 Set 2 Q3 Recursive function to find the sum of odd numbers between a given range Program #include <stdio.h> int sumodd(int n,int m); int main() { int n,m; printf("Enter the start and end range "); scanf("%d %d", &n,&m); if (n%2==0) n=n+1; printf("Sum of odd numbers between %d and %d is %dn",n,m, sumodd(n,m)); return 0; } int sumodd(int n, int m) { if (n<=m) { return n+sumodd(n+2,m); } else return 0; } Output Enter the start and end range 3 9 Sum of odd numbers between 3 and 9 is 24
  • 5. 30 Set 2 Q4 Program to find the GCD of two numbers using recursive function Program #include <stdio.h> int gcd(int a, int b); int main() { int a, b, g; printf("Enter the two numbers: "); scanf("%d %d", &a, &b); g = gcd(a, b); printf("The GCD of %d and %d is %d n", a, b, g); return 0; } int gcd(int a, int b) { while (a != b) { if (a > b) { return gcd(a - b, b); } else { return gcd(a, b - a); } } return a; } Output Enter the two numbers: 100 50 The GCD of 100 and 50 is 50
  • 6. 31 Set 2 Q5 Program to reverse a text using recursive function Program #include <stdio.h> #include <string.h> void rev(char*, int, int); int main() { char a[100]; printf("Enter the string: n"); gets(a); rev(a, 0, strlen(a)-1); printf("%sn",a); return 0; } void rev(char *x, int start, int end) { char c; if (start >= end) return; c = *(x+start); *(x+start) = *(x+end); *(x+end) = c; rev(x, ++start, --end); } Output Enter the string: hello how are you uoy era woh olleh
  • 7. 32 Set 2 Q6 Program to print the nth term of a Fibonacci series using recursive function Program #include <stdio.h> int fibonacci(int a); int main() { int n; printf("Enter the nth number in fibonacci series: "); scanf("%d", &n); if (n < 0) printf("Fibonacci of negative number is not possiblen"); else printf("The %d number in fibonacci series is %d n", n, fibonacci(n)); return 0; } int fibonacci(int num) { if (num == 0) return 0; else if (num == 1) return 1; else return(fibonacci(num - 1) + fibonacci(num - 2)); } Output Enter the nth number in fibonacci series: 5 The 5 number in fibonacci series is 5