C Lab Programs
C Lab Programs
Part B: C Programming
#include<stdio.h>
#include<conio.h>
void main( )
{
clrscr( );
int num,sum=0,rev=0,d;
printf("Enter the number: ");
scanf("%d",&num);
while(num){
d=num%10;
num=num/10;
sum=sum+d;
rev=rev*10+d;
}
printf("Sumof digits = %d",sum);
printf("\nReverse of the number = %d",rev);
getch( );
}
Algorithm
Step .1: start
Step . 2:declare required variables and initialize sum=0 and rev=0
Step . 3: get the value for variable “num”
Step . 4: using while loop, perform step 5 until condition become false
Step . 5: calculate : d=num%10
Num=num/10
Sum=sum+d
Rev=rev*10+d
Step . 6: print the value of sum and reverse
Step . 7:stop
Output
Enter the number: 52
Sumof digits = 7
Reverse of the number = 25
2
Page
HTML & C
YUVAKSHETRA INSTITUTE OF MANAGEMENT STUDIES – DEPARTMENT OF COMPUTER SCIENCE
Programming
#include<stdio.h>
#include<conio.h>
void main()
{
int n, first = 0, second = 1, next, c;
clrscr();
printf("Enter the number of terms\n");
scanf("%d",&n);
printf("First %d terms of Fibonacci series are :-\n",n);
Algorithm
second = next;
Page
HTML & C
YUVAKSHETRA INSTITUTE OF MANAGEMENT STUDIES – DEPARTMENT OF COMPUTER SCIENCE
Programming
Output
4
Page
HTML & C
YUVAKSHETRA INSTITUTE OF MANAGEMENT STUDIES – DEPARTMENT OF COMPUTER SCIENCE
Programming
#include <stdio.h>
int main()
{
int i, space, rows, k=0;
printf("Enter number of rows: ");
scanf("%d",&rows);
for(i=1; i<=rows; ++i, k=0)
{
for(space=1; space<=rows-i; ++space)
{
printf(" ");
}
while(k != 2*i-1)
{
printf("* ");
++k;
}
printf("\n");
}
return 0;
}
Algorithm
Step 1: start
Step 2; declare required variables and initialize k=0
Step 3: get number of rows ,rows
Step 4: using for loop “i” with initial value zero and condition i<=rows perform step
5 to 8
Step 5: using another for loop “space” with initial value 1 and condition
space<=rows-I,
perform step 6
Step 6: perform print function printf(“ ”)
Step 7: using while loop with condition k!=2*i-1,print “*” and increment k value
Step 8: print next line of pyramid by printf(“\n”)
5
Step 9: stop
Page
HTML & C
YUVAKSHETRA INSTITUTE OF MANAGEMENT STUDIES – DEPARTMENT OF COMPUTER SCIENCE
Programming
Output
*
***
*****
*******
*********
6
Page
HTML & C
YUVAKSHETRA INSTITUTE OF MANAGEMENT STUDIES – DEPARTMENT OF COMPUTER SCIENCE
Programming
#include <stdio.h>
#include <string.h>
void main()
{
char s[200];
int count = 0, i;
printf("enter the string\n");
scanf("%[^\n]s", s);
for (i = 0;s[i] != '\0';i++)
{
if (s[i] == ' ')
count++;
}
printf("number of words in given string are: %d\n", count + 1);
}
Algorithm
Step 1: start
Step 2: declare a char array s[200],and other required variables
Step 3: get the string value “s”
Step 4: using for loop with initial value zero and condition s[i]!=’\0’, perform step 5
Step 5: check condition s[i]==” ” ,if true, perform count++
Step 6: print totals words in given string
Step 7: stop
Output
enter the string:
welcome to yims
#include<stdio.h>
#include<conio.h>
void main( )
{
clrscr();
int n,i,f=0;
printf("Enter the number: ");
scanf("%d",&n);
for(i=2;i<n;i++)
{
if(n%i==0)
{
f=1;
break;
}
}
if(f==0)
printf("The given number is prime");
else
printf("The given number is not prime");
getch();
}
Algorithm
Step 1: Start
Step 2: Read number n
Step 3: Set f=0
Step 4: For i=2 to n-1
Step 5: If n mod 1=0 then
Step 6: Set f=1 and break
Step 7: Loop
Step 8: If f=0 then
8
else
print 'The given number is not prime'
Step 9: Stop
Output
9
Page
HTML & C
YUVAKSHETRA INSTITUTE OF MANAGEMENT STUDIES – DEPARTMENT OF COMPUTER SCIENCE
Programming
#include <stdio.h>
#include<conio.h>
#define ROW 2
#define COL 2
void matrixInput(int mat[][COL]);
void matrixPrint(int mat[][COL]);
void matrixTranspose(int mat[][COL]);
void main()
{
int mat1[ROW][COL];
int mat2[ROW][COL];
int product[ROW][COL],add[ROW][COL],sub[ROW][COL];
clrscr();
printf("Enter elements in matrix of size %dx%d\n", ROW, COL);
matrixInput(mat1);
printf("\n matrix before transpose \n");
matrixPrint(mat1);
matrixTranspose(mat1);
getch();
}
void matrixInput(int mat[][COL])
{
int row, col;
for (row = 0; row < ROW; row++)
{
for (col = 0; col < COL; col++)
{
scanf("%d", (*(mat + row) + col));
}
}
}
void matrixPrint(int mat[][COL])
{
int row, col;
10
{
for (col = 0; col < COL; col++)
{
printf("%d ", *(*(mat + row) + col));
}
printf("\n");
}
}
void matrixTranspose(int mat[][COL])
{
int row, col,trans[ROW][COL];
for (row = 0; row < ROW; row++)
{
for (col = 0; col < COL; col++)
{
*(*(trans + col) + row) = *(*(mat + row) + col);
}
}
printf("matrix after transpose\n");
for (row = 0; row < ROW; row++)
{
for (col = 0; col < COL; col++)
{
printf("%d ", *(*(trans + row) + col));
}
printf("\n");
}
}
Algorithm
Step 1: start
Step 2: declare functions like,matrixinput(),matrixprint(),matrixtranspose
with required attributes and variables
Step 3: declare 2D matrix mat1[][]
Step 4: get values to mat1[][]using matrixinput()
Step 5: perform transpose of matrix using matrixtranspose() with arguments mat1
and print the transpose of matrix mat1
11
Step 9: stop
Page
HTML & C
YUVAKSHETRA INSTITUTE OF MANAGEMENT STUDIES – DEPARTMENT OF COMPUTER SCIENCE
Programming
OUTPUT
12
Page
HTML & C
YUVAKSHETRA INSTITUTE OF MANAGEMENT STUDIES – DEPARTMENT OF COMPUTER SCIENCE
Programming
7. Find the sum of the series S = 1 + (½)2 + (1/3)3 +. ... to 0.0001% accuracy.
#include <math.h>
#include <stdio.h>
double Series(int n)
{
int i;
double sums = 0.0, ser;
for (i = 1; i <= n; ++i) {
ser = 1 / pow(i, i);
sums += ser;
}
return sums;
}
int main()
{
int n ;
printf("Enter the limit ");
scanf("%d",&n);
double res = Series(n);
printf("\n sum of the series is %f", res);
return 0;
}
Algorithm
Step 1: start
Step 2: get the value for “n”
Step 3: call the function “series()” with pass by value “n” and assign the
return value to “res”
Step 4: within the function series(), declare required variables and assign
sums=0.0
Step 5: perform ser=1/pow(i,i) using for loop “i”
Step 6: calculate sum+=ser
Step 7: return “sums” value to main()
Step 8: stop
13
Page
HTML & C
YUVAKSHETRA INSTITUTE OF MANAGEMENT STUDIES – DEPARTMENT OF COMPUTER SCIENCE
Programming
Output
#include <stdio.h>
#include<conio.h>
void main()
{
long n , i=1;
clrscr();
printf("Enter number");
scanf("%ld",&n);
printf("\n Pattern \n");
for(i=10;i<n;i*=10);
for (i=i/10; n>0; i/=10)
{ printf("%ld \n", n);
n%=i;
}
getch();
}
Algorithm
4
Page
HTML & C
YUVAKSHETRA INSTITUTE OF MANAGEMENT STUDIES – DEPARTMENT OF COMPUTER SCIENCE
Programming
#include<stdio.h>
#include<string.h>
void main()
{
char str[100],*ptr,i,l;
clrscr();
printf("Enter any string\n");
gets(str);
l=strlen(str);
ptr=str;
printf("%c",*(ptr+0));
for(i=1;i<l;i++)
{
if(*(ptr+i-1)==' ')
printf(" %c ",*(ptr+i));
}
getch();
}
Algorithm
OUTPUT
16
Page
HTML & C
YUVAKSHETRA INSTITUTE OF MANAGEMENT STUDIES – DEPARTMENT OF COMPUTER SCIENCE
Programming
#include <stdio.h>
#include <conio.h>
#define SIZE 9
void main()
{
int amount, notes,i;
int denominations[SIZE] = { 2000, 500, 200, 100, 50, 20, 10, 5, 1 };\
clrscr();
printf("Enter amount: ");
scanf("%d", &amount);
printf("\n");
for (i=0;i<SIZE;i++)
{
notes = amount / denominations[i];
if (notes)
{
amount = amount % denominations[i];
printf("%d * %d = %d \n", notes, denominations[i],
notes * denominations[i]);
}
}
getch();
}
ALGORITHM
OUTPUT
18
Page
HTML & C
YUVAKSHETRA INSTITUTE OF MANAGEMENT STUDIES – DEPARTMENT OF COMPUTER SCIENCE
Programming
Algorithm
Step 1: start
19
Step 3: using for loop with initial value “low” and limit “high” ,perform step 4 to8
Step 4: assign value of “I” to temp2 and temp1
Step 5: using while loop with condition temp1!=0,perform temp/=10 and increment
“n” value
Step 6: using while loop with condition temp2!=0 perform
Reaminder=temp%10
Result+=pow(remainder,n)
Temp2/=10
Step 7: if result value equal to one, printvalue of “I”
Step 8: assign n and result value with zero
Step 9: stop
Output
Enter two numbers (intervals):
10
500
Armstrong numbers between 10 an 500 are:
153
370
371
407
20
Page
HTML & C
YUVAKSHETRA INSTITUTE OF MANAGEMENT STUDIES – DEPARTMENT OF COMPUTER SCIENCE
Programming
#include<stdio.h>
#include<conio.h>
long int fact(int n);
void main() {
int n;
clrscr();
printf("Enter a positive integer: ");
scanf("%d",&n);
printf("Factorial of %d = %ld", n, fact(n));
getch();
}
Algorithm
Step 1: start
Step 2: declare function fact(int n)and required varibles.
Step 3: get positive value for variable “n”
Step 4: call function fact(n); and perform step 5 and print factorial value
Step 5: long int fact(int n)
{
if (n>=1)
return n*fact(n-1);
else
return 1;
}
Step 6: stop
21
Page
HTML & C
YUVAKSHETRA INSTITUTE OF MANAGEMENT STUDIES – DEPARTMENT OF COMPUTER SCIENCE
Programming
OUTPUT
22
Page
HTML & C
YUVAKSHETRA INSTITUTE OF MANAGEMENT STUDIES – DEPARTMENT OF COMPUTER SCIENCE
Programming
malayalam is a palindrome
Page
HTML & C
YUVAKSHETRA INSTITUTE OF MANAGEMENT STUDIES – DEPARTMENT OF COMPUTER SCIENCE
Programming
#include <stdio.h>
#include <conio.h>
void main()
{
int year;
printf("Enter a year: ");
scanf("%d",&year);
if(year%4 == 0)
{
if( year%100 == 0)
{
if ( year%400 == 0)
printf("%d is a leap year.", year);
else
printf("%d is not a leap year.", year);
}
else
printf("%d is a leap year.", year );
}
else
printf("%d is not a leap year.", year);
getch();
}
Algorithm
Step 1: start
Step 2: declare and get a value for variable “year”
Step 3: if year%4 == 0, year%100 == 0) and year%400 == 0) is true then
print leap year, else print not a leap year
Step 4: stop
Output
Enter a year: 2020
2020 is a leap year.
24
Page
HTML & C
YUVAKSHETRA INSTITUTE OF MANAGEMENT STUDIES – DEPARTMENT OF COMPUTER SCIENCE
Programming
#include<stdio.h>
#include<math.h>
void main()
{
FILE *all,*even,*odd;
int number,i,records;
clrscr();
printf("INPUT THE TOTAL NUMBER OF RECORDS THAT U WANT TO ENTER");
scanf("%d",& records);
printf("enter numbers");
all=fopen("ANYNUMBER","w");
for(i=1;i<=records;i++)
{
scanf("%d",&number);
if(number==-1)break;
putw(number,all);
}
fclose(all);
all=fopen("ANYNUMBER","r");
even=fopen("EVENNUMBER","w");
odd=fopen("ODDNUMBER","w");
while((number=getw(all))!=EOF)
{
if(number%2==0)
putw(number,even);
else
putw(number,odd);
}
fclose(all);
fclose(even);
fclose(odd);
even=fopen("EVENNUMBER","r");
odd=fopen("ODDNUMBER","r");
printf("THE EVEN NUMBERS ARE");
25
while((number=getw(even))!=EOF)
Page
HTML & C
YUVAKSHETRA INSTITUTE OF MANAGEMENT STUDIES – DEPARTMENT OF COMPUTER SCIENCE
Programming
printf("\n %d",number);
printf("\n THE ODD NUMBERS ARE");
while((number=getw(odd))!=EOF)
printf("\n %d\n",number);
fclose(even);
fclose(odd);
getch();
}
Algorithm
Step 1: start
Step 2; declare file pointer “all,”even” and “odd”, also declare required variables
Step 3: get total number of records to variable “number”
Step 4: using fopen(),open file “ANY NUMBER” in write mode and assign value to
file pointer “all”
Step 5: using for loop with limit records,get value to variable “number”
Step 6; if value of number ==-1, break
Step 7: write number to file pointer “all” using putw()
Step 8: close all files using fclose()
Step 9: open “ANYNUMBER” “ODDNUMBER”EVENNUMBER” files in read and
write mode respectively
Step 10: calculate odd and even number and assign values to filepointer “even” and
“odd”
Step 11: close all files
Step 12 : print even and odd numbers from respective file
Step 13: stop
OUTPUT
INPUT THE TOTAL NUMBER OF RECORDS THAT U WANT TO ENTER
5
Enter numbers
1
2
3
4
5
THE EVEN NUMBERS ARE
2
4
THE ODD NUMBERS ARE
1
3
26
5
Page
HTML & C
YUVAKSHETRA INSTITUTE OF MANAGEMENT STUDIES – DEPARTMENT OF COMPUTER SCIENCE
Programming
#include <stdio.h>
#include<conio.h>
void main()
{
char base_digits[16] =
{'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
int converted_number[64];
int number_to_convert;
int next_digit, base, index=0;
clrscr();
printf("Decimal to other base conversion program \n");
printf("Enter a decimal number ");
scanf("%d",&number_to_convert);
printf("Enter desired base to convert: ");
scanf("%d",&base);
while (number_to_convert != 0)
{
converted_number[index] = number_to_convert % base;
number_to_convert = number_to_convert / base;
++index;
}
--index;
printf("\n\nConverted Number from base 10 to base %d= ",base);
for( ; index>=0; index--)
{
printf("%c", base_digits[converted_number[index]]);
}
printf("\n");
getch();
}
Algorithm
Step 1: start
27
Step 3: get a decimal value and base value to the respective variables
Step 4: using while loop with condition number_to_convert !=0, perform conversion
calculations
Step 5: decrement index value
Step 6: using for loop print new number (converted to new base)
Step 7: stop
Output
Decimal to other base conversion program
Enter a decimal number 9
Enter desired base to convert: 2
Converted Number from base 10 to base 2= 1001
28
Page
HTML & C
YUVAKSHETRA INSTITUTE OF MANAGEMENT STUDIES – DEPARTMENT OF COMPUTER SCIENCE
Programming
#include <stdio.h>
void main()
{
int arr1[100], arr2[100], arr3[200];
int s1, s2, s3;
int i, j, k;
clrscr();
printf("\n\nMerge two arrays in sorted order.\n");
printf(" -----------------------------------------------------------\n");
printf("Input the number of elements to be stored in the first array :");
scanf("%d",&s1);
i++;
Page
HTML & C
YUVAKSHETRA INSTITUTE OF MANAGEMENT STUDIES – DEPARTMENT OF COMPUTER SCIENCE
Programming
}
for(i=0;i<s3; i++)
{
for(k=0;k<s3-1;k++)
{
if(arr3[k]<=arr3[k+1])
{
j=arr3[k+1];
arr3[k+1]=arr3[k];
arr3[k]=j;
}
}
}
printf("\nThe merged array in decending order is :\n");
for(i=s3; i>0; i--)
{
printf("%d ", arr3[i]);
}
printf("\n\n");
getch();
}
Algorithm
Step 1: start
Step 2: declare, array variables arr1[],arr2[],arr3[] and other required variables
Step 3: using for loops get un sorted array values to arr1[] and arr2[] respectively
Step 4: perform merging and sorting operations using i and j loops with limits s1
and s2 respectively
Step 5: print sorted array arr3[]
Step 6: stop
30
Page
HTML & C
YUVAKSHETRA INSTITUTE OF MANAGEMENT STUDIES – DEPARTMENT OF COMPUTER SCIENCE
Programming
OUTPUT
11345677
31
Page
HTML & C
YUVAKSHETRA INSTITUTE OF MANAGEMENT STUDIES – DEPARTMENT OF COMPUTER SCIENCE
Programming
18. Fill upper triangle with 1, lower triangle with -1 and diagonal elements with 0.
#include<stdio.h>
#include<conio.h>
void main()
{
int i, j,row=3,col=3;
clrscr();
printf("Upper Triangle forms like \n");
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
if (i<j || i==j)
{
printf("1");
}
else
printf("0");
printf(" ");
}
printf("\n");
}
printf(" ");
Page
HTML & C
YUVAKSHETRA INSTITUTE OF MANAGEMENT STUDIES – DEPARTMENT OF COMPUTER SCIENCE
Programming
}
printf("\n");
}
printf(" Triangle with diagonal elements zero \n");
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
if (i==j)
{
printf("0");
}
else
printf("1");
printf(" ");
}
printf("\n");
}
getch();
}
Algorithm
Step 1: start
Step 2: declare required variables and also assign row and col value as 3
Step 3: using for loop “i” and “j” with limit row and col , check i<j and i==j, ,if true,
print upper triangle value 1,
Step 3: using for loop “i” and “j” with limit row and col , check i>j and i==j, ,if true,
print lower triangle value -1,
Step 3: using for loop “i” and “j” with limit row and col , check i==j, ,if true, print
diagonal value zero,
Step 4: stop
33
Page
HTML & C
YUVAKSHETRA INSTITUTE OF MANAGEMENT STUDIES – DEPARTMENT OF COMPUTER SCIENCE
Programming
Output
Upper Triangle forms like
111
011
001
Lower Triangle forms like
-1 0 0
-1 -1 0
-1 -1 -1
Triangle with diagonal elements zero
011
101
110
******************************************************************
34
Page