0% found this document useful (0 votes)
110 views

C Programs 2016

This document contains 16 C programming problems/exercises related to basic programming concepts like input/output, arithmetic operations, conditional statements, loops, functions etc. Each problem is presented with its problem statement and sample code to solve it. The problems cover calculating marks and percentages, simple and compound interest, area and circumference of a circle, temperature conversion, variable swapping, checking even/odd/prime numbers, finding maximum of 3 numbers, generating tables and Fibonacci series.

Uploaded by

Fresh Epic
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
110 views

C Programs 2016

This document contains 16 C programming problems/exercises related to basic programming concepts like input/output, arithmetic operations, conditional statements, loops, functions etc. Each problem is presented with its problem statement and sample code to solve it. The problems cover calculating marks and percentages, simple and compound interest, area and circumference of a circle, temperature conversion, variable swapping, checking even/odd/prime numbers, finding maximum of 3 numbers, generating tables and Fibonacci series.

Uploaded by

Fresh Epic
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 24

NCS101 ‘C’ Programs B.

Tech IICS/EC/EN

1. WAP that accepts the marks of 5 subjects and finds the sum and
percentage marks obtained by the student.

#include<stdio.h>
void main()
{
int s1,s2,s3,s4,s5,sum,per,tm;
printf("Enter the marks of s1 s2 s3 s4 s5=");
scanf("%d%d%d%d%d",&s1,&s2,&s3,&s4,&s5);
printf("Enter total marks=");
scanf("%d",&tm);
sum=(s1+s2+s3+s4+s5);
per=(sum*100)/tm;
printf("sum=%d, per=%d=", sum, per);
getch();
}

2. WAP that calculates the Simple Interest and Compound Interest. The
Principal , Amount, Rate of Interest and Time are entered through
the keyboard.

#include <stdio.h>
#include <conio.h>
void main()
{ float p, r, si;
int t;
clrscr();
printf("Enter the values of p,r and t\n");
scanf ("%f %f %d", &p, &r, &t);
si = (p * r * t)/ 100.0;
printf ("Amount = Rs. %5.2f\n", p);
printf ("Rate = Rs. %5.2f%\n", r);
printf ("Time = %d years\n", t);
printf ("Simple interest = %5.2f\n", si);
getch();
}

3. WAP to calculate the area and circumference of a circle.

