0% found this document useful (0 votes)
9 views3 pages

C Programs Complete Collection

The document contains C programs demonstrating various programming concepts including calculating simple interest, factorial of a number, finding the largest of three numbers using nested if and ternary operator, calculating string length without built-in functions, 2D matrix addition, sum and standard deviation, and using structures for date of birth and average of marks. Each program includes sample output for clarity. These examples serve as practical applications of basic programming skills in C.

Uploaded by

tejashwini.r1909
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views3 pages

C Programs Complete Collection

The document contains C programs demonstrating various programming concepts including calculating simple interest, factorial of a number, finding the largest of three numbers using nested if and ternary operator, calculating string length without built-in functions, 2D matrix addition, sum and standard deviation, and using structures for date of birth and average of marks. Each program includes sample output for clarity. These examples serve as practical applications of basic programming skills in C.

Uploaded by

tejashwini.r1909
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Simple Interest

C Program:
#include <stdio.h>
int main() {
float p,r,t,si;
scanf("%f %f %f",&p,&r,&t);
si=(p*r*t)/100;
printf("Simple Interest = %.2f",si);
return 0;
}

Sample Output:
Simple Interest = 100.00

Factorial of a Number
C Program:
#include <stdio.h>
int main() {
int n,i; long long fact=1;
scanf("%d",&n);
for(i=1;i<=n;i++) fact*=i;
printf("Factorial = %lld",fact);
return 0;
}

Sample Output:
Factorial = 120

Largest of Three Numbers (Nested if)


C Program:
#include <stdio.h>
int main() {
int a,b,c;
scanf("%d %d %d",&a,&b,&c);
if(a>b){
if(a>c) printf("Largest = %d",a);
else printf("Largest = %d",c);
} else {
if(b>c) printf("Largest = %d",b);
else printf("Largest = %d",c);
}
return 0;
}

Sample Output:
Largest = 45

Largest of Three Numbers (Ternary Operator)


C Program:
#include <stdio.h>
int main(){
int a,b,c,largest;
scanf("%d %d %d",&a,&b,&c);
largest = (a>b)?((a>c)?a:c):((b>c)?b:c);
printf("Largest = %d",largest);
return 0;
}

Sample Output:
Largest = 25

String Length Without Built-in Function


C Program:
#include <stdio.h>
int main(){
char str[100]; int i=0,len=0;
gets(str);
while(str[i]!='\0'){ len++; i++; }
printf("Length = %d",len);
return 0;
}

Sample Output:
Length = 5

2D Matrix Addition
C Program:
#include <stdio.h>
int main(){
int a[2][2],b[2][2],s[2][2],i,j;
for(i=0;i<2;i++)
for(j=0;j<2;j++) scanf("%d",&a[i][j]);
for(i=0;i<2;i++)
for(j=0;j<2;j++) scanf("%d",&b[i][j]);
for(i=0;i<2;i++)
for(j=0;j<2;j++) s[i][j]=a[i][j]+b[i][j];
for(i=0;i<2;i++){
for(j=0;j<2;j++) printf("%d ",s[i][j]);
printf("\n");
}
return 0;
}

Sample Output:
6 8
10 12

Sum and Standard Deviation


C Program:
#include <stdio.h>
#include <math.h>
int main(){
int n,i; float x[10],sum=0,mean,var=0,sd;
scanf("%d",&n);
for(i=0;i<n;i++){ scanf("%f",&x[i]); sum+=x[i]; }
mean=sum/n;
for(i=0;i<n;i++) var+=(x[i]-mean)*(x[i]-mean);
sd=sqrt(var/n);
printf("Sum=%.2f SD=%.2f",sum,sd);
return 0;
}

Sample Output:
Sum=60.00 SD=8.16

Structure: Date of Birth


C Program:
#include <stdio.h>
struct DOB{
int day,month,year;
};
int main(){
struct DOB d;
scanf("%d %d %d",&[Link],&[Link],&[Link]);
printf("DOB = %d/%d/%d",[Link],[Link],[Link]);
return 0;
}

Sample Output:
DOB = 12/08/2003

Structure: Average of Marks


C Program:
#include <stdio.h>
struct Student{
int m1,m2,m3;
float avg;
};
int main(){
struct Student s;
scanf("%d %d %d",&s.m1,&s.m2,&s.m3);
[Link]=(s.m1+s.m2+s.m3)/3.0;
printf("Average = %.2f",[Link]);
return 0;
}

Sample Output:
Average = 80.00

You might also like