C Basic Programs by Sanaa Salam
C Basic Programs by Sanaa Salam
AIM:
Write a program to find the sum and average of three numbers.
PROGRAM:
#include <stdio.h>
#include <conio.h>
main()
{
int a,b,c,sum,average;
clrscr();
scanf("%d%d%d%",&a,&b,&c);
sum=a+b+c;
average=(a+b+c)/3;
printf("\nsum=%d\n average=%d",sum,average);
getch();
return 0;
}
1
OUTPUT:
RESULT:
The program is compiled. Average of three numbers is shown successfully
2
Exp No:2 Date:20-10-20
AIM:
Write a program to print the greatest among three numbers.
PROGRAM:
#include <stdio.h>
#include <conio.h>
main()
{
int a,b,c;
clrscr();
printf("Enter the values of a,b,c");
scanf("%d%d%d",&a,&b,&c);
if(a>b)
{
if(a>c)
{
printf("\n a is the biggest");
}
else
{
printf("\n c i the biggest");
}
}
else if(b>c)
{
printf("\n b is the biggest");
}
else
{
printf("\n c is biggest");
}
getch();
return 0;
} 3
OUTPUT:
Enter the values of a,b,c
3
4
5
c is biggest
RESULT:
The program compiled. Biggest among three numbers is shown successfully.
4
Exp No:3 Date:23-10-20
ELECTRICITY CHARGE CALCULATION USING IF ELSE LADDER
AIM:
an electricity power distribution company changes its domestic consumers as
follows:
PROGRAM:
#include <stdio.h>
#include <conio.h>
main()
{
int a;float amount; 5
printf("Enter the consumption unit:");
scanf("%d",&a);
if(a>=0 && a<=200)
amount=a*0.25;
else if(a>200 && a<400)
amount=100+((a-200)*0.65);
else if (a>400 && a<=600)
amount=230+((a-400)*0.80);
else if (a>600)
amount=390+((a-600)*1.00);
printf("amount to be paid=%f",amount);
getch();
return 0;
}
OUTPUT:
Enter the consumption unit:200
amount to be paid=100.000000
RESULT:
Program compiled. Amount to be paid is shown successfully.
7
Exp No:4 Date:27-10-20
AIM:
PROGRAM:
#include <stdio.h>
#include <conio.h>
main()
{
int a,b,result;
char op;clrscr();
printf("Enter an expression:\n");
scanf("%d%c%d",&a,&op,&b);
switch(op)
{
case'+':
result=a+b;
break;
case'-':
result=a-b;
break;
case'*':
result=a*b;
break;
case'/':
result=a/b;
break;
}
printf("result=%d\n",result);
getch();
return 0;}
OUTPUT :
Enter an expression:
56+4
result=60
RESULT:
Program compiled. Operations are done in the simple calculator successfully.
9
Exp No:5 Date:30-10-20
FACTORIAL OF A NUMBER USING WHILE LOOP
AIM:
PROGRAM:
#include <stdio.h>
#include <conio.h>
main()
{
int i,n,f;
clrscr();
f=1;
i=1;
printf("enter the value of n:\n");
scanf("%d",&n);
while(i<=n)
{
f=f*i;
i=i+1;
}
printf("factorial=%d\n",f);
getch();
return 0;
}
10
OUTPUT:
RESULT:
The program was compiled and the factorial of number is shown successfully.
11
Exp No:6 Date:3-11-20
PERFECT NUMBER OR NOT
AIM:
Write a program in c to check whether a number is perfect or not
PROGRAM:
#include <stdio.h>
#include <conio.h>
main()
{
int i,n,sum=0;
clrscr();
printf("enter a number:\n");
scanf("%d",&n);
for(i=1;i<n;i++)
{
if(n%i==0)
sum=sum+i;
}
if(sum==n)
printf("\n%d is a perfect number",n);
else
printf("\n%d is not a perfect number",n);
getch();
return 0;
}
12
OUTPUT:
enter a number:
6
6 is a perfect number
RESULT:
The program was compiled .The entered number is successfully showed whether perfect or not
13
Exp No:7 Date:6-11-20
FIBONACCI SERIES
AIM:
Write a program in c to generate fibonacci series(do...while loop)
PROGRAM:
#include <stdio.h>
#include <conio.h>
main()
{
int n,i,t1,t2,nextterm;
i=1;
t1=0;
t2=1;
nextterm=0;
clrscr();
printf("enter the nubmer of terms:\n");
scanf("%d",&n);
printf("fibonacci series:\n");
do
{
i++;
printf("%d\n",nextterm);
nextterm=t1+t2;
t1=t2;
t2=nextterm;
}
while(i<=n);
getch();
return 0;
}
14
OUTPUT:
enter the number of terms:
10
fibonacci series:
0
1
2
3
5
8
13
21
34
55
RESULT:
The program was compiled. Fibonacci series is shown successfully.
15
Exp No:8 Date:10-11-20
PROGRAM:
#include <stdio.h>
#include <conio.h>
main()
{
int i,j,n,flag;
clrscr();
printf("enter the value of n:");
scanf("%d",&n);
printf("all prime numbers between 1 and n are:\n");
for(i=2;i<=n;i++)
{
flag=0;
for(j=1;j<=n;j++)
{
if(i%j==0)
flag++;
}
if(flag==2)
printf("%d\n",i);
}
getch();
return 0;
}
16
OUTPUT:
enter the value of n:20
all prime numbers between 1 and n are:
2
3
5
7
11
13
17
19
RESULT:
The program was compiled and the prime numbers from 1 to n was printed successfully.
17
Exp No:9 Date:9-11-20
PROGRAM
#include <stdio.h>
#include <conio.h>
main()
{
int n,sum,m;
clrscr();
sum=0;
printf("enter a number:\n");
scanf("%d",&n);
while(n!=0)
{
m=n%10;
n=n/10;
sum=sum+m;
}
printf("sum of digits=%d\n",sum);
getch();
return 0;
}
18
OUTPUT
enter a number:
45
sum of digits=9
RESULT:
The program was compiled. The sum of digits of the number is shown successfully.
19
Exp No:10 Date:17-11-20
AIM:
Write a program to implement break and continue.
PROGRAM:
#include <stdio.h>
#include <conio.h>
#include <math.h>
main()
{
int count,negative;
double n,sqroot;
clrscr();
printf("\nenter 999 to stop\n");
count=0;
negative=0;
while(count<=100)
{
printf("\nenter a number\n");
scanf("%lf",&n);
if(n==999)
break;
if(n<0)
{
printf("number is negative\n");
negative++;
continue;
}
sqroot=sqrt(n);
printf("n=%lf\n square root of %lf=%lf\n\n",n,n,sqroot);
count++;
}
printf("\n\n\nnubmer ofsquareroots found=%d\n",count);
printf("\n\nno.of Negative numbers found=%d\n",negative);
printf("END OF DATA\n");
getch();
return 0;
}
20
OUTPUT:
enter a number
25
n=25.000000
square root of 5.000000=0.000000
enter a number
-25
number is negative
enter a number
999
RESULT:
The programs were compiled. Break and continue statements were implemented
successfully.
21
Exp No:11 Date:20-11-2020
AIM:
Write a program to searching of a key value in an array
PROGRAM:-
#include <stdio.h>
#include <conio.h>
#define SIZE 100
int main()
{
int arr[SIZE];
int size,i,n,found;
clrscr();
printf("enter the size of array:");
scanf("%d",&size);
printf("enter elements in array:");
for (i=0;i<size;i++)
{
scanf("%d",&arr[i]);
}
printf("\nenter element to search:");
scanf("%d",&n);
found=0;
for(i=0;i<size;i++)
{
if(arr[i]==n)
{
found=1;
break;
}
}
if(found==1)
{
printf("\n%d is found at position %d",n,i+1);
}
else
{
printf("\n%d is not found in the array",n);
}
getch();
return 0;}
22
OUTPUT:-
enter the size of array:5
enter elements in array:10
15
20
25
30
25 is found at position 4
RESULT:-
The program was compiled .the position of searched element is shown
successfully.
23
Exp No:12 Date:24-11-2020
SORTING AN ARRAY
AIM:
Write a program to sort an array
PROGRAM:
#include <stdio.h>
#include <conio.h>
void main()
{
int i,j,temp,n,arr[30];
clrscr();
crintf(“enter the size of array:\n”);
scanf(“%d”,&n);
printf(“enter the elements of array:”);
for(i=0;i<n;i++)
scanf(“%d”,&arr[i]);
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(arr[i]>arr[j])
{
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;} 24
}
}
printf(“the numbers arranged in ascending order is given below:-\n”);
for(i=0;i<n;i++)
printf(“%d\n”,arr[i]);
getch();
}
25
OUTPUT:
RESULT:
The program was compiled and the elements of array is sorted in ascending order and shown
successfully.
26
Exp No:13 Date:26-11- 2020
TRANSPOSE 0F A MATRIX
AIM:
Write a program in c to find the transpose of a matrix.
PROGRAM:
#include <stdio.h>
#include <conio.h>
#define SIZE 3
main()
{
int mat[3][3],i,j;
clrscr();
printf("enter the elements of matrix:\n");
for(i=0;i<SIZE;i++)
{
for(j=0;j<SIZE;j++)
{
scanf("%d",&mat[i][j]);
}
}
printf("transpose of the the matrix:\n");
for(i=0;i<SIZE;i++)
{
for(j=0;j<SIZE;j++)
{
printf("%d",mat[j][i]);
}
printf("\n");}
getch();
return 0;} 27
OUTPUT
RESULT:
The program is compiled and transpose of the matrix is got successfully.
28
Exp no:14 Date:01-12-2021
MATRIX MULTIPLICATION
AIM:
Write a program in C to find the product of two matrices
PROGRAM:
#include <stdio.h>
#include <conio.h>
int main()
{
int m,n,p,q,i,j,k,sum=0;
int first[10][10],second[10][10],multiply[10][10];
clrscr();
printf("enter the number of rows and columns of first matrix:\n");
scanf("%d%d",&m,&n);
printf("enter the elements of first matrix:\n");
for(i=0;i<m;i++)
for (j=0;j<n;j++)
scanf("%d",&first[i][j]);
printf("enter the number of rows and columns of second matrix:\n");
scanf("%d%d",&p,&q);
if(n!=p)
printf("multiplication not possible");
else
{
printf("enter the elements of second matrix:\n");
}
for(i=0;i<p;i++)
for(j=0;j<q;j++)
scanf("%d",&second[i][j]);
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
{
for(k=0;k<p;k++)
{
sum=sum+first[i][k]*second[k][j];
} 29
multiply[i][j]=sum;
}
}
printf("product of the matrices:\n");
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
{
printf("%d\t",multiply[i][j]);
printf("\n");}
}
getch();
return 0;
}
30
OUTPUT:
enter the number of rows and columns of first matrix:
2
2
enter the elements of first matrix:
1
2
3
4
enter the number of rows and columns of second matrix:
2
2
enter the elements of second matrix:
5
6
7
8
product of the matrices:
14
16
28
32
RESULT:
The program is compiled and multiplication of two matrices is done successfully.
31
Exp no:15 Date:04-12-2020
AIM:
write a program to perform all string operations using built in functions in c
PROGRAM:
#include <stdio.h>
#include <conio.h>
#include <string.h>
int main()
{
char str1[50],str2[50];
clrscr();
puts("enter str1:\n");
gets(str1);
puts("\nenter str2:\n");
gets(str2);
strcat(str1,str2);
printf("\ncombined string:| %s |\n",str1);
if(strcmp(str1,str2)==0)
printf("\nentered strings are equal\n");
else
printf("\nentered strings are not equal\n");
strcpy(str1,str2);
printf("\nstr1 is:%s",str1);
strrev(str1);
printf("\nreverse of str1 is:%s\n",str1);
strlen(str1);
printf("the length of str1 is:%d",strlen(str1));
getch();
return 0;
}
32
OUTPUT
enter str1:
THE EARTH
enter str2:
IS BEAUTIFUL
RESULT:
The program compiled successfully,using built in funtionsall the string operations are
performed
33
Exp no:16 Date:11-12-2020
AIM:
WAP to count the no.of words in a text
PROGRAM :
#include <stdio.h>
#include <conio.h>
#include <string.h>
void main()
{
char s[200];
int i,count=0;
clrscr();
printf("enter the string:\n");
scanf("%[^\n]s",s);
for(i=0;s[i]!='\0';i++)
{
if(s[i]==' '&& s[i+1]!=' ')
count++;
}
printf("no.of words in given string are %d\n",count);
getch();
}
34
OUTPUT:
enter the string:
welcome to c programming
no.of words in given string are 4
RESULT:
The program was compiled.number of words in the entered text is shown successfully
35
Exp no:17 Date:18-12-2020
AIM:
WAP to check whether a given string is palindrome or not
#include <stdio.h>
#include <conio.h>
#include <string.h>
int main()
{
char string[100],temp,x[100];
int i,j;
printf("enter a string:\n");
scanf("%s",string);
strcpy(x,string);
i=0;
j=strlen(string)-1;
while(i<j)
{
temp=string[i];
string[i]=string[j];
string[j]=temp;
i++;j--;
clrscr();
}
printf("reverse of the string is: %s\n",string);
if(strcmp(x,string)==0)
printf("%s is a palindrome string",x);
else
printf("%s is not a palindrome string",x);
getch();
return 0;
}
36
OUTPUT:
enter a string:
malayalam
reverse of the string is: malayalam
malayalam is a palindrome string
RESULT:
The program was compiled successfully
37
Exp no:18 Date:22-12-2020
AIM:
swap two numbers using call by value & and call by reference
PROGRAM:
#include <stdio.h>
#include <conio.h>
void swap1(int,int);
void swap2(int*,int*);
void main()
{
int a,b;
int c,d;
clrscr();
printf("Enter the value of two integers");
scanf("%d%d",&a,&b);
printf("a=%d,b=%d before calling function\n",a,b);
swap1(a,b);
printf("a=%d,b=%d after calling the function\n",a,b);
RESULT:
The program is compiled .the numbers are swapped using call by value and call by reference
successfully.
39
Exp No::19 Date:05-01-2020
PROGRAM:
#include <stdio.h>
#include <conio.h>
int factorial(int);
int main()
{
int num,fact;
clrscr();
printf("enter any integer number:");
scanf("%d",&num);
fact=factorial(num);
printf("factorial of %d is:%d",num,fact);
getch();
return 0;
}
int factorial(int n)
{
if(n==0)
return(1);
else
return(n*factorial(n-1));
}
40
OUTPUT:
enter any integer number:5
factorial of 5 is:120
RESULT:
The Program is compiled. Factorial of number is shown successfully.
41
Exp No:20 Date:08-01-2020
AIM:
WAP to find average marks and display student details using structure.
PROGRAM:-
#include <stdio.h>
#include <conio.h>
struct student
{
int rno,marks[5],total;
float avg;
char name[10];
}s[10];
void main()
{
int i,j,n;
clrscr();
printf("Enter the no.of students:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter the data of student %d\n",i+1);
printf("\nEnter the name:\n");
scanf("%s",s[i].name);
printf("enter the roll number:\n");
scanf("%d",&s[i].rno);
s[i].total=0;
for(j=0;j<5;j++)
{
printf("enter the marks of the subject%d:",j+1);
scanf("%d",&s[i].marks[j]);
s[i].total=s[i].total+s[i].marks[j];
}
s[i].avg=(s[i].total)/5;
}
for(i=0;i<n;i++)
{
printf("\nTHE DETAILS ARE :\n\nrollno:%d\nname:%s\naverage:%f\n
",s[i].rno,s[i].name,s[i].avg);
}
getch();
return 0;
}
42
OUTPUT:
rollno:1
name:RAM
average:46.000000
rollno:2
name:RAJU
average:44.000000
RESULT:
The program was compiled and thestudent details are shown successfully.
43
Exp No:21 Date:12-01-2021
AIM:
WAP to display employee details using union.
PROGRAM:
#include <stdio.h>
#include <conio.h>
union employee
{
int code;
char name[10];
int bpay;
}u[5];
void main()
{
int i,n;
clrscr();
printf("enter the number of employees:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nenter the details of employee %d:\n",i+1);
printf("\nenter the name:\n");
scanf("%s",u[i].name);
printf("\nname:%s\n",u[i].name);
printf("\nenter the code:\n");
scanf("%d",&u[i].code);
printf("\ncode:%d\n",u[i].code);
printf("\nenter the amount of bpay:\n");
scanf("%d",&u[i].bpay);
printf("\nbpay:%d",u[i].bpay);
}
getch();
return 0;
}
44
OUTPUT:
code:1
bpay:200
enter the details of employee 2:
name:RAMU
code:2
bpay:300
RESULT:
The program is compiled and details of employee printed succesfully.
45
Exp No:22 Date:19-01-2021
AIM:
WAP to implement command line arguments.
PROGRAM :
#include <stdio.h>
if(argc==2)
else if(argc>2)
else
46
OUTPUT :
RESULT :
program to implement command line arguments done successfully.
47
Exp No:23 Date:22-01-2021
DISPLAY ADDRESS AND CONTENTS OF VARIABLES USING
POINTERS
AIM:
WAPto display address and contents of different variables(data types )using pointers.
PROGRAM:
#include <stdio.h>
#include <conio.h>
void main()
{
int i=3;
int *j;
float k=2.5;
float *f;
char ch='a';
char *b;
double p=2.3005;
double *q;
clrscr();
j=&i;
f=&k;
b=&ch;
q=&p;
printf("i=%d\n",i);
printf("&i=%u\n",&i);
printf("j=%u\n",j);
printf("&j=%u\n",&j);
printf("*j=%d\n",*j);
printf("\nk=%f\n",k);
printf("&k=%u\n",&k);
printf("f=%u\n",f);
printf("&f=%u\n",&f);
printf("*f=%f\n",*f);
printf("\nch=%c\n",ch);
printf("&ch=%u\n",&ch);
printf("b=%u\n",b); 48
printf("&b=%u\n",&b);
printf("*b=%c\n",*b);
printf("\np=%lf\n",p);
printf("&p=%u\n",&p);
printf("q=%u\n",q);
printf("&q=%u\n",&q);
printf("*q=%lf\n",*q);
getch();}
49
OUTPUT:
i=3
&i=65524
j=65524
&j=65522
*j=3
k=2.500000
&k=65518
f=65518
&f=65516
*f=2.500000
ch=a
&ch=65515
b=65515
&b=65512
*b=a
p=2.300500
&p=65504
q=65504
&q=65502
*q=2.300500
RESULT:
The program is compiled.the contents and address of different variables of different data
types using pointers displayed succesfully.
50
Exp No:24 Date:25-01-2021
PROGRAM:
#include <stdio.h>
#include <conio.h>
main()
{
int a[10],i,sum=0,*p;
clrscr();
printf("enter 10 elements:");
for(i=0;i<10;i++)
scanf("%d",&a[i]);
p=a;
for(i=0;i<10;i++)
{
sum=sum+*p;
p++;
}
printf("the sum is %d",sum);
getch();
return 0;
}
51
OUTPUT:
enter 10 elements:10
20
30
40
50
60
70
80
90
100
the sum is 550
RESULT:
The program is compiled and sum of the elements of the array got succesfully using
pointers.
52
Exp No:25 Date:29-01-2021
FILE PROGRAM
AIM:
A file named DATA contains a series of integer numbers. Write a program to
read these numbers and then write all “odd” numbers to a filecalledODD and
all “even” numbers to a file called EVEN.
PROGRAM:
#include <stdio.h>
#include <conio.h>
main()
{
FILE *f1,*f2,*f3;
int number,i;
clrscr();
printf("contents of DATA file:\n");
f1=fopen("DATA","w");
for(i=1;i<=10;i++)
{
scanf("%d",&number);
if(number==-1)
break;
putw(number,f1);
}
fclose(f1);
f1=fopen("DATA","r");
f2=fopen("ODD","w");
f3=fopen("EVEN","w");
while((number=getw(f1))!=EOF)
{
if((number%2)==0)
putw(number,f3);
else
putw(number,f2);
}
fclose(f1);
fclose(f2);
fclose(f3);
f2=fopen("ODD","r");
f3=fopen("EVEN","r");
printf("\ncontents of ODD files\n");
while((number=getw(f2))!=EOF)
printf("%4d",number);
printf("\ncontents of EVEN file:\n"); 53
while((number=getw(f3))!=EOF)
printf("%4d",number);
fclose(f2);
fclose(f3);
getch();
return 0;
}
54
OUTPUT:
contents of DATA file:
111
222
333
444
555
666
777
888
999
000
RESULT:
The program is compiled and contents of ODD files and EVEN files got successfully.
55