#include<stdio.h>
void main()
{
float r,circum,area,pi=3.14;
printf("Enter the radius=>”);
scanf(“%f”,&r);
area = pi*r*r;
circum = 2*pi*r;
printf("Area of circle =%f,circumference=%f=>”,area,circum);
getch();
}

1
By: Anuj Bhardwaj
NCS101 ‘C’ Programs B.Tech IICS/EC/EN

4. WAP that accepts the temperature in Fahrenheit and converts into


Centigrade using the formula C/5=(F-32)/9.

#include<stdio.h>
void main()
{
float c,f;
printf("Enter the temp in fahrenheit=>”);
scanf(“%f”,&f);
c = (5*(f-32))/9;
printf("temperature in celsius=%f=>”c);
getch();
}
5. WAP that swaps values of two variables using a third variable.

#include<stdio.h>
void main()
{
int a,b,c;
printf(“Enter the value of a & b=>”);
scanf(“%d%d”,&a,&b);
c = a;
a = b;
b = c;
printf(“After swapping value of a and b:%d,%d”=>a,b);
getch();
}
6. WAP that checks whether the two numbers entered by the user are
equal or not.

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
printf("Enter the Value of a and b=");
scanf("%d%d",&a,&b);
if(a==b)
printf("Numbers are equal");
else
printf("numbers are not equal");
getch();
}
7. WAP to find the greatest of three numbers

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
printf("Enter the value of a,b,c=");
scanf("%d%d%d",&a,&b,&c);
if((a>b)&&(a>c))

2
By: Anuj Bhardwaj
NCS101 ‘C’ Programs B.Tech IICS/EC/EN

printf("A is greater");
else
if((b>a)&&(b>c))
printf("B is greater");
else
printf("C is greater");
getch();
}
8. WAP that finds whether a given number is even or odd

#include<stdio.h>
#include<conio.h>
void main()
{
int num;
printf("Enter the number=");
scanf("%d,&num);
if(num%2==0)
printf("number is even");
else
printf("number is odd");
getch();
}

9. WAP that tells whether a given year is a leap year or not.

#include<stdio.h>
#include<conio.h>
void main()
{
int year;
clrscr();
printf("\n\tEnter the year: ");
scanf("%d",&year);
if(year%4==0)
printf("\nYear is leap year");
else
printf("\nYear is NOT leap year");
getch();
}
10. WAP that accepts marks of five subjects and finds percentage and
prints grades according to the following criteria:
Between 90-100%--------------Print ‘A’
80-90%----------------------------Print ‘B’
60-80%---------------------------Print ‘C’
Below 60%----------------------Print ‘D’

#include<stdio.h>
void main()
{
int s1,s2,s3,s4,s5,sum,per,tm;
printf("Enter the marks of s1 s2 s3 s4 s5=");
scanf("%d%d%d%d%d",&s1,&s2,&s3,&s4,&s5);

3
By: Anuj Bhardwaj
NCS101 ‘C’ Programs B.Tech IICS/EC/EN

printf("Enter total marks=");


scanf("%d",&tm);
sum=(s1+s2+s3+s4+s5);
per=(sum*100)/tm;
if(per>=90&&per<=100)
printf("grade A");
else
if(per>=80&&per<90)
printf("grade B");
else
if(per>60&&per<80)
printf("grade C");
else
if(per<=60)
printf("grade D");
getch();
}
11. WAP to print the sum of all numbers up to a given number.

#include<stdio.h>
void main()
{
int i,n,sum=0;
printf(“enter the range=”)
scanf("%d",&n);
for ( i = 1 ; i <= n ; i++ )
{
sum = sum + i;
}
Printf(“Sum of elements=%d”,sum);
getch();
}
12. WAP to find the factorial of a given number

#include<stdio.h>
#include<conio.h>
void main()
{
int i,n,fact=1;
printf("The the number whose factorial is required:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
fact=fact*i;
}
printf(“fact=%d”,fact);
getch();
}

13. WAP to generate the table of any number.

#include<stdio.h>
#include<conio.h>
void main()

4
By: Anuj Bhardwaj
NCS101 ‘C’ Programs B.Tech IICS/EC/EN

{
int i,n,t;
printf("The the number whose table is to be generated:");
scanf("%d",&n);
for(i=1;i<=10;i++)
{
t=n*i;
printf(“%d x %d=%d”,n,i,t);
}
getch();
}
14. WAP to print sum of even and odd numbers from 1 to N numbers.

#include<stdio.h>
#include<conio.h>
void main()
{
int i,n,even=0,odd=0;
printf("The the range of numbers :");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
if(i%2==0)
even=even+i;
else
odd=odd+i;
}
printf(“even sum=%d, odd sum=%d”,even,odd);
getch();
}
15. WAP to print the Fibonacci series.

#include<stdio.h>
#include<conio.h>
void main()
{
int a=0,b=1,c,i,n;
printf(“Enter the range=>”);
scanf(“%d”,&n)
printf("The fibonacci series is:");
printf("%d %d",a,b);
for(i=1;i<=n;i++)
{
c=a+b;
a=b;
b=c;
printf("%d",c);
}
getch();}
16. WAP to check whether the entered number is prime or not.

#include<stdio.h>
#include<conio.h>
void main()

5
By: Anuj Bhardwaj
NCS101 ‘C’ Programs B.Tech IICS/EC/EN

{
int i,n,c=0;
printf("Enter any number:");
scanf("%d",&n);
for(i=2;i<=n/2;i++)
{
if(n%i==0)
{
c++;
break;
}
}
if(c==0)
printf(“number is prime”);
else
printf(“number is not prime”);
getch();
}

Or
#include <stdio.h>
main() {

int n, i, c = 0;
printf("Enter any number n:");
scanf("%d", &n);
/*logic*/
for (i = 1; i <= n; i++) {
if (n % i == 0) {
c++;
}
}
if (c == 2) {
printf("n is a Prime number");
}
else {
printf("n is not a Prime number");
}
return 0;
}

17. WAP to find the sum of digits of the entered number.

#include<stdio.h>
#include<conio.h>
void main()
{
int num,n,sum=0;
clrscr();
printf("\nEnter the any digit ==>: \n");
scanf("%d",&num);
printf("\nThe reverse of any digit numbers are: ");
while(num!=0)
{

6
By: Anuj Bhardwaj
NCS101 ‘C’ Programs B.Tech IICS/EC/EN

n=num%10;
num=num/10;
sum=sum+n;
printf("%d",n);
}
printf("\n Sum of above digits are: %d",sum);
getch();
}

18. WAP to find the reverse of a number.

#include<stdio.h>
#include<conio.h>
void main()
{
int i,num,d,rev=0;
printf("enter the Number:");
scanf("%d",&num);
n=num;
while(n!=0)
{
d=n%10;
rev=(rev*10)+d;
n=n/10;
}
printf("reverse is= %d”, rev);
getch();
}
19. WAP to print Armstrong numbers from 100 to 500.

#include<stdio.h>
#include<conio.h>
void main()
{
int i,n,d,sum=0;
for(i=100;i<=500;i++)
{
n=i;
while(n!=0)
{
d=n%10;
sum=sum+(d*d*d*);
n=n/10;
}
if (sum==i)
printf("Armstrong numbers are= %d”, i);
}
getch();
}
20. WAP to find the day of the week

#include<stdio.h>
#include<conio.h>
void main()

7
By: Anuj Bhardwaj
NCS101 ‘C’ Programs B.Tech IICS/EC/EN

{
int day;
clrscr();
printf("\n\tEnter the day: ");
scanf("%d",&day);
if(day==1)
printf("\nDAY is MONDAY");
else
if(day==2)
printf("\nDAY is Tuesday");
else
if(day==3)
printf("\nDAY is Wedneday");
else
if(day==4)
printf("\nDAY is Thursday");
else
if(day==5)
printf("\nDAY is Friday");
else
if(day==6)
printf("\nDAY is Saturday");
else
if(day==7)
printf("\n*********HOLIDAY is SUNDAY");
else
printf("\n*********This is not the day of the week illegal
entry");
getch();
}

21. WAP that simply takes elements of the array from the user and finds
the sum of these elements.

#include<stdio.h>
#include<conio.h>
void main()
{
int a[100], i,n,sum=0;
printf("Enter number of elements\n");
scanf("%d",&n);

for ( i = 1 ; i <= n ; i++ )


{
scanf(“%d”,&a[i]);
}
for ( i = 1 ; i <= n ; i++ )
{
sum=sum + a[i];
}
Printf(“Sum of elements=%d”,sum);
getch();
}

8
By: Anuj Bhardwaj
NCS101 ‘C’ Programs B.Tech IICS/EC/EN

22. WAP to search an element in a array using Linear Search.

#include <stdio.h>
void main()
{
int a[100], search, i, n;
printf("Enter the number of elements in array\n");
scanf("%d",&n);
printf("Enter %d integer(s)\n", n);
for (i = 0; i < n; i++)
scanf("%d", &a[i]);

printf("Enter the number to search\n");


scanf("%d", &search);
for (i = 0; i < n; i++)
{
if (a[i] == search)
{
printf("%d is present at location %d.\n", search, i+1);
break;
}
}
if (i == n)
printf("%d is not present in array.\n", search);
getch();
}
23. WAP to search an element in a array using Binary Search.

#include <stdio.h>
void main()
{
int i, first, last, middle, n, search, a[100];

printf("Enter number of elements\n");


scanf("%d",&n);

printf("Enter %d integers\n", n);

for ( i = 0 ; i < n ; i++ )


scanf("%d",&a[i]);
printf("Enter value to find\n");
scanf("%d",&search);
first = 0;
last = n - 1;
middle = (first+last)/2;
while( first <= last )
{
if ( a[middle] < search )
first = middle + 1;
else if ( a[middle] == search )
{
printf("%d found at location %d.\n", search, middle+1);
break;

9
By: Anuj Bhardwaj
NCS101 ‘C’ Programs B.Tech IICS/EC/EN

}
else
last = middle - 1;
middle = (first + last)/2;
}
if ( first > last )
printf("Not found! %d is not present in the list.\n", search);
getch();
}
24. WAP to find the minimum and maximum element of the array.

#include <stdio.h>
void main()
{
int a[100], max,min, n, i, loc = 1;
printf("Enter the number of elements in array\n");
scanf("%d", &n);
printf("Enter the elements:");
for (i= 0; i < n; i++)
scanf("%d", &a[i]);
max = a[0];
min = a[0];
for (i = 1; i < n; i++)
{
if (max < a[i])
max = a[i];
if (min> a[i])
min = a[i];
}
printf(“max element is=%d”, max);
printf(“min element is=%d”, min);
getch();
}

25. WAP to simulate the calculator using switch statement

#include<stdio.h>
#include<conio.h>
void main()
{
int choice,a,b,c;
clrscr();
printf("\n\t C A L C U L A T O R");
printf("\n\t××××××××××××××××××××××××\n");
printf("\n\t1->ADDITION");
printf("\n\t2->SUBSTRACTION");
printf("\n\t3->MULTIPLICATION");
printf("\n\t4->DIVISION");
printf("\n\t8->Exit");
printf("\n\t Enter your choice: ");
scanf("%d",&choice);
printf("\n\tEnter the value of two numbers: ");
scanf("%d%d",&a,&b);
switch(choice)

10
By: Anuj Bhardwaj
NCS101 ‘C’ Programs B.Tech IICS/EC/EN

{
case 1:
c=a+b;
printf("\nSUM = %d",c);
break;
case 2:
c=a-b;
printf("\nSUBTRACTION = %d",c);
break;
case 3:
c=a*b;
printf("\nMultiplication = %d",c);
break;
case 4:
c=a/b;
printf("\ndivision = %d",c);
break;
default:
printf("\n******Wrong choice****");
break;
}
getch();
}

26. WAP to find the concept about 2D array(print matrix)

#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,r,c,n;
int matrix[10][10];
printf("Enter the order of the matrix\n");
scanf("%d %d",&r,&c);
printf("Enter the element of the matrix\n");
for (i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&matrix[i][j]);
}
}
printf("Matrix is:-\n");
for (i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("%3d",matrix[i][j]);
}
printf("\n");
}
getch();
}

11
By: Anuj Bhardwaj
NCS101 ‘C’ Programs B.Tech IICS/EC/EN

27. WAP to multiply two matrices of order nxn.

#include<stdio.h>
#include<conio.h>
#define row 3
#define col 3
void main()
{
int a[row][col],b[row][col],mul[row][col],i,j,k;
clrscr();
p1rintf("\n\t The MATRIX A: \n" );
for(i=0;i<row;i++)
for(j=0;j<col;j++)
scanf("%d",&a[i][j]);
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
printf("\t%d",a[i][j]);
}
printf("\n\n");
}
printf("\n\t The MATRIX B: \n" );
for(i=0;i<row;i++)
for(j=0;j<col;j++)
scanf("%d",&b[i][j]);
for(i=0;i<row;i++)

