You are on page 1of 35

AMITY INSTITUTE OF

INFORMATION TECHNOLOGY

C Programming File

SUBMITTED TO:

SUBMITTED BY:

INDEX
S.No Title
.
1.

Write a program to shift input data by 2 bits left and right.

2.

Write a program to use bitwise & operator between 2


integer and display the result.
Write a program to input 6 numbers and find the biggest
and smallest using nested if.
Write a program to enter a year and find the number of:
a. Minutes
b. Hours
c. Days
d. Months
e. Seconds
Write a program to find the sum of even and odd numbers
using switch, if, if-else, nested if between 1 and 20.
Write a program to find the numbers between 1 and 100
that are not divisible by 2, 3 and 5.
Write a program to enter a character (alphabetical) and
display its position and its corresponding ASCII value.
Write a program to simulate a digital clock.

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.

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()
Write a program to detect the occurrence of a number in a
string.
Write a program to accept a string up to 15 character, and
display the position of a character in a separate line.
Write a program to display and count the number of
vowels in a string.
Write a program to generate a palindrome.
Write a program to add to pointer addresses of a pointer
variable.
Write a program to find the factorial of a number using
recursion.
Write a program to perform different arithmetic operations
using pointers.
Write a program to obtain prime factors of any integer
number using functions i.e. 24 -> 2, 2, 2, & 3.
Write a program to find the sum of 5 digit number:
a. Without using recursion
b. With using recursion.
Write a program to obtain Fibonacci series by using
recursion.
Write a program to create, display, modify and append a
file (sequential file).
Write a program to copy the content of one file to another.

1. Write a program to shift input data by 2 bits left and right.


#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,j;
printf("Enter the number on which left shift operation is to be performed:");
scanf("%d",&n);
printf("\nBefore shifting the number was: %d\n",n);
i=n<<2;

//LEFT SHIFT OPERATION

j=n>>2;

//Right SHIFT OPERATION

printf("After shifting the number left is: %d\n",i);


printf("After shifting the number right is: %d\n",j);
getch();
}
OUTPUT:

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

printf("\nSmallest value is:- %d",c);


}
else
{
if(b<c)
printf("\nSmallest value is:- %d",b);
else
printf("\nSmallest value is:- %d",c);
}
getch();
}

OUTPUT:

4. Write a program to enter a year and find the number of:


a. Minutes
b. Hours
c. Days
d. Months
e. Seconds
#include<stdio.h>
#include<conio.h>
void main()
{
long min,hrs,ds,mon,se;
int yrs;
clrscr();
printf("Enter Years:");
scanf("%d",&yrs);
mon=yrs*12;
ds=mon*30;
ds=ds+yrs*5;
hrs=ds*24;
min=hrs*60;
se=min*60;
6

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

for(number = min;number <= max; number++)


if(number % 2 ==0)
printf("%d ",number);
getch();
}
OUTPUT:

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:

7. Write a program to enter a character (alphabetical) and display its position


and its corresponding ASCII value.
#include <stdio.h>
#include<conio.h>
void main()
{
char c;
printf("Enter a character: ");
scanf("%c",&c);
printf("ASCII value of %c = %d",c,c);
getch();
}

OUTPUT:

8. Write a program to simulate a digital clock.


