Documente Academic
Documente Profesional
Documente Cultură
#include
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>
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>
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);
/*
Program to Swap Values of Two Variables using Third Variable
*/
#include <stdio.h>
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>
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>
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();
}
CONTROL STATEMENTS
/*** Program to Find Largest of Three Numbers ***/
#include <stdio.h>
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>
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;
www.w3professors.com Gursharan Singh Tatla Page No. 2
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>
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>
main()
{
/*
Program to Print the Numbers, Which are Divisible by 3 and 5 from
First 100 Natural Numbers
*/
#include <stdio.h>
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();
}
pal = pal*10 + x;
}
if (pal == n)
printf("\n%d is palindrome", n);
else
printf("\n%d is not palindrome", n);
getch();
}
#include <stdio.h>
int fibbo(int, int, int, int);
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();
}
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 Value of sin(x) using Expansion Series Given Below:
if (i <= pwr)
goto ab;
printf("\n\nSum = %f", sum);
getch();
}
/*
Program to Print the Following Output:
1
121
12321
1234321
123454321
12345654321
1234567654321
123456787654321
12345678987654321
*/
#include <stdio.h>
main()
{
int i, j, k, space, n=9;
for (i=1; i<=n; i++)
{
}
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 Vowels in a String ***/
#include <stdio.h>
#include <string.h>
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 Count Number of Words and Number of Characters in a
String
*/
#include <stdio.h>
#include <string.h>
main()
{
char str[20];
int i=0, word=0, chr=0;
printf("\nEnter any string: ");
gets(str);
while (str[i] != '\0')
{
if (str[i] == ' ')
{
word++;
chr++;
}
else
chr++;
i++;
}
printf("\nNumber of characters: %d", chr);
printf("\nNumber of words: %d", word+1);
getch();
}
www.w3professors.com Gursharan Singh Tatla Page No. 1
ARRAYS
/*** Program to Print Transpose of a Matrix ***/
#include <stdio.h>
main()
{
int a[10][10], i, j, row, col;
printf("\nEnter no. of rows & columns: ");
scanf("%d %d", &row, &col);
printf("\nEnter elements of Matrix:\n");
for (i=0; i<row; i++)
for (j=0; j<col; j++)
scanf("%d", &a[i][j]);
printf("\n\nElements of Matrix:\n\n");
for (i=0; i<row; i++)
{
for (j=0; j<col; j++)
printf("\t%d", a[i][j]);
printf("\n\n");
}
printf("\n\nTranspose of Matrix:\n\n");
for (i=0; i<col; i++)
{
for (j=0; j<row; j++)
printf("\t%d", a[j][i]);
printf("\n\n");
}
getch();
}
/*** Program to Add Two Matrices ***/
#include <stdio.h>
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");
}
www.w3professors.com Gursharan Singh Tatla Page No. 2
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>
main()
{
int a[10][10], b[10][10], c[10][10], i, j, k, r1, r2, c1, c2;
back:
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]);
www.w3professors.com Gursharan Singh Tatla Page No. 2
}
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>
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);
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 Illustrate the Concept of Passing 2-D Array to Function
Program to Find Sum of Diagonal Elements of a Matrix
*/
#include <stdio.h>
#define ROW 10
#define COL 10
int diagonal_sum(int [][], int, int);
main()
{
int a[ROW][COL], row, col, i, j, sum;
printf("\nEnter no. of rows and columns of a matrix: ");
scanf("%d %d", &row, &col);
printf("\nEnter elements:\n");
for (i=0; i<row; i++)
for (j=0; j<col; j++)
scanf("%d", &a[i][j]);
printf("\nMatrix is:\n\n");
for (i=0; i<row; i++)
{
for (j=0; j<col; j++)
printf("\t%d", a[i][j]);
printf("\n\n");
}
{
string1[temp] = string2[i];
temp++;
}
string1[temp] = '\0';
printf("\nThe concatenated string is:\n");
puts(string1);
getch();
}
/*** Program to Compare Two Strings using strcmp() ***/
#include <stdio.h>
#include <string.h>
main()
{
char s1[20], s2[20];
int result;
printf("\nEnter first string: ");
gets(s1);
printf("\nEnter second string: ");
gets(s2);
result = strcmp(s1, s2);
if (result == 0)
printf("\nBoth strings are equal");
else
printf("\nBoth strings are not equal");
getch();
}
/** Program to Compare Two Strings Without using strcmp() **/
#include<stdio.h>
main()
{
char string1[5],string2[5];
int i,temp = 0;
printf("Enter the string1 value:\n");
gets(string1);
printf("\nEnter the String2 value:\n");
gets(string2);
for(i=0; string1[i]!='\0'; i++)
{
if(string1[i] == string2[i])
temp = 1;
else
temp = 0;
}
if(temp == 1)
printf("Both strings are same.");
else
printf("Both string not same.");
getch();
}
/*** Program to Copy String using strcpy() ***/
#include <stdio.h>
#include <string.h>
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>
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>
main()
{
char s1[20];
int len;
{
char name[20], ch = '\0';
int i=0;
printf("\nEnter your name: ");
while (ch != '\r')
{
ch = getche();
name[i] = ch;
i++;
}
printf("\nName: ");
for (i=0; name[i]!='\0'; i++)
putchar(name[i]);
getch();
}
/** Program to Input/Output Strings using gets() and puts() **/
#include <stdio.h>
main()
{
char name[20];
puts("\nEnter your name: ");
gets(name);
puts("\nName: ");
puts(name);
getch();
}
/** Program to Input/Output Strings using printf() and scanf() **/
#include <stdio.h>
main()
{
char name[20];
printf("\nEnter your name: ");
scanf("%s", name);
printf("\nName: %s", name);
getch();
}
/*** Program to Find Length of a String Without using strlen() ***/
#include <stdio.h>
main()
{
char str[20];
int i = 0;
flag = 1;
break;
}
i++;
j--;
}
if (flag == 0)
printf("\nString is palindrome");
else
printf("\nString is not palindrome");
getch();
}
FUNCTIONS
/**** Program to Show Call by Value ****/
#include <stdio.h>
swap (int, int);
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 *);
main()
{
int a, b;
#include <stdio.h>
int fibbo(int, int, int, int);
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();
}
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);
}
}
POINTERS
#include<stdio.h>
struct student
{
char name[20];
int rollno;
float marks;
};
struct student s1[3];
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);
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 *);
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);
}
SEARCHING AND SORTING
/* Program to Search an Element in the Array using Linear Search */
#include <stdio.h>
main()
{
int a[10], i, item;
printf("\nEnter elements of an array:\n");
for (i=0; i<=9; i++)
scanf("%d", &a[i]);
printf("\nEnter item to search: ");
scanf("%d", &item);
for (i=0; i<=9; i++)
if (item == a[i])
{
printf("\nItem found at location %d", i+1);
break;
}
if (i > 9)
printf("\nItem doesnot exist.");
getch();
}
/***** Program to Search an Array using Binary Search *****/
#include <stdio.h>
void binary_search();
int a[50], n, item, loc, beg, mid, end, i;
main()
{
printf("\nEnter size of an array: ");
scanf("%d", &n);
printf("\nEnter elements of an array in sorted form:\n");
for(i=0; i<n; i++)
scanf("%d", &a[i]);
printf("\nEnter ITEM to be searched: ");
scanf("%d", &item);
binary_search();
getch();
}
void binary_search()
{
beg = 0; end = n-1;
mid = (beg + end) / 2;
while ((beg<=end) && (a[mid]!=item))
{
if (item < a[mid])
end = mid - 1;
else
beg = mid + 1;
mid = (beg + end) / 2;
}
if (a[mid] == item)
printf("\n\nITEM found at location %d", mid+1);
else
printf("\n\nITEM doesn't exist");
}
/***** Program to Sort an Array using Bubble Sort *****/
#include <stdio.h>
void bubble_sort();
int a[50], n;
main()
{
int i;
printf("\nEnter size of an array: ");
scanf("%d", &n);
printf("\nEnter elements of an array:\n");
for(i=0; i<n; i++)
scanf("%d", &a[i]);
bubble_sort();
printf("\n\nAfter sorting:\n");
for(i=0; i<n; i++)
printf("\n%d", a[i]);
getch();
}
void bubble_sort()
{
int j, k, temp;
for(j=0; j<n; j++)
for(k=0; k<(n-1)-j; k++)
if(a[k] > a[k+1])
{
temp = a[k];
a[k] = a[k+1];
a[k+1] = temp;
}
}
/***** Program to Sort an Array using Selection Sort *****/
#include <stdio.h>
void selection_sort();
int a[50], n;
main()
{
int i;
printf("\nEnter size of an array: ");
scanf("%d", &n);
printf("\nEnter elements of an array:\n");
for(i=0; i<n; i++)
scanf("%d", &a[i]);
selection_sort();
printf("\n\nAfter sorting:\n");
for(i=0; i<n; i++)
printf("\n%d", a[i]);
getch();
}
void selection_sort()
{
int i, j, min, temp;
for (i=0; i<n; i++)
{
min = i;
for (j=i+1; j<n; j++)
{
if (a[j] < a[min])
min = j;
}
temp = a[i];
a[i] = a[min];
a[min] = temp;
}
}
FILE HANDLING
/*** Program to Write and Read a Character from a File ***/
#include <stdio.h>
main()
{
FILE *fptr;
char ch;
printf("\nEnter any character: ");
scanf("%c", &ch);
fptr = fopen("abc.txt", "w");
putc(ch, fptr);
fclose(fptr);
fptr = fopen("abc.txt", "r");
ch = getc(fptr);
printf("\nCharacter: %c", ch);
getch();
}
/*** Program to Write and Read an Integer from a File ***/
#include <stdio.h>
main()
{
FILE *fptr;
int a;
printf("\nEnter any number: ");
scanf("%d", &a);
fptr = fopen("abc.txt", "w");
putc(a, fptr);
fclose(fptr);
fptr = fopen("abc.txt", "r");
a = getc(fptr);
printf("\nCharacter: %d", a);
getch();
}
/*** Program to Write and Read a String from a File */
#include <stdio.h>
#define SIZE 50
main()
{
FILE *fptr;
char s1[SIZE], s2[SIZE];
printf("\nEnter any string: ");
gets(s1);
fptr = fopen("abc.txt", "w");
fputs(s1, fptr);
fclose(fptr);
fptr = fopen("abc.txt", "r");
fgets(s2, SIZE, fptr);
printf("\nYou entered:");
puts(s2);
getch();
}
/*** Program to Write Data into a File using fprintf() ***/
#include <stdio.h>
main()
{
FILE *fptr;
int rollno;
char name[20];
float marks;
printf("\nEnter roll no.: ");
scanf("%d", &rollno);
printf("\nEnter name: ");
fflush(stdin);
gets(name);
printf("\nEnter marks: ");
scanf("%f", &marks);
fptr = fopen("abc.txt", "w");
fprintf(fptr, "%d %s %f", rollno, name, marks);
fclose(fptr);
getch();
}
/*** Program to Read Data from File using fscanf() ***/
#include <stdio.h>
main()
{
FILE *fptr;
int rollno;
char name[20];
float marks;