You are on page 1of 21

Program to Find Sum and Average of Three Real Numbers

#include <stdio.h> void main() { float a, b, c, sum, avg; printf("\nEnter value of three numbers: "); scanf("%f %f %f", &a, &b, &c); sum = a + b + c; avg = sum / 3; printf("\nSum = %.2f", sum); printf("\nAverage = %.2f", avg); getch(); }

Program to Find Area of Square & Circumference of a Circle


#include <stdio.h> #define PI 3.142 void main() { float len, r, area, circum; printf("\nEnter length of a square: "); scanf("%f", &len); area = len * len; printf("\nEnter radius of a circle: "); scanf("%f", &r); circum = 2 * PI * r; printf("\nArea of square = %.2f", area); printf("\nCircumference of circle = %.2f", circum); getch(); }

Program to find Sphere Surface Area and Volume of a Sphere


Sphere Surface Area = 4 * PI * r * r Volume of Sphere = (4/3) * PI * r * r * r #include <stdio.h> #define PI 3.142 void main() { float r, area, vol; printf("\nEnter radius of Sphere: "); scanf("%f", &r); area = 4 * PI * r * r; vol = (4/3) * PI * r * r * r;

printf("\nSphere Surface Area = %.2f", area); printf("\nVolume of Sphere = %.2f", vol); getch(); }

Program to Find Area of a Triangle using Hero s Formula


#include <stdio.h> #include <math.h> void main() { float a, b, c, s, area; back: printf("\nEnter three sides of a triangle: "); scanf("%f %f %f", &a, &b, &c); if (a==0 || b==0 || c==0) { printf("\nValue of any side should not be equal to zero\n"); goto back; } if (a+b<c || b+c<a || c+a<b) { printf("\nSum of two sides should not be less than third\n"); goto back; } s = (a + b + c) / 2; area = sqrt(s * (s - a) * (s - b) * (s - c)); printf("\n\nArea of triangle: %.2f", area); getch(); }

Program to find Simple Interest and Compound Interest


SI = (p * r * t) / 100 CI = p * pow((1 + r/100), t) p #include <stdio.h> #include <math.h> void main() { float p, r, t, si, ci; printf("\nEnter priciple, rate and time: "); scanf("%f %f %f", &p, &r, &t); si = (p * r * t) / 100; ci = p * pow((1 + r/100), t) - p; printf("\n\nSimple Interest: %.2f", si); printf("\n\nCompound Interest: %.2f", ci); getch();

Program to Convert Temperature from Degree Centigrade to Fahrenheit


f = (1.8*c) + 32 #include <stdio.h> void main() { float c, f; printf("\nEnter temperature in degree Centigrade: "); scanf("%f", &c); f = (1.8*c) + 32; printf("\n\nTemperature in degree Fahrenheit: %.2f", f); getch(); }

Program to Convert Time in Seconds to Hours, Minutes and Seconds


#include <stdio.h> void main() { long sec, hr, min, t; printf("\nEnter time in seconds: "); scanf("%ld", &sec); hr = sec/3600; t = sec%3600; min = t/60; sec = t%60; printf("\n\nTime is %ld hrs %ld mins %ld secs", hr, min, sec); getch(); }

Program to Swap Values of Two Variables using Third Variable


#include <stdio.h> void main() { int a, b, temp; printf("\nEnter any two numbers: "); scanf("%d %d", &a, &b); printf("\n\nBefore Swapping:\n"); printf("\na = %d\n", a); printf("\nb = %d\n", b); temp = a; a = b; b = temp;

printf("\n\nAfter Swapping:\n"); printf("\na = %d\n", a); printf("\nb = %d\n", b); getch(); }

Program to Swap Values of Two Variables Without using 3rd Variable


#include <stdio.h> void main() { int a, b, temp; printf("\nEnter any two numbers: "); scanf("%d %d", &a, &b); printf("\n\nBefore Swapping:\n"); printf("\na = %d\n", a); printf("\nb = %d\n", b); a = a + b; b = a - b; a = a - b; printf("\n\nAfter Swapping:\n"); printf("\na = %d\n", a); printf("\nb = %d\n", b); getch(); } Basic salary of an employee is input through the keyboard. The DA is 25% of the basic salary while the HRA is 15% of the basic salary. Provident Fund is deducted at the rate of 10% of the gross salary(BS+DA+HRA).

