C Programming Lab
C Programming Lab
C Programming Lab
Link: http://www.c4learn.com/c-programs/
1. C Program to find sum of two numbers
#include<stdio.h>
int main()
{
int a, b, sum;
printf("\nEnter two no: ");
scanf("%d %d", &a, &b);
sum = a + b;
printf("Sum : %d", sum);
return(0);
}
return(0);
}
#include<stdio.h>
int main()
{
int rad;
float PI = 3.14, area, ci;
return (0);
}
int main() {
int s1, s2, angle;
float area;
int main() {
int side, area;
printf("\nEnter the Length of Side : ");
scanf("%d", &side);
return (0);
}
return (0);
}
#include<stdio.h>
#define ACCURACY 0.0001
int main() {
int n, count;
float x, term, sum;
printf("\nEnter value of x :");
scanf("%f", &x);
n = term = sum = count = 1;
while (n <= 100)
{
term = term * x / n;
sum = sum + term;
count = count + 1;
void main() {
int x, y, z;
float a, b, c;
clrscr();
a = (x + y + z) / (x - y - z);
b = (x + y + z) / 3;
c = (x + y) * (x - y) * (y - z);
printf("\nValue of a = %f",a);
printf("\nValue of b = %f",b);
printf("\nValue of c = %f",c);
getch();
}
#include<stdio.h>
#include<conio.h>
void main() {
intcust_no, powerUsage;
float amount;
clrscr();
getch();
}
int main() {
intgross_salary, basic, da, ta;
int main() {
intnum, rem, rev = 0;
while (num>= 1) {
rem = num % 10;
rev = rev * 10 + rem;
num = num / 10;
}
printf("\nReversed Number : %d", rev);
return (0);
}
#include<stdio.h>
int main() {
floatcelsius, fahrenheit;
return (0);
}
Calender Program in C Programming Language :
14, Program will accept Year,Month and Date
from the user and will display the day of the
month.
#include<stdio.h>
#include<conio.h>
#include<math.h>
//bring it in range of 0 to 6
fmonth = fmonth % 7;
returnfmonth;
}
//----------------------------------------------
intday_of_week(int date, int month, int year) {
intdayOfWeek;
int YY = year % 100;
int century = year / 100;
//remainder on division by 7
dayOfWeek = dayOfWeek % 7;
switch (dayOfWeek) {
case 0:
printf("weekday = Saturday");
break;
case 1:
printf("weekday = Sunday");
break;
case 2:
printf("weekday = Monday");
break;
case 3:
printf("weekday = Tuesday");
break;
case 4:
printf("weekday = Wednesday");
break;
case 5:
printf("weekday = Thursday");
break;
case 6:
printf("weekday = Friday");
break;
default:
printf("Incorrect data");
}
return 0;
}
//------------------------------------------
int main() {
int date, month, year;
int main() {
int a, b;
a = a + b;
b = a - b;
a = a - b;
return (0);
}
16 Sample program of pre increment and post increment
program in c
#include<stdio.h>
void main()
{
inta,b,x=10,y=10;
a = x++;
b = ++y;
printf("Value of a : %d",a);
printf("Value of b : %d",b);
}
#include<stdio.h>
void main()
{
inta,b,x=10,y=10;
a = x--;
b = --y;
printf("Value of a : %d",a);
printf("Value of b : %d",b);
}
#include<stdio.h>
int main(){
intnum;
printf("Enter an integer you want to check: ");
scanf("%d",&num);
if((num%2)==0) /* Checking whether remainder is 0 or
not. */
printf("%d is even.",num);
else
printf("%d is odd.",num);
return0;
}
#include<stdio.h>
int main(){
intnum;
printf("Enter an integer you want to check: ");
scanf("%d",&num);
((num%2)==0) ? printf("%d is even.",num) : printf("%d
is odd.",num);
return0;
}
19.C Program to Check Vowel or Consonant
#include <stdio.h>
int main(){
char c;
printf("Enter an alphabet: ");
scanf("%c",&c);
if(c=='a'||c=='A'||c=='e'||c=='E'||c=='i'||c=='I'||c=='o'||c
=='O'||c=='u'||c=='U')
printf("%c is a vowel.",c);
else
printf("%c is a consonant.",c);
return 0;
}
#include <stdio.h>
int main()
{
int n, count, sum=0;
printf("Enter an integer: ");
scanf("%d",&n);
count=1;
while(count<=n) /* while loop terminates if count>n */
{
sum+=count; /* sum=sum+count */
++count;
}
printf("Sum = %d",sum);
return 0;
}
21 ,C program to Generate Multiplication Table
#include <stdio.h>
int main()
{
int n, i;
printf("Enter an integer to find multiplication table: ");
scanf("%d",&n);
for(i=1;i<=10;++i)
{
printf("%d * %d = %d\n", n, i, n*i);
}
return 0;
}
#include <stdio.h>
int main()
{
int num1, num2, i, hcf;
printf("Enter two integers: ");
scanf("%d %d", &num1, &num2);
for(i=1; i<=num1 || i<=num2; ++i)
{
if(num1%i==0 && num2%i==0) /* Checking whether i is
a factor of both number */
hcf=i;
}
printf("H.C.F of %d and %d is %d", num1, num2, hcf);
return 0;
}
23,C Program to Check Whether a Number is Prime or Not
#include <stdio.h>
int main()
{
int n, i, flag=0;
printf("Enter a positive integer: ");
scanf("%d",&n);
for(i=2;i<=n/2;++i)
{
if(n%i==0)
{
flag=1;
break;
}
}
if (flag==0)
printf("%d is a prime number.",n);
else
printf("%d is not a prime number.",n);
return 0;
}
# include <stdio.h>
int main()
{
char o;
float num1,num2;
printf("Enter operator either + or - or * or divide : ");
scanf("%c",&o);
printf("Enter two operands: ");
scanf("%f%f",&num1,&num2);
switch(o) {
case '+':
printf("%.1f + %.1f = %.1f",num1, num2, num1+num2);
break;
case '-':
printf("%.1f - %.1f = %.1f",num1, num2, num1-num2);
break;
case '*':
printf("%.1f * %.1f = %.1f",num1, num2, num1*num2);
break;
case '/':
printf("%.1f / %.1f = %.1f",num1, num2, num1/num2);
break;
default:
/* If operator is other than +, -, * or /, error
message is shown */
printf("Error! operator is not correct");
break;
}
return 0;
}
Programs on Arrays
1.C Program to Delete an element from the specified
location from Array
#include<stdio.h>
int main() {
int i, arr[50], num;
return (0);
}
int main() {
int arr1[30], arr2[30], res[60];
int i, j, k, n1, n2;
i = 0;
j = 0;
k = 0;
// Merging starts
while (i < n1 && j < n2) {
if (arr1[i] <= arr2[j]) {
res[k] = arr1[i];
i++;
k++;
} else {
res[k] = arr2[j];
k++;
j++;
}
}
int main()
{
int c, first, last, middle, n,
search, array[100];
printf("Enter number of
elements\n");
scanf("%d",&n);
first = 0;
last = n - 1;
middle = (first+last)/2;
return 0;
}
Linear search c program
#include <stdio.h>
int main()
{
int array[100], search, c, n;
return 0;
}
3.C Program to Reversing an Array Elements in C
Programming
#include<stdio.h>
int main() {
int arr[30], i, j, num, temp;
while (i < j) {
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
i++; // increment i
j--; // decrement j
}
return (0);
}
The following program initializes an integer
array with five values and prints the array
in.
#include <stdio.h>
#include <conio.h>
int main()
{
int numbers[]={1,2,3,4,5};
int i;
clrscr();
printf("Array elements are\n");
for(i=0;i<=4;i++)
printf("%d\n",numbers[i]);
getch();
return 0;
}
String Input and Output
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char str[20];
clrscr();
printf("Enter a string");
scanf("%[^\n]",&str);
printf("%s",str);
getch();
}
Accessing Two-Dimensional Array Elements:
#include <stdio.h>
int main ()
{
/* an array with 5 rows and 2 columns*/
int a[5][2] = { {0,0}, {1,2}, {2,4},
{3,6},{4,8}};
int i, j;
#include <string.h>
main()
{ char s1[20], s2[20],
s3[20];
int x, l1, l2, l3;
printf("\n\nEnter two string
constants \n");
printf("?");
scanf("%s %s", s1, s2);
/* comparing s1 and s2 */
x = strcmp(s1, s2);
if(x != 0)
{ printf("\n\nStrings are
not equal \n");
strcat(s1, s2); /*
joining s1 and s2 */
}
else
printf("\n\nStrings are
equal \n");
/* copying s1 to s3
strcpy(s3, s1);
/* Finding length of strings */
l1 = strlen(s1);
l2 = strlen(s2);
l3 = strlen(s3);
/* output */
#include <stdio.h>
void main() {
char fname[30];
char lname[30];
printf("Type first name:\n");
scanf("%s", fname);
#include<stdio.h>
int main() {
int a[30], i, num, largest;
return (0);
}
5.Program : Find Smallest Element in Array in C
Programming
#include<stdio.h>
int main() {
int a[30], i, num, smallest;
return (0);
}
int main() {
int arr[20], i, j, k, size;
return (0);
}
#include<stdio.h>
#include<conio.h>
#define MAX 30
void main() {
int size, i, arr[MAX];
int *ptr;
clrscr();
ptr = &arr[0];
int string_ln(char*);
void main() {
char str[20];
int length;
clrscr();
length = string_ln(str);
printf("The length of the given string %s is : %d", str,
length);
getch();
}
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#include<conio.h>
void main() {
int nob, now, nod, nov, nos, pos = high;
char *str;
nob = now = nod = nov = nos = 0;
clrscr();
printf("Enter any string : ");
gets(str);
getch();
}
#include <stdio.h>
int main(){
int marks[10],i,n,sum=0;
printf("Enter number of students: ");
scanf("%d",&n);
for(i=0;i<n;++i){
printf("Enter marks of student%d: ",i+1);
scanf("%d",&marks[i]);
sum+=marks[i];
}
printf("Sum= %d",sum);
return 0;
}
Reference operator(&)
/* Example to demonstrate use of reference operator in C
programming. */
#include <stdio.h>
int main(){
int var=5;
printf("Value: %d\n",var);
printf("Address: %d",&var); //Notice, the ampersand(&)
before var.
return 0;
}
void main()
{
int i, j, a[3][4] = {1,2,3,4,5,6,7,8,9,10,11,12};
for(i = 2; i >= 0; i = i - 1)
{
for(j = 3; j >= 0; j = j - 1)
printf("a[%d][%d] = %d\t", i, j, a[i][j]);
printf("\n");
}
}
In the following experiment, the first loop scans characters
into a character array and the second prints them. For
both loops, i stays fixed while j varies or the row stays fixed
while the column varies. Hence, the data is read in the
array row-wise and printed out row-wise. Enter this
data: a, b, c, d, e, f, g, h, i, j, k, l. Remember to put a space
preceding the %c in the scanf_s(). Show the output and
answer the questions.
#include <stdio.h>
void main()
{
int i, j;
char a[3][4];
for(i = 0; i <= 2; i = i + 1)
for(j = 0; j <= 3; j = j + 1)
// scanf(" %c", &a[i][j]);
scanf_s(" %c", &a[i][j], sizeof(a));
for(i = 0; i <= 2; i = i + 1)
{
for(j = 0; j <= 3; j = j + 1)
printf("a[%d][%d] = %c\t", i, j, a[i][j]);
printf("\n");
}
}
#include<stdio.h>
int main(){
int s,temp,i,j,a[20];
return 0;
}
#include<stdio.h>
int main(){
int i,j,s,temp,a[20];
for(i=1;i<s;i++){
temp=a[i];
j=i-1;
while((temp<a[j])&&(j>=0)){
a[j+1]=a[j];
j=j-1;
}
a[j+1]=temp;
}
return 0;
}
#include<stdio.h>
int main(){
int s,i,j,temp,a[20];
for(i=0;i<s;i++){
for(j=i+1;j<s;j++){
if(a[i]>a[j]){
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
return 0;
}
19.Source code of simple quick sort implementation using
array ascending order in c programming language
#include<stdio.h>
int main(){
int x[20],size,i;
quicksort(x,0,size-1);
printf("Sorted elements: ");
for(i=0;i<size;i++)
printf(" %d",x[i]);
return 0;
}
if(first<last){
pivot=first;
i=first;
j=last;
while(i<j){
while(x[i]<=x[pivot]&&i<last)
i++;
while(x[j]>x[pivot])
j--;
if(i<j){
temp=x[i];
x[i]=x[j];
x[j]=temp;
}
}
temp=x[pivot];
x[pivot]=x[j];
x[j]=temp;
quicksort(x,first,j-1);
quicksort(x,j+1,last);
}
}
#include<stdio.h>
#define MAX 50
void mergeSort(int arr[],int low,int mid,int high);
void partition(int arr[],int low,int high);
int main(){
int merge[MAX],i,n;
partition(merge,0,n-1);
int mid;
if(low<high){
mid=(low+high)/2;
partition(arr,low,mid);
partition(arr,mid+1,high);
mergeSort(arr,low,mid,high);
}
}
int i,m,k,l,temp[MAX];
l=low;
i=low;
m=mid+1;
while((l<=mid)&&(m<=high)){
if(arr[l]<=arr[m]){
temp[i]=arr[l];
l++;
}
else{
temp[i]=arr[m];
m++;
}
i++;
}
if(l>mid){
for(k=m;k<=high;k++){
temp[i]=arr[k];
i++;
}
}
else{
for(k=l;k<=mid;k++){
temp[i]=arr[k];
i++;
}
}
for(k=low;k<=high;k++){
arr[k]=temp[k];
}
}
#include <stdio.h>
int main()
{
int first, second, *p, *q, sum;
printf("Enter two integers to add\n");
scanf("%d%d", &first, &second);
p = &first;
q = &second;
sum = *p + *q;
return 0;
}
int main()
{
long first, second, *p, *q, sum;
printf("Input two integers to add\n");
scanf("%ld%ld", &first, &second);
return 0;
}
sum = *x + *y;
return sum;
}
23./* Source code to demonstrate, handling of pointers in
C program */
#include <stdio.h>
int main(){
int* pc;
int c;
c=22;
printf("Address of c:%d\n",&c);
printf("Value of c:%d\n\n",c);
pc=&c;
printf("Address of pointer pc:%d\n",pc);
printf("Content of pointer pc:%d\n\n",*pc);
c=11;
printf("Address of pointer pc:%d\n",pc);
printf("Content of pointer pc:%d\n\n",*pc);
*pc=2;
printf("Address of c:%d\n",&c);
printf("Value of c:%d\n\n",c);
return 0;
}
24.#include <stdio.h>
int main(){
char c[4];
int i;
for(i=0;i<4;++i){
printf("Address of c[%d]=%x\n",i,&c[i]);
}
return 0;
}