for(j=0;j<col;j++)
{
printf("\t%d",b[i][j]);
}
printf("\n\n");
}
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
mul[i][j]=0;
for(k=0;k<3;k++)
{
mul[i][j]=mul[i][j]+a[i][k]*b[k][j];
}}}
printf("\n\nThe sum of above matrix is: \n\n");
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
printf("\t%d",mul[i][j]);
}
printf("\n\n");
}
getch();
}

12
By: Anuj Bhardwaj
NCS101 ‘C’ Programs B.Tech IICS/EC/EN

28. WAP that finds the sum of two matrices.

#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,r,c,n;
int a[10][10], mat2[10][10], matsum[10][10];
printf("Enter the order of the matrix\n");
scanf("%d x %d",&r,&c);
printf("Enter the element of the matrix 1\n");
for (i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&mat1[i][j]);
}
}
printf("Enter the element of the matrix 2\n");
for (i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&mat2[i][j]);
}
}
for (i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
matsum[i][j]=mat1[i][j]+mat2[i][j];
}
}
printf("The sum of Matrices is.......\n");
for (i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("%3d",matsum[i][j]);
}
printf("\n");
}
getch();
}

29. WAP to change the case of the string upper to lower vice versa.

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int i;
char a[20];

13
By: Anuj Bhardwaj
NCS101 ‘C’ Programs B.Tech IICS/EC/EN