Program to Calculate the Net Salary.


#include <stdio.h> void main() { float basic_sal, da, hra, pf, gross_sal, net_sal; printf("\nEnter basic salary of the employee: Rs. "); scanf("%f", &basic_sal); da = (basic_sal * 25)/100; hra = (basic_sal * 15)/100; gross_sal = basic_sal + da + hra; pf = (gross_sal * 10)/100; net_sal = gross_sal - pf; printf("\n\nNet Salary: Rs. %.2f", net_sal); getch(); }

Program to Find Largest of Three Numbers


#include <stdio.h> void main() { int a, b, c; printf("\nEnter three numbers: "); scanf("%d %d %d", &a, &b, &c); if (a>b && a>c) printf("\n\n%d is greater", a); else if (b>a && b>c) printf("\n\n%d is greater", b); else printf("\n\n%d is greater", c); getch(); }

Program to Check Whether a Character is a Vowel or not by using switch Statement


#include <stdio.h> void main() { char ch; printf("\nEnter any character: "); scanf("%c", &ch); switch (ch) { case 'a': case 'A': printf("\n\n%c is a vowel", ch); break; case 'e': case 'E': printf("\n\n%c is a vowel", ch); break; case 'i': case 'I': printf("\n\n%c is a vowel", ch); break; case 'o': case 'O': printf("\n\n%c is a vowel", ch); break; case 'u':

case 'U': printf("\n\n%c is a vowel", ch); break; default: printf("\n\n%c is not a vowel", ch); } getch(); }

Program to Find the Sum of First 100 Positive Integers


#include <stdio.h> void main() { int i, sum=0; printf("\n\tSum of first 100 positive numbers\n"); for(i=0; i<=100; i++) sum = sum + i; printf("\nSum = %d", sum); getch(); }

Program to Find the Sum of Even and Odd Numbers from First 100 Positive Integers
#include <stdio.h> void main() { int i, sumEven=0, sumOdd=0; for (i=0; i<=100; i++) if (i%2 == 0) sumEven = sumEven + i; else sumOdd = sumOdd + i; printf("\nSum of first even 100 numbers: %d\n", sumEven); printf("\nSum of first odd 100 numbers: %d\n", sumOdd); getch(); }

Program to Find the Sum of Digits of a Positive Integer


#include <stdio.h> void main() { long n; int digit, sum = 0;

printf("\nEnter any number: "); scanf("%d", &n); while (n > 0) { digit = n%10; n = n/10; sum = sum + digit; } printf("\n\nSum of digits: %d", sum); getch(); }

Program to Reverse a Given Number


#include <stdio.h> void main() { long n; int rev; printf("\nEnter any number: "); scanf("%ld", &n); printf("\nReverse no. is:\n\n"); while (n > 0) { rev = n % 10; n = n / 10; printf("%d", rev); } getch(); }

Program to Print First N Prime Numbers


#include <stdio.h> void main() { int i, j, n; printf("\nEnter how many prime numbers you want to print: "); scanf("%d", &n); printf("\n2"); for (i=2; i<=n; i++) for (j=2; j<=i; j++) { if(i%j == 0) break; else

{ printf("\n%d", i); break; } } getch(); }

Program to Print a Table of any Number


#include <stdio.h> void main() { int n, mul, i; printf("\nEnter any no.: "); scanf("%d", &n); for(i=1; i<=10; i++) { mul = n*i; printf("\n\n%d\tx\t%d\t=\t%d", n, i, mul); } getch(); }

Program to Check Whether the Given Number is an Armstrong Number


#include <stdio.h> void main() { int n, temp, d, arm=0; printf("\nEnter any number: "); scanf("%d", &n); temp = n; while (temp > 0) { d = temp%10; temp = temp/10; arm = arm + (d*d*d); } if (arm == n) printf("\n\n%d is an Armstrong number\n", n); else printf("\n\n%d is not an Armstrong number\n", n); getch(); }

