0% found this document useful (0 votes)
10 views

C Program

Uploaded by

Mudavath Raju
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

C Program

Uploaded by

Mudavath Raju
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 9

Program to Check Leap Year

#include <stdio.h>
int main() {
int year;
printf("Enter a year: ");
scanf("%d", &year);

// leap year if perfectly divisible by 400


if (year % 400 == 0) {
printf("%d is a leap year.", year);
}
// not a leap year if divisible by 100
// but not divisible by 400
else if (year % 100 == 0) {
printf("%d is not a leap year.", year);
}
// leap year if not divisible by 100
// but divisible by 4
else if (year % 4 == 0) {
printf("%d is a leap year.", year);
}
// all other years are not leap years
else {
printf("%d is not a leap year.", year);
}

return 0;
}

Output 1

Enter a year: 1900


1900 is not a leap year.

Output 2

Enter a year: 2012


2012 is a leap year.
3.c

1#include<stdio.h>
2
3void main()
4{
5 int a,b,c;
6
7 printf("\n\nEnter three numbers=");
8 scanf("%d %d %d",&a,&b,&c);
9
1 if (a<b && a<c)
0 {
1 if(b<c)
1 {
1 printf("%d %d %d",a,b,c);
2 }
1 else
3 {
1 printf("%d %d %d",a,c,b);
4 }
1 }
5
1 else if (b<c && b<a)
6 {
1
7 if (c<a)
1 {
8 printf("%d %d %d",b,c,a);
1 }
9
2 else
0 {
2 printf("%d %d %d",b,a,c);
1 }
2
2 }
2
3 else if (c<a && c<b)
2 {
4
2 if (b<a)
5 {
2 printf("%d %d %d",c,b,a);
6 }
2
7 else
2 {
8 printf("%d %d %d",c,a,b);
2 }
9
3 }
0}
3
1
3
2
3
3
3
4
3
5
3
6
3
7
3
8
3
9
4
0
4
1
4
2
4
3
4
4
4
5
4
6
4
7
4
8
4
9
5
0
5
1

5.a

1. int main()
2. {
3. int n,sum=0,m;
4. printf("Enter a number:");
5. scanf("%d",&n);
6. while(n>0)
7. {
8. m=n%10;
9. sum=sum+m;
10. n=n/10;
11. }
12. printf("Sum is=%d",sum);
13. return 0;
14. }

6.c

#include <stdio.h>
2 void lcm_gcd(int ,int);
9
3 void main()
0{
3 int no1,no2;
1
3 printf("Enter two Numbers\n");
2 scanf("%d %d",&no1,&no2);
3 lcm_gcd(no1,no2);
3}
3
4 void lcm_gcd(int n1,int n2)
3{
5 int gcd,lcm,remainder,numerator,denominator;
3 if (n1>n2)
6 {
3 numerator=n1;
7 denominator=n2;
3 }
8 else
{
numerator=n2;
denominator=n1;
}
remainder=numerator%denominator;
while (remainder!=0)
{
numerator=denominator;
denominator=remainder;
remainder=numerator%denominator;
}
gcd = denominator;
lcm = n1*n2/gcd;

printf("GCD of %d and %d = %d\n",n1,n2,gcd);


printf("LCM of %d and %d = %d\n",n1,n2,lcm);
}

7.A

#include <stdio.h>

int main() {
int base, exponent, result = 1;

printf("Enter the Base & Exponent values:\n");


scanf("%d%d", &base, &exponent);

// It will finding the power of Base value


// by equiping Exponent value
for (int i = 1; i <= exponent; i++) {
result *= base;
}

printf("\nResult: %d^%d = %d\n", base, exponent, result);


return 0;
}

7.c

1. #include <stdio.h>
2. #include <conio.h>
3. void main()
4. {
5. int i, j, rows;
6. printf (" Enter a number to define the rows: \n ");
7. scanf("%d", &rows);
8. printf("\n");
9. for (i = 1; i <= rows; ++i) // outer loop
10. {
11. for (j = 1; j <= i; ++j) // inner loop
12. {
13. printf ("i "); // print the Star
14. }
15. printf ("\n");
16. }
17. getch();
18. }

7d.

1. #include < stdio.h >


2. long factorial(int);
3. int main()
4. {
5. int i, n, c;
6. printf("Enter the number of rows you wish to see
in pascal triangle\n");
7. scanf("%d", & n);
8. for (i = 0; i < n; i++) {
9. for (c = 0; c <= (n - i - 2); c++) printf("
");
10. for (c = 0; c <= i; c++) printf("%ld ",
factorial(i) / (factorial(c) * factorial(i - c)));
11. printf("\n");
12. }
13. return 0;
14. }
15. long factorial(int n) {
16. int c;
17. long result = 1;
18. for (c = 1; c <= n; c++) result = result *
c;
19. return result;
20. }

8a

/ C program to find the smallest and largest element in an array

#include<stdio.h>

int main()

int a[50],i,n,large,small;

printf(“\nEnter the number of elements : “);

scanf(“%d”,&n);

printf(“\nInput the array elements : “);

for(i=0;i<n;++i)

scanf(“%d”,&a[i]);

large=small=a[0];

for(i=1;i<n;++i)

if(a[i]>large)

large=a[i];

if(a[i]<small)

small=a[i];

}
printf(“\nThe smallest element is %d\n”,small);

printf(“\nThe largest element is %d\n”,large);

return 0;

OUTPUT
Enter the number of elements : 5

Input the array elements : 1 2 3 4 5

The smallest element is 1

The largest element is 5

8b

#include <stdio.h>
#include <math.h>
#define MAXSIZE 10

void main()
{
float x[MAXSIZE];
int i, n;
float average, variance, std_deviation, sum = 0, sum1 = 0;

printf("Enter the value of N \n");


scanf("%d", &n);
printf("Enter %d real numbers \n", n);
for (i = 0; i < n; i++)
{
scanf("%f", &x[i]);
}
/* Compute the sum of all elements */
for (i = 0; i < n; i++)
{
sum = sum + x[i];
}
average = sum / (float)n;
/* Compute variance and standard deviation */
for (i = 0; i < n; i++)
{
sum1 = sum1 + pow((x[i] - average), 2);
}
variance = sum1 / (float)n;
std_deviation = sqrt(variance);
printf("Average of all elements = %.2f\n", average);
printf("variance of all elements = %.2f\n", variance);
printf("Standard deviation = %.2f\n", std_deviation);
}

1b

#include<stdio.h>

#include<math.h>

int main()

int p,t;

float r,si,amount,ci;

printf("Please enter principal,time and rate of interest");

scanf("%d%d%f",&p,&t,&r);

si=p*t*r/100;

//Simple Interest formula is p*t*r

printf("\nSimple interest = %.3f",si);

//Compound Interest formula is below

amount=p*pow((1 +r/100),t);

ci=amount-p;

printf("\nCompound interest = %.3f",ci);

You might also like