clrscr();
printf("\n enter the string:-");
gets(a);
for(i=0;i<strlen(a);i++)
{
if(a[i]>=65 && a[i]<=90)
a[i]+=32;
else if (a[i]>=97 && a[i]<=122)
a[i]-=32;
}
printf("\n\n case changed:-%s",a);
getch(); }
30. WAP to search a character in a string

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int i;
char ch[50],c;
clrscr();
printf("\n enter the string:-");
gets(ch);
printf("\n enter a character to search:-");
scanf("%c",&c);
for (i=0;i<strlen(ch);i++)
{
if(c==ch[i]|| c==ch[i]+32|| c==ch[i]-32)
{
textcolor(6);
cprintf("%c",ch[i]);
}
else
printf("%c",ch[i]);
}
getch();
}

31. WAP To count the number of vowels in a sentence using switch


statement.

#include<stdio.h>
#include<ctype.h>
#include<string.h>
void main()
{
int i,c=0;
char ch[10];
clrscr();
printf("\n Enter the string:-");
gets(ch);
for (i=0;i<strlen(ch);i++)
{

14
By: Anuj Bhardwaj
NCS101 ‘C’ Programs B.Tech IICS/EC/EN

switch(tolower(ch[i]))
{
case 'a':++c;
break;
case 'e':++c;
break;
case 'i':++c;
break;
case 'o':++c;
break;
case 'u':++c;
break;
}
}
printf("\n there are %d vowel in the string<%s>",c,ch);
getch();
}