Program to Print the Numbers, Which are Divisible by 3 and 5 from First 100 Natural Numbers
#include <stdio.h> void main() { int i; printf("\nFirst 100 numbers which are divisible by 3 and 5\n\n"); for (i=1; i<=100; i++) if (i%3==0 && i%5==0) printf("\t%d", i); getch(); }

Program to Find Whether a Number is Palindrome or Not


#include <stdio.h> void main() { int n, pal=0, temp, x; printf("\nEnter any number: "); scanf("%d", &n); temp = n; while (temp > 0) { x = temp%10; temp = temp/10; pal = pal*10 + x; } if (pal == n) printf("\n%d is palindrome", n); else printf("\n%d is not palindrome", n); getch(); }

Program to Find Factorial of a Number without using Recursion


#include <stdio.h> void main() { int n, i; long fact=1; printf("\nEnter any number: "); scanf("%d", &n); for (i=1; i<=n; i++)

fact = fact*i; printf("\nFactorial = %ld", fact); getch(); }

Program to Print Fibonacci Series without Recursion


#include <stdio.h> void main() { int x, y, z, n, i; x=0; y=1; printf("\nEnter value of n: "); scanf("%d", &n); printf("\nFibonacci Series:\n\n"); printf("\n%d", x); printf("\t%d", y); for( i=0; i<n-2; i++) { z = x+y; printf("\t%d", z); x = y; y = z; } getch(); }

Program to Print Fibonacci Series using Recursion


#include <stdio.h> int fibbo(int, int, int, int); void main() { int n, f, x=0, y=1, i=3; printf("\nEnter value of n: "); scanf("%d", &n); printf("\n%d\t%d", x, y); fibbo(x, y, n, i); getch(); } int fibbo(int x, int y, int n, int i) { int z; if (i <= n) { z = x + y;

printf("\t%d", z); x = y; y = z; i++; fibbo(x,y,n,i); } }

Program to Find HCF of Two Numbers using Recursion


#include <stdio.h> int hcf(int, int); void main() { int h, i, a, b; printf("\nEnter values of two numbers: "); scanf("%d %d", &a, &b); h = hcf(a, b); printf("\nHCF of numbers is: %d", h); getch(); } int hcf(int a, int b) { if (a%b == 0) return b; else return hcf(b, a%b); }

Program to Find HCF of Two Numbers Without Recursion


#include <stdio.h> void main() { int q, m, n, temp; printf("\nEnter values of two numbers: "); scanf("%d %d", &m, &n); if (m == 0) { printf("\nHCF of number is: %d", n); goto end; } if (n == 0) { printf("\nHCF of numbers is: %d", m); goto end;

} if (n > m) { temp = m; m = n; n = temp; } q = 1; while (q != 0) { q = m % n; if (q != 0) { m = n; n = q; } } printf("\nHCF of number is: %d", n); end: getch(); }

Program to Find the number of Vowels in a String


#include <stdio.h> #include <string.h> void main() { char str[20]; int count=0, i=0; printf("\nEnter any string: "); gets(str); while (str[i] != '\0') { if (str[i]=='a' || str[i]=='e' || str[i]=='i' || str[i]=='o' || str[i]=='u') count++; i++; } printf("\nNo. of vowels: %d", count); getch(); }

Program to Add Two Matrices


#include <stdio.h> void main() { int a[10][10], b[10][10], c[10][10], i, j, row, col; printf("\nEnter number of rows and columns: "); scanf("%d %d", &row, &col); printf("\nEnter elements of Array A:\n"); for (i=0; i<row; i++) for (j=0; j<col; j++) scanf("%d", &a[i][j]); printf("\nEnter elements of Array B:\n"); for (i=0; i<row; i++) for (j=0; j<col; j++) scanf("%d", &b[i][j]); printf("\nElements of Matrix A:\n\n"); for (i=0; i<row; i++) { for (j=0; j<col; j++) printf("\t%d", a[i][j]); printf("\n\n"); } printf("\nElements of Matrix B:\n\n"); for (i=0; i<row; i++) { for (j=0; j<col; j++) printf("\t%d", b[i][j]); printf("\n\n"); } for (i=0; i<row; i++) for (j=0; j<col; j++) c[i][j] = a[i][j] + b[i][j]; printf("\nMatrix Addition is:\n\n"); for (i=0; i<row; i++) { for (j=0; j<col; j++) printf("\t%d", c[i][j]); printf("\n"); } getch(); }

