Professional Documents
Culture Documents
INFORMATION TECHNOLOGY
C Programming File
SUBMITTED TO:
SUBMITTED BY:
INDEX
S.No Title
.
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
Page
No.
Remar
k
Write a program to find the sum of its digits till the result
is in single digit.
Write a program to find the factorial of a given number.
Write a program to obtain the sum of the diagonal
elements of matrices.
Write function to add, subtract, multiply & divide two
complex numbers (x+iy) & (a+ib).
Write a program to find the roots of a quadratic equation
with each condition.
Write a program to find numbers between 7 and 100 which
is exactly divisible by 4 and is divisible by either 5 or 6.
Write a program to convert:
a. Binary to Decimal
b. Decimal to Binary
c. Binary to Hexadecimal
Write a program to perform Arithmetic operation on an
array i.e. Addition, Subtraction, Multiplication and
Division and store the result in another array.
2
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
j=n>>2;
2. Write a program to use bitwise & operator between 2 integer and display
the result.
#include<stdio.h>
#include<conio.h>
void main()
{
unsigned a=0x5fab;
4
unsigned b=0xa62b;
int c;
c=a&b;
printf("c = %x\n",c);
getch();
}
OUTPUT:
3. Write a program to input 6 numbers and find the biggest and smallest using
nested if.
#include <stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
printf("Enter numbers to find biggest and smallest values:- \n");
scanf("%d%d%d",&a,&b,&c);
if(a>b)
{
if(a>c)
printf("\nBeggest value is:- %d",a);
else
printf("\nBeggest value is:- %d",c);
}
else
{
if(b>c)
printf("\nBeggest value is:- %d",b);
else
printf("\nBeggest value is:- %d",c);
}
if(a<b)
{
if(a<c)
printf("\nSmallest value is:- %d",a);
else
5
OUTPUT:
printf("\n Months:%d",mon);
printf("\n Hours:%ld",hrs);
printf("\n Days:%ld",ds);
printf("\n Minutes:%1d",min);
printf("\n Seconds:%d",se);
getch();
}
OUTPUT:
5. Write a program to find the sum of even and odd numbers using switch, if,
if..else, nested if between 1 and 20.
#include<stdio.h>
#include<conio.h>
void main()
{
int number;
int min,max;
printf("Enter the minimum range: ");
scanf("%d",&min);
printf("Enter the maximum range: ");
scanf("%d",&max);
printf("Odd numbers in given range are: ");
for(number = min;number<= max; number++)
if(number % 2 !=0)
printf("%d ",number);
printf("\nEven numbers in given range are: ");
7
6. Write a program to find the numbers between 1 and 100 that are not divisible
by 2, 3 and 5.
#include<stdio.h>
#include<conio.h>
void main()
{
int x,c=0;
printf("\n Numbers from 1 to 100 not divisible by 2,3&5\n\n");
for(x=0;x<=100;x++)
{
if(x%2!=0&&x%3!=0&&x%5!=0)
{
printf(" %d\t",x);
c++;
}
}
printf("\nTotal Numbers:%d",c);
getch();
}
OUTPUT:
OUTPUT:
if(s>59)
{
m=m+1;
s=0;
}
if(m>59)
{
h=h+1;
m=0;
10
}
if(h>11)
{
h=0;
m=0;
s=0;
}
delay(1000);
s=s+1;
clrscr();
printf("\n DIGITAL CLOCK");
printf("\n HOUR:MINUTE:SECOND");
printf("\n%d:%d:%d",h,m,s);
}
OUTPUT:
11
9. Write a program to find the sum of its digits till the result is in single digit.
#include<stdio.h>
#include<conio.h>
int main()
{
int n,rem,sum = 0;
printf("\nEnter a number : ");
scanf("%d",&n);
while(n > 0)
{
while(n != 0)
{
rem = n%10;
sum = sum+rem;
n=n/10;
}
if(sum > 9)
{
n = sum;
sum = 0;
}
}
printf("Answer is %d",sum);
getch();
}
OUTPUT:
12
OUTPUT:
11. Write a program to obtain the sum of the diagonal elements of matrices.
#include<stdio.h>
13
#include<conio.h>
int main()
{
int a[10][10],i,j,sum=0,m,n;
printf("\nEnter the row and column of matrix: ");
scanf("%d %d",&m,&n);
printf("\nEnter the elements of matrix: ");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
printf("\nThe matrix is\n");
for(i=0;i<m;i++){
printf("\n");
for(j=0;j<m;j++){
printf("%d\t",a[i][j]);
}
}
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
if(i==j)
sum=sum+a[i][j];
}
}
printf("\n\nSum of the diagonal elements of a matrix is: %d",sum);
getch();
14
OUTPUT:
12. Write function to add, subtract, multiply & divide two complex numbers
(x+iy) & (a+ib).
#include<stdio.h>
#include<conio.h>
struct complex
{
int r,i;
};
int read(struct complex *);
struct complex add(struct complex *,struct complex *);
struct complex sub(struct complex *,struct complex *);
struct complex mul(struct complex *,struct complex *);
int write(struct complex *);
int main()
{
struct complex a,b,c,d,e;
15
read(&a);
read(&b);
c=add(&a,&b);
d=sub(&a,&b);
e=mul(&a,&b);
write(&c);
write(&d);
write(&e);
getch();
}
int read(struct complex *x)
{
printf("Enter Real Part : ");
scanf("%d",&(x->r));
printf("Enter Imig Part : ");
scanf("%d",&(x->i));
}
struct complex add(struct complex *x,struct complex *y)
{
struct complex z;
z.r=x->r+y->r;
z.i=x->i+y->i;
return(z);
}
struct complex sub(struct complex *x,struct complex *y)
{
struct complex z;
16
z.r=x->r-y->r;
z.i=x->i-y->i;
return(z);
}
struct complex mul(struct complex *x,struct complex *y)
{
struct complex z;
z.r=(x->r)*(y->r)-(x->i)*(y->i);
z.i=(x->r)*(y->i)+(x->i)*(y->r);
return(z);
}
int write(struct complex *x)
{
printf("\nThe no. is= %d+%di",x->r,x->i);
}
OUTPUT:
13. Write a program to find the roots of a quadratic equation with each
condition.
#include <stdio.h>
#include <math.h>
#include<conio.h>
void main()
{
float a, b, c, determinant, r1,r2, real, imag;
17
clrscr();
printf("Enter coefficients a, b and c: ");
scanf("%f%f%f",&a,&b,&c);
determinant=b*b-4*a*c;
if (determinant>0)
{
r1= (-b+sqrt(determinant))/(2*a);
r2= (-b-sqrt(determinant))/(2*a);
printf("Roots are: %.2f and %.2f",r1 , r2);
}
else if (determinant==0)
{
r1 = r2 = -b/(2*a);
printf("Roots are: %.2f and %.2f", r1, r2);
}
else
{
real= -b/(2*a);
imag = sqrt(-determinant)/(2*a);
printf("Roots are: %.2f+%.2fi and %.2f-%.2fi", real, imag, real, imag);
}
getch();
}
OUTPUT:
14. Write a program to find numbers between 7 and 100 which is exactly
divisible by 4 and is divisible by either 5 or 6.
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c,i;
clrscr();
for(i=7;i<101;i++)
{
if(i%4==0)
{
if((i%5==0) && (i%6==0))
18
{
printf("Numbers exactly divisible by 5 and divisible by 5 or 6
are:-\n%d",i);
}
}
}
getch();
}
OUTPUT:
OUTPUT:
b. Decimal to Binary
19
#include<stdio.h>
#include<conio.h>
void main()
{
long int d,q,j=1;
int binary[100],i=1;
clrscr();
printf("Enter any decimal number: ");
scanf("%ld",&d);
q= d;
while(q!=0)
{
binary[i++]= q % 2;
q= q / 2;
}
printf("Equivalent binary value of decimal number %d: ",d);
for(j=i-1;j>0;j--)
printf("%d",binary[j]);
getch();
}
OUTPUT:
c. Binary to Hexadecimal
#include<stdio.h>
#include<conio.h>
void main()
{
long int b,d=0,j=1,h=0,r;
clrscr();
printf("Enter any binary number: ");
scanf("%ld",&b);
while(b!=0)
{
r=b%10;
h=h+r*j;
j=j*2;
b=b/10;
}
printf("Equivalent hexadecimal value: %lX",h);
getch();
}
20
OUTPUT:
OUTPUT:
21
17. Write a program to perform following string operation: with string functions
& without string functions
a. Reverse a string
b. Concatinate 2 string strcat()
c. Compare 2 string strcmp(), strcmpi()
#include<conio.h>
#include<stdio.h>
#include<string.h>
void main()
{
int choice;
clrscr();
printf("Enter Choice:\n1.Reverce a String.\n2.Concatinate 2 string.\n3.Compare 2
strings.\n");
scanf("%d",&choice);
if(choice==1)
{
int choice2;
printf("\n1.Unsing Library Function.\n2.With out Using Library Fuction.");
scanf("%d",&choice2);
if(choice2==1)
{
char s1[20],s2[20];
printf("\nEnter any string: ");
scanf("%s",&s1);
strrev(s1);
22
c[i]='\0';
printf("\n\nTHE COMBINED STRING IS:- %s",c);
}
}
if(choice==3)
{
int choice2;
printf("\n1.Unsing Library Function.\n2.With out Using Library Fuction.");
scanf("%d",&choice2);
if(choice2==1)
{
char a[100], b[100];
printf("Enter the first string\n");
scanf("%s",&a);
printf("Enter the second string\n");
scanf("%s",&b);
if( strcmp(a,b) == 0 )
printf("Entered strings are equal.\n");
else
printf("Entered strings are not equal.\n");
}
if(choice2==2)
{
char str1[25],str2[25];
int dif,i=0;
printf("\nEnter the first String:");
scanf("%s",&str1);
printf("\nEnter the second String;");
scanf("%s",&str2);
while(str1[i]!='\0'||str2[i]!='\0')
{
dif=(str1[i]-str2[i]);
if(dif!=0)
break;
i++;
}
if(dif>0)
printf("%s comes after %s",str1,str2);
else
{
if(dif<0)
printf("%s comes after %s",str2,str1);
else
printf("both the strings are same");
}
}
}
getch();
}
OUTPUT:
24
OUTPUT:
void main()
{
char str[15];
int i;
clrscr();
printf("Enter the string:");
scanf("%s",str);
for(i=0;str[i]!='\0';i++)
{
printf("\n The position of %c in string: %d",str[i],i);
}
getch();
}
OUTPUT:
20. Write a program to display and count the number of vowels in a string.
#include<stdio.h>
#include<conio.h>
void main()
{
char str[50];
int j = 0;
int a,e,i,o,u;
clrscr();
a=e=i=o=u=0;
printf("\n\nENTER A STRING: ");
gets(str);
while(str[j] != '\0')
{
if(str[j]=='A' || str[j]=='a' || str[j]=='E' || str[j]=='e' || str[j]=='I' || str[j]=='i' ||
str[j]=='O' || str[j]=='o' || str[j]=='U' || str[j]=='u')
{
switch (str[j])
{
case 'a':
a++;
break;
26
case 'e':
e++;
break;
case 'i':
i++;
break;
case 'o':
o++;
break;
case 'u':
u++;
break;
case 'A':
a++;
break;
case 'E':
e++;
break;
case 'I':
i++;
break;
case 'O':
o++;
break;
case 'U':
u++;
break;
}
}
j++;
}
printf("\n\n\t THE NUMBER OF VOWEL 'A' IS-: %d", a);
printf("\n\n\t THE NUMBER OF VOWEL 'E' IS-: %d", e);
printf("\n\n\t THE NUMBER OF VOWEL 'I' IS-: %d", i);
printf("\n\n\t THE NUMBER OF VOWEL 'O' IS-: %d", o);
printf("\n\n\t THE NUMBER OF VOWEL 'U' IS-: %d", u);
getch();
}
OUTPUT:
27
OUTPUT:
28
OUTPUT:
int fact(int n)
{
if(n==1)
return 1;
else
return(n*fact(n-1));
}
OUTPUT:
OUTPUT:
30
25. Write a program to obtain prime factors of any integer number using
functions i.e. 24 -> 2, 2, 2, & 3.
#include<stdio.h>
#include<conio.h>
void main()
{
int num,i=1,j,k;
clrscr();
printf("\nEnter a number:");
scanf("%d",&num);
while(i<=num)
{
k=0;
if(num%i==0)
{
j=1;
while(j<=i)
{
if(i%j==0)
k++;
j++;
}
if(k==2)
printf("\n%d is a prime factor",i);
}
i++;
}
getch();
}
OUTPUT:
31
OUTPUT:
r=n%10;
s=s+r;
sum(n/10);
}
else
return s;
}
OUTPUT:
33
28. Write a program to create, display, modify and append a file (sequential
file).
#include <stdio.h>
void main()
{
FILE *fptr;
char name[20];
int age;
float salary;
/* open for writing */
clrscr();
fptr = fopen("emp.txt", "w");
if (fptr == NULL)
{
printf("File does not exists \n");
return;
}
printf("Enter the name \n");
scanf("%s", name);
fprintf(fptr, "Name = %s\n", name);
printf("Enter the age\n");
scanf("%d", &age);
fprintf(fptr, "Age = %d\n", age);
printf("Enter the salary\n");
scanf("%f", &salary);
fprintf(fptr, "Salary = %.2f\n", salary);
fclose(fptr);
}
OUTPUT:
#include<process.h>
void main()
{
FILE *fp1, *fp2;
char a;
clrscr();
fp1 = fopen("test.txt", "r");
if (fp1 == NULL)
{
puts("cannot open this file");
exit(1);
}
fp2 = fopen("test1.txt", "w");
if (fp2 == NULL)
{
puts("Not able to open this file");
fclose(fp1);
exit(1);
}
do
{
a = fgetc(fp1);
fputc(a, fp2);
}while (a != EOF);
fcloseall();
getch();
}
OUTPUT:
35