C Programs 2016
C Programs 2016
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();
}
#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
#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();
}
#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
#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();
}
#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;
}
#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();
}
#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);
8
By: Anuj Bhardwaj
NCS101 ‘C’ Programs B.Tech IICS/EC/EN
#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]);
#include <stdio.h>
void main()
{
int i, first, last, middle, n, search, a[100];
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();
}
#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();
}
#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
#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
#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();
}
#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();
}
#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();
}
#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();
}
#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);
}
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(); }
#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();
}
#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();
}
#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]);
return 0;
}
Or
#include<stdio.h>
int main(){
int a[3][3],i,j;
int determinant=0;
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]));
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);
}
}
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);
22
By: Anuj Bhardwaj
NCS101 ‘C’ Programs B.Tech IICS/EC/EN
23
By: Anuj Bhardwaj
NCS101 ‘C’ Programs B.Tech IICS/EC/EN
return 0;
}
24
By: Anuj Bhardwaj