32. WAP to check the string palindrome or not

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int b=0,a,i;
char s[10];
clrscr();
printf("\n\n Enter the string:-");
gets(s);
a=strlen(s);
for (i=0;i<a;i++)
{
if(s[a-1-i]==s[i])
++b;
}
if(b==a)
printf("\n\n String Entered is Palindrome.");
else
printf("\n\n String Entered is not palindrome.");
getch();
}

33. WAP to check the Number is palindrome or not.

#include<stdio.h>
#include<conio.h>
void main()
{
int a,n,b,c=0;
printf("enter the Number:");
scanf("%d",&a);

15
By: Anuj Bhardwaj
NCS101 ‘C’ Programs B.Tech IICS/EC/EN

n=a;
while(a>0)
{
b=a%10;
c=(c*10)+b;
a=a/10;
}
if(n==c)
printf("\nsince the reverse of %d is %d\n so the no. is
palindrom",n,c);
else
printf("\nsince the reverse of %d is %d\n so the no. is not
palindrom",n,c);
getch();.
}
34. WAP program to check whether given character is alphabet or not

#include<stdio.h>
#include<conio.h>
void main()
{
char ch;
clrscr();
printf("enter a character");
scanf(" %c",&ch);
if((ch>=65&&ch<=90)|| (ch>=97&&ch<=122))
printf("%c is an alphabet",ch);
else
printf("%c is not an alphabet" ,ch);
getch();
}
35. WAP to find length of the string.
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char name[200];
clrscr();
printf("\nEnter the name: ");
gets(name);
printf("\n\tThe length of the string is %d",strlen(name));
getch();
}

36. WAP to swap two elements using the concept of pointers.

#include <stdio.h>
void swap(int * q,int * p)
{
int temp = *p;
*p = *q;
*q = temp;
}

16
By: Anuj Bhardwaj
NCS101 ‘C’ Programs B.Tech IICS/EC/EN

void main()
{
int a = 10, b = 2;
printf("a,b:%d,%d\n", a, b);
swap(&a, &b);
printf("a,b:%d,%d\n", a, b);
}

37. WAP FIND THE ROOTS OF QUADRATIC EQUATION


#include<stdio.h>
#include<conio.h>
#include<math.h>
void main() {
float a,b,c,d,Dr,x1,x2;
clrscr();
printf("Quadratic Equation is of the form:-
\n\n\t\t(Ax^2)+(Bx)+C=D\n\n");
printf("Enter the constants\n A =");
scanf("%f",&a);
printf("\n B =") ;
scanf("%f",&b);
printf("\n C =");
scanf("%f",&c);
printf("\n D =");
scanf("%f",&d);
c=c-d;
Dr=(b*b)-(4*a*c);
if(Dr>=0)
{
x1=((-b+sqrt(Dr))/(2*a));
x2=((-b-sqrt(Dr))/(2*a));
printf("\n Roots are REAL x=%.2f,%.2f",x1,x2);
}
else if(Dr<0)
{
Dr=Dr*(-1);
x1=(-b)/(2*a);
x2=(sqrt(Dr)/(2*a));
printf("\n Roots are IMAGINARY x = %.2f+i%.2f,
%.2f-i%.2f",x1,x2,x1,x2);
}
getch() ;
}

38. WAP to print the following output


1
12
123
1234
123
12
1

17
By: Anuj Bhardwaj
NCS101 ‘C’ Programs B.Tech IICS/EC/EN

#include<stdio.H>
#include<conio.h>
void main()
{
int i=1,j=1;
clrscr();
for(i=1;i<=8;i++)
{
for(j=1;j<=i;j++)
{
printf("%1d",j);
}
printf("\n");
}
for(i=7;i>=1;i--)
{
for(j=1;j<=i;j++)
{
printf("%1d",j);
}
printf("\n\a");
} getch(); }

39. Program: wap to find the sum


sum= 1+2+3+4+………upto n terms.

#include<stdio.h>
#include<conio.h>

void main()
{
int i,n,sum=0
printf("Enter the value of n");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
sum=sum + i;
}
printf("sum is=%d",sum);
getch();
}
40. Program: wap to find the sum
sum= 1 + 22 + 33 + 44 +………
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int i,n,sum=0;
printf("Enter the value of n");
scanf("%d",&n);
for(i=1;i<n;i++)
{
sum=sum+pow(i,i);

18
By: Anuj Bhardwaj
NCS101 ‘C’ Programs B.Tech IICS/EC/EN

}
printf("sum is=%d",sum);
getch();
}

41. Program: wap to find the sum


sum= 1 + 24 + 34 + 44 +………
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int i,n,sum=0;
printf("Enter the value of n");
scanf("%d",&n);
for(i=1;i<n;i++)
{
sum=sum+pow(i,4);
}
printf("sum is=%d",sum);
getch();
}
42. Write a C program to convert the given binary number into decimal