Program to multiply two matrices. The order and the elements of the two matrices
will be entered by the user as input to the program and if multiplication is not possible then it should be reported to the user #include <stdio.h> void main() { int a[10][10], b[10][10], c[10][10], i, j, k, r1, r2, c1, c2; printf("\nEnter no. of rows and columns of Matrix A: "); scanf("%d %d", &r1, &c1); printf("\nEnter no. of rows and columns of Matrix B: "); scanf("%d %d", &r2, &c2); if (c1 != r2) { printf("\n\nMultiplication is not possible\n"); goto back; } printf("\n\nEnter elements of Matrix A:\n"); for (i=0; i<r1; i++) for (j=0; j<c1; j++) scanf("%d", &a[i][j]); printf("\nEnter elements of Matrix B:\n"); for (i=0; i<r2; i++) for (j=0; j<c2; j++) scanf("%d", &b[i][j]); printf("\n\nElements of Matrix A:\n\n"); for (i=0; i<r1; i++) { for (j=0; j<c1; j++) printf("\t%d", a[i][j]); printf("\n\n"); } printf("\n\nElements of Matrix B:\n"); for (i=0; i<r2; i++) { for (j=0; j<c2; j++) printf("\t%d", b[i][j]); printf("\n\n"); } for (i=0; i<r1; i++) for (j=0; j<c2; j++) { c[i][j] = 0; for (k=0; k<r2; k++) c[i][j] = c[i][j] + a[i][k] * b[k][j]; }

printf("\n\nMultiplication of Matrices:\n\n"); for (i=0; i<r1; i++) { for (j=0; j<c2; j++) printf("\t%d", c[i][j]); printf("\n\n"); } getch(); }

Program to Find Smallest Among N Numbers


#include <stdio.h> void main() { int a[10], i, small; printf("\nEnter elements of an array:\n"); for (i=0; i<=9; i++) scanf("%d", &a[i]); small = a[0]; for (i=0; i<=9; i++) if (a[i] < small) small = a[i]; printf("\nSmallest number is %d", small); getch(); } Program to Illustrate the Concept of Passing 1-D Array to Function

Program to Find Largest from an Array


#include <stdio.h> #define SIZE 50 int big(int [], int); void main() { int a[SIZE], n, i, b; printf("\nEnter size of array: "); scanf("%d", &n); printf("\nEnter elements:\n"); for (i=0; i<n; i++) scanf("%d", &a[i]); b = big(a, n); printf("\nLargest number: %d", b); getch(); }

int big(int a[], int n) { int b, i; b = a[0]; for (i=0; i<n; i++) if (a[i] > b) b = a[i]; return b; }

Program to Concatenate Two Strings using strcat()


#include <stdio.h> #include <string.h> void main() { char s1[20], s2[20]; printf("\nEnter first string: "); gets(s1); printf("\nEnter second string: "); gets(s2); strcat(s1, s2); printf("\nThe concatenated string is: %s", s1); getch(); }

Program to Concatenate Two Strings without using strcat()


#include <stdio.h> #include <conio.h> #include <string.h> void main() { char string1[30], string2[20]; int i, length=0, temp; printf("Enter the Value of String1: \n"); gets(string1); printf("\nEnter the Value of String2: \n"); gets(string2); for(i=0; string1[i]!='\0'; i++) length++; temp = length; for(i=0; string2[i]!='\0'; i++) { string1[temp] = string2[i];

temp++; } string1[temp] = '\0'; printf("\nThe concatenated string is:\n"); puts(string1); getch(); }

Program to Copy String using strcpy()


#include <stdio.h> #include <string.h> void main() { char s1[20], s2[20]; printf("\nEnter string into s1: "); gets(s1); strcpy(s2, s1); printf("\ns2: %s", s2); getch(); }

Program to Copy one String to Another Without Using strcpy()


#include <stdio.h> #include <conio.h> #include <string.h> void main() { char string1[20], string2[20]; int i; printf("Enter the value of STRING1: \n"); gets(string1); for(i=0; string1[i]!='\0'; i++) string2[i]=string1[i]; string2[i]='\0'; printf("\nThe value of STRING2 is:\n"); puts(string2); getch(); }