#include "stdio.h"
#include "conio.h"
#include "dos.h"
void main()
{
int h,m,s;
h=0;
m=0;
s=0;
while(1)
{

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

10. Write a program to find the factorial of a given number.


#include<stdio.h>
#include<conio.h>
int main()
{
int i=1,f=1,num;
printf("Enter a number: ");
scanf("%d",&num);
while(i<=num){
f=f*i;
i++;
}
printf("Factorial of %d is: %d",num,f);
getch();
}

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:

15. Write a program to convert:


a. Binary to Decimal
#include<stdio.h>
#include<conio.h>
void main()
{
long int b,d=0,j=1,rem;
clrscr();
printf("Enter any number any binary number: ");
scanf("%ld",&b);
while(b!=0)
{
rem=b%10;
d=d+rem*j;
j=j*2;
b=b/10;
}
printf("Equivalent decimal value: %ld",d);
getch();
}

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:

16. Write a program to perform Arithmetic operation on an array i.e. Addition,


Subtraction, Multiplication and Division and store the result in another array.
#include<stdio.h>
#include<conio.h>
void main()
{
int i,a[5],b[5],c[5];
clrscr();
printf("\nReading the 1st array\n");
for (i=0;i<5;i++)
{
printf("Enter the value");
scanf("%d",&a[i]);
}
printf("\nReading the 2nd array\n");
for (i=0;i<5;i++)
{
printf("Enter the value");
scanf("%d",&b[i]);
}
printf("\nThe output of addition of 2 array is\n");
for(i=0;i<5;i++)
{
c[i]=a[i]+b[i];
printf("\nthe sum of %d & %d is %d",a[i],b[i],c[i]);
}
getch();
}

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

printf("\nReverse of string: %s", s1);


}
if(choice2==2)
{
char str[100], temp;
int i=0, j = 0;
printf("\nEnter the string :");
scanf("%s",&str);
j = strlen(str) - 1;
while (i < j)
{
temp = str[i];
str[i] = str[j];
str[j] = temp;
i++;
j--;
}
printf("\nReverse string is :%s", str);
}
}
if(choice==2)
{
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);
strcat(a,b);
printf("String obtained on concatenation is %s\n",a);
}
if(choice2==2)
{
char a[10],b[10],c[40];
int i,j;
printf("\n\nENTER FIRST STRING:");
scanf("%s",&a);
printf("\n\nENTER SECOND STRING:");
scanf("%s",&b);
for(i=0;a[i]!='\0';i++)
c[i]=a[i];
for(j=0;a[j]!='\0';j++)
{
c[i]=b[j];
i++;
}
23

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

18. Write a program to detect the occurrence of a number in a string.


#include<stdio.h>
#include<conio.h>
void main()
{
int nod;
char *str;
nod=0;
clrscr();
printf("Enter any string : ");
gets(str);
while (*str != '\0')
{
if (isdigit(*str)) /* counting number of digits. */
++nod;
str++;
}
printf("\nNumber of digits %d", nod);
getch();
}

OUTPUT:

19. Write a program to accept a string up to 15 character, and display the


position of a character in a separate line.
#include<stdio.h>
#include<conio.h>
25

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

21. Write a program to generate a palindrome.


#include<stdio.h>
#include<conio.h>
void main()
{
int num,n,rem,a=0,i;
clrscr();
printf("Enter limit to print all palindrome no. upto that limit \n");
scanf("%d",&num);
for(i=1;i<=num;i++)
{
n=i;
do
{
rem=n%10;
n=n/10;
a=a*10+rem;
}while(n>0);
if(a==i)
{
printf("%d \t",a);
}
a=0;
}
getch();
}

OUTPUT:

28

22. Write a program to add to pointer addresses of a pointer variable.


#include<stdio.h>
#include<conio.h>
void main()
{
int a, b, *p, *q, sum;
clrscr();
printf("Enter two integers to add\n");
scanf("%d%d", &a, &b);
p = &a;
q = &b;
sum =*p+*q;
printf("\nSum of two numbers using pointers = %d\n",sum);
getch();
}

OUTPUT:

23. Write a program to find the factorial of a number using recursion.


#include<stdio.h>
#include<conio.h>
int fact(int);
int main()
{
int num,f;
clrscr();
printf("\nEnter a number: ");
scanf("%d",&num);
f=fact(num);
printf("\nFactorial of %d is: %d",num,f);
getch();
}
29

int fact(int n)
{
if(n==1)
return 1;
else
return(n*fact(n-1));
}

OUTPUT:

24. Write a program to perform different arithmetic operations using pointers


#include<stdio.h>
#include<conio.h>
void main()
{
int a, b, *p1, *p2, x, y, z;
int add,mul,sub,div;
clrscr();
printf("Enter the value of a and b:-");
scanf("%d%d",&a,&b);
p1 = &a;
p2 = &b;
add = *p1 + *p2;
sub = *p1 - *p2;
mul = *p1 * *p2;
div= *p1/ *p2;
printf("\nAddition :- %d",add);
printf("\nSubtraction :- %d",sub);
printf("\nMultiplication :- %d",mul);
printf("\nDevidation :- %d",div);
getch();
}

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

26. Write a program to find the sum of 5 digit number:


a. Without using recursion.
#include<stdio.h>
#include<conio.h>
void main()
{
int n, sum = 0, rem;
clrscr();
printf("Enter an integer\n");
scanf("%d",&n);
while(n != 0)
{
rem = n % 10;
sum = sum + rem;
n = n / 10;
}
printf("Sum of digits of entered number = %d\n",sum);
getch();
}

OUTPUT:

b. With using recursion.


#include<stdio.h>
#include<conio.h>
int sum(int);
void main()
{
int num,x;
clrscr();
printf("\nEnter a number: ");
scanf("%d",&num);
x=sum(num);
printf("Sum of the digits of %d is: %d",num,x);
getch();
}
int r,s;
int sum(int n)
{
if(n)
{
32

r=n%10;
s=s+r;
sum(n/10);
}
else
return s;
}

OUTPUT:

27. Write a program to obtain Fibonacci series by using recursion.


#include<stdio.h>
#include<conio.h>
void printFibonacci(int);
void main()
{
int k,n;
long int i=0,j=1,f;
clrscr();
printf("Enter the range of the Fibonacci series: ");
scanf("%d",&n);
printf("Fibonacci Series: ");
printf("%d %d ",0,1);
printFibonacci(n);
getch();
}
void printFibonacci(int n)
{
static long int first=0,second=1,sum;
if(n>0)
{
sum = first + second;
first = second;
second = sum;
printf("%ld ",sum);
printFibonacci(n-1);
}
}

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:

29. Write a program to copy the content of one file to another.


#include<stdio.h>
34

#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

You might also like