#include <stdio.h>
void main()
{
int num, bnum, dec = 0, base = 1, rem ;
printf("Enter a binary number(1s and 0s)\n");
scanf("%d", &num); /*maximum five digits */
bnum = num;
while( num > 0)
{ rem = num % 10;
dec = dec + rem * base;
num = num / 10 ;
base = base * 2;
} printf("The Binary number is = %d\n", bnum);
printf("Its decimal equivalent is =%d\n", dec);
getch();
}

43. WAP in C for Determinant of 3X3 matrix:

#include<stdio.h>

int main(){

int a[3][3],i,j;

long determinant;
printf("Enter the 9 elements of matrix: ");
for(i=0;i<3;i++)
for(j=0;j<3;j++)

19
By: Anuj Bhardwaj
NCS101 ‘C’ Programs B.Tech IICS/EC/EN

scanf("%d",&a[i][j]);

printf("\nThe matrix is\n");


for(i=0;i<3;i++){
printf("\n");
for(j=0;j<3;j++)
printf("%d\t",a[i][j]);
}

determinant = a[0][0]*((a[1][1]*a[2][2]) - (a[2][1]*a[1][2])) -


a[0][1]*(a[1][0]*a[2][2] - a[2][0]*a[1][2]) +
a[0][2]*(a[1][0]*a[2][1] - a[2][0]*a[1][1]);

printf("\nDeterminant of 3X3 matrix: %ld",determinant);

return 0;
}
Or
#include<stdio.h>
int main(){
int a[3][3],i,j;
int determinant=0;

printf("Enter the 9 elements of matrix: ");


for(i=0;i<3;i++)
for(j=0;j<3;j++)
scanf("%d",&a[i][j]);

printf("\nThe First matrix is\n");


for(i=0;i<3;i++){
printf("\n");
for(j=0;j<3;j++)
printf("%d\t",a[i][j]);
}

for(i=0;i<3;i++)
determinant = determinant +
(a[0][i]*(a[1][(i+1)%3]*a[2][(i+2)%3] -
a[1][(i+2)%3]*a[2][(i+1)%3]));

printf("\nDeterminant of matrix is: %d",determinant);

return 0;
}
44. C Program to Sort Elements in Lexicographical Order (Dictionary Order)

#include<stdio.h>
#include <string.h>
int main(){
int i,j;
char str[10][50],temp[50];
printf("Enter 10 words:\n");
for(i=0;i<10;++i)
gets(str[i]);
for(i=0;i<9;++i)

20
By: Anuj Bhardwaj
NCS101 ‘C’ Programs B.Tech IICS/EC/EN

for(j=i+1;j<10 ;++j){
if(strcmp(str[i],str[j])>0)
{
strcpy(temp,str[i]);
strcpy(str[i],str[j]);
strcpy(str[j],temp);
}
}

printf("In lexicographical order: \n");


for(i=0;i<10;++i){
puts(str[i]);
}
return 0;
}

45. Write a program in C sort n integer’s numbers .


#include <stdio.h>
void main()
{
int a[100], i,temp, n;
printf("Enter the number of elements in array\n");
scanf("%d",&n);
printf("Enter %d integer(s)\n", n);
for (i = 0; i < n; i++)
scanf("%d", &a[i]);

for (i = 0; i < n-1; i++)


{
for (j = 0; i < n-i-1; i++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
printf("Array after sorting\n");
for (i = 0; i < n; i++)
{
printf("%d", a[i]);
}
getch();
}
46. Factorial of a number by recursion
#include<stdio.h>
void main( )
{
int n, fact;
printf(“Enter the number whose factorial required=”);
scanf(“%d”,&n);
fact=rec(n);

21
By: Anuj Bhardwaj
NCS101 ‘C’ Programs B.Tech IICS/EC/EN

printf(“Factorial=%d”,fact);
}
int rec(int x)
{
int f;
if(x==1)
return(1);
else
f=x*rec(x-1);
return(f);
}
47. Write a program in C using structure to create a library information system which
includes the following items: book number, name of the author, title of the book,
publisher’s name, cost of the book.
Answer: #include <stdio.h>
struct library
{
int b_num;
char b_name[20], author[20],publisher[20];
float cost;
}z;
void main(){
printf("Enter library information: \n");
printf("\nFor Book number=");
scanf(“%d”,&z.b_num)
printf("Enter Book name: ");
scanf("%s",z.b_name);
printf("Enter Author name: ");
scanf("%s",z.author);
printf("Enter publisher name: ");
scanf("%s",z.publisher);

printf("Enter cost: ");


scanf("%f",&z.cost);
printf("\n");
}
printf("Displaying library information : \n\n");
printf(“%d”,z.b_num)
printff("%s",z.b_name);
printff("%s",z.author);
printf("%s",z.publisher);
printf("%f",z.cost);
}
getch();
}
48 .Write a simple database program in C which stores personal details of 100 persons such
as Name, Date of Birth, Address, Phone number etc.
#include <stdio.h>
struct student
{
int r_num, ph_num;
char name[20],address[20];
}s;
void main()
{
printf("Enter person information: \n");

22
By: Anuj Bhardwaj
NCS101 ‘C’ Programs B.Tech IICS/EC/EN

printf("\nFor Roll number=");


scanf(“%d”,&s.r_num)
printf("Enter Phone name: ");
scanf("%d",&s.ph_name);
printf("Enter name: ");
scanf("%s",s.name);
printf("Enter address: ");
scanf("%f",&s.address);
printf("\n");
printf("Displaying person information :\n\n");
printf(“%d”,s.r_num)
printff("%d",s.ph_num);
printff("%s",s.name);
printf("%s",s.address);
getch();
}OR
#include <stdio.h>
struct student
{
char name[50];
int roll;
float marks;
};
int main()
{
struct student s;
printf("Enter information of students:\n\n");
printf("Enter name: ");
scanf("%s",s.name);
printf("Enter roll number: ");
scanf("%d",&s.roll);
printf("Enter marks: ");
scanf("%f",&s.marks);
printf("\nDisplaying Information\n");
printf("Name: %s\n",s.name);
printf("Roll: %d\n",s.roll);
printf("Marks: %.2f\n",s.marks);
return 0;
}
.
49.program to check perfect number
#include<stdio.h>
int main(){
int n,i=1,sum=0;
printf("Enter a number: ");
scanf("%d",&n);
while(i<n){
if(n%i==0)
sum=sum+i;
i++;
}
if(sum==n)
printf("%d is a perfect number",i);
else
printf("%d is not a perfect number",i);

23
By: Anuj Bhardwaj
NCS101 ‘C’ Programs B.Tech IICS/EC/EN

return 0;
}

24
By: Anuj Bhardwaj

You might also like