Program to Find Length of a String using strlen()


#include <stdio.h> #include <string.h> void main()

{ char s1[20]; int len; printf("\nEnter any string: "); gets(s1); len = strlen(s1); printf("\nLength of string: %d", len); getch(); }

Program to Find Whether a String is Palindrome or Not


#include <stdio.h> #include <string.h> void main() { char s1[20], s2[20]; int result; printf("\nEnter any string: "); gets(s1); strcpy(s2, s1); strrev(s2); result = strcmp(s1, s2); if(result == 0) printf("\nIt is a palindrome string"); else printf("\nIt is not a palindrome string"); getch(); }

Program to Show Call by Value


#include <stdio.h> swap (int, int); void main() { int a, b; printf("\nEnter value of a & b: "); scanf("%d %d", &a, &b); printf("\nBefore Swapping:\n"); printf("\na = %d\n\nb = %d\n", a, b); swap(a, b); printf("\nAfter Swapping:\n"); printf("\na = %d\n\nb = %d", a, b); getch(); }

swap (int a, int b) { int temp; temp = a; a = b; b = temp; }

Program to Show Call by Reference


#include <stdio.h> swap (int *, int *); void main() { int a, b; printf("\nEnter value of a & b: "); scanf("%d %d", &a, &b); printf("\nBefore Swapping:\n"); printf("\na = %d\n\nb = %d\n", a, b); swap(&a, &b); printf("\nAfter Swapping:\n"); printf("\na = %d\n\nb = %d", a, b); getch(); } swap (int *x, int *y) { int temp; temp = *x; *x = *y; *y = temp; }

Program to Illustrate the Concept of Pointers


#include <stdio.h> void main() { int a = 10; int *p; p = &a; printf("\nAddress of a: %u", &a); printf("\n\nAddress of a: %u", p); printf("\n\nAddress of p: %u", &p); printf("\n\nValue of p: %d", p); printf("\n\nValue of a: %d", a); printf("\n\nValue of a: %d", *(&a));

printf("\n\nValue of a: %d", *p); getch(); }

Program to Implement Structure


#include<stdio.h> struct student { char name[20]; int rollno; float marks; }; void main() { struct student s1 = {"abc", 1, 450}; struct student s2; printf("Enter student Name, Rollno, Marks:\n"); scanf("%s%i%f", &s2.name, &s2.rollno, &s2.marks); printf("\nStudent Name\tRollno\tMarks\n"); printf("%s\t%i\t%f", s1.name, s1.rollno, s1.marks); printf("\n"); printf("%s\t%i\t%f",s2.name,s2.rollno,s2.marks); getch(); }

Program to Implement Structure with Array


#include<stdio.h> struct student { char name[20]; int rollno; float marks; }; struct student s1[3]; void main() { int i; printf("Enter Name, RollNo, Marks:\n"); for(i=0; i<=2; i++) scanf("%s %d %f",&s1[i].name,&s1[i].rollno,&s1[i].marks); printf("Student Name\tStudent Rollno\tStudent Marks:"); for(i=0; i<=2; i++) printf("\n%s\t\t%d\t\t%f", s1[i].name, s1[i].rollno, s1[i].marks);

getch(); }

Program to Implement Structure with Function


#include<stdio.h> struct student { char name; int rollno; float marks; }; struct student std_func(char, int, float); void main() { struct student s1 = {'x', 888, 450}; std_func(s1.name, s1.rollno, s1.marks); getch(); } struct student std_func(char name, int rollno, float marks) { printf("Name\tRoll No.\tMarks\n"); printf("%c\t%d\t\t%f", name, rollno, marks); }

Program to Implement Structure with Pointers


#include<stdio.h> struct student { char name[20]; int rollno; float marks; }; void show(struct student *); void main() { struct student s1 = {"xyz", 1, 450}; show(&s1); getch(); } void show(struct student *ptr) { printf("Student name\tRollno\tMarks\n"); printf("%s\t\t%i\t%f",ptr->name,ptr->rollno,ptr->marks); }

You might also like