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

Program List

The document contains C code snippets for various programs that perform calculations and logical operations. The programs include: 1. Calculating sum and percentage of marks from 5 subjects. 2. Calculating simple and compound interest given principal, rate, time. 3. Calculating circumference and area of a circle given radius. 4. Converting Celsius to Fahrenheit. 5. Swapping values of two variables. 6. Checking if two numbers are equal. 7. Finding greatest of three numbers. 8. Checking if a number is even or odd. 9. Checking if a year is a leap year. 10. Finding grade based on percentage and

Uploaded by

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

Program List

The document contains C code snippets for various programs that perform calculations and logical operations. The programs include: 1. Calculating sum and percentage of marks from 5 subjects. 2. Calculating simple and compound interest given principal, rate, time. 3. Calculating circumference and area of a circle given radius. 4. Converting Celsius to Fahrenheit. 5. Swapping values of two variables. 6. Checking if two numbers are equal. 7. Finding greatest of three numbers. 8. Checking if a number is even or odd. 9. Checking if a year is a leap year. 10. Finding grade based on percentage and

Uploaded by

sk262406s
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 30

#include<stdio.

h>

/*1. WAP that accepts the marks of 5 subjects and finds the sum and
percentage marks obtained by the student.*/
void main()

{ int hindi, english, science,math,computer,sum ;

float per;

printf("Enter marks of Hindi=");

scanf("%d",&hindi);

printf("Enter marks of English=");

scanf("%d",&english);

printf("Enter marks of Science=");

scanf("%d",&science);

printf("Enter marks of Math=");

scanf("%d",&math);

printf("Enter marks of Computer=");

scanf("%d",&computer);

sum=hindi+english+science+math+computer;

printf("Sum of marks=%d",sum);

per=(float)sum/5;

printf("Percentage of marks=%f",per);

Output:
Enter marks of Hindi=12

1Enter marks of English=1

Enter marks of Science=13

Enter marks of Math=14

Enter marks of Computer=2

Sum of marks=42Percentage of marks=8.400000


#include<stdio.h>

#include<math.h>

/*2. WAP that calculates the Simple Interest and Compound Interest.
The Principal, Amount, Rate of Interest and Time are entered through
the keyboard.*/
void main()

float p, r, t, a, si, ci;

printf("Enter Principle=");

scanf("%f",&p);

printf("Enter Rate=");

scanf("%f",&r);

printf("Enter Time=");

scanf("%f",&t);

si=(p*r*t)/100;

printf("Simple Interest=%f",si);

a = p*(pow((1 + r / 100), t));

ci = a - p;

printf("\nCompound Interest=%f",ci);

Output
Enter Principle=1250

Enter Rate=4

Enter Time=2

Simple Interest=100.000000

Compound Interest=101.999878
#include<stdio.h>

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


void main()

float r,c,a;

printf("Enter radius=");

scanf("%f",&r);

float pi=3.14;

c=2*pi*r;

a=pi*r*r;

printf("\nCircumference of a circle=%f",c);

printf("\nArea of a circle=%f",a);

Output:
Enter radius=22
Circumference of a circle=138.160004
Area of a circle=1519.760010
#include<stdio.h>

/*4. WAP that accepts the temperature in Centigrade and


converts into Fahrenheit using the formula C/5=(F-32)/9. */
void main()
{

float c,f;
printf("Enter Centigrade=");
scanf("%f",&c);
f=(9*c)/5+32;

printf("Fahrenheit=%f",f);
}

Output:
Enter Centigrade=52
Fahrenheit=125.599998
#include<stdio.h>
/*5. WAP that swaps values of two variables using a third variable. */
void main()

{
int a,b,temp;
printf("Enter a=");
scanf("%d",&a);
printf("Enter b=");

scanf("%d",&b);

temp=a;
a=b;
b=temp;

printf("\nAfter swapping");
printf("\na=%d",a);
printf("\nb=%d",b);
}
Output:
Enter a=2
Enter b=3
After swapping
a=3
b=2
#include<stdio.h>

/*6. WAP that checks whether the two numbers entered by


the user are equal or not. */
void main()
{

int a,b;
printf("Enter a=");
scanf("%d",&a);
printf("Enter b=");
scanf("%d",&b);

if(a==b)
{
printf("\na and b are equal.");
}

else
{
printf("\na and b are not equal.");
}
}

Output:
Enter a=2
Enter b=2
a and b are equal.
#include<stdio.h>

/*WAP to find the greatest of three numbers. */


void main()
{
int a,b,c;
printf("Enter a=");
scanf("%d",&a);
printf("Enter b=");
scanf("%d",&b);
printf("Enter c=");
scanf("%d",&c);

if(a>b)
{
if(a>c)
{
printf("\na is greatest.");
}
else
{
printf("\nc is greatest.");
}
}
else
{
if(b>c)
{
printf("\nb is greatest.");
}
else
{
printf("\nc is greatest.");
}
}
}

Output:

Enter a=2

Enter b=3
Enter c=5
c is greatest.
#include<stdio.h>
/* WAP that finds whether a given number is even or odd. */
void main()
{
int n;
printf("Enter a number=");
scanf("%d",&n);

if(n%2==0)
{
printf("\nn is even number.");
}
else
{
printf("\nn is odd number.");
}
}

Output:

Enter a number=3

n is odd number.
#include<stdio.h>
/* WAP that tells whether a given year is a leap year or not. */
void main()
{
int year;
printf("Enter a number=");
scanf("%d",&year);

if((year%4==0||year%400==0)&&year%100!=0)
{
printf("\nYear is leap year.");
}
else
{
printf("\nYear is not leap year.");
}
}

output:

Enter a number=2004
Year is leap year.
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>
/* WAP that accepts the marks of 5 subjects and finds the sum and percentage marks obtained by the
student.*/
void main()
{
int hindi, english, science,math,computer,sum ;
float per;
printf("Enter marks of Hindi=");
scanf("%d",&hindi);
printf("Enter marks of English=");
scanf("%d",&english);
printf("Enter marks of Science=");
scanf("%d",&science);
printf("Enter marks of Math=");
scanf("%d",&math);
printf("Enter marks of Computer=");
scanf("%d",&computer);

sum=hindi+english+science+math+computer;
printf("\nSum of marks=%d",sum);

per=(float)sum/5;
printf("\nPercentage of marks=%f",per);

if(per>=90&&per<=100)
{
printf("\nGrade A");
}
else if(per>=80&&per<90)
{
printf("\nGrade B");
}
else if(per>=60&&per<80)
{
printf("\nGrade C");
}
else if(per<60)
{
printf("\nGrade D");
}
}

Output:
Enter marks of Hindi=99
Enter marks of English=89
Enter marks of Science=79

Enter marks of Math=66


Enter marks of Computer=99
Sum of marks=432
Percentage of marks=86.400002
Grade B
WAP that takes two operands and one operator from the
user and perform the operation and prints the result by
using Switch statement.
#include<stdio.h>
void main()
{
int choice,a,b;
printf("Select your choice:\n");
printf("1- Add:\n");
printf("2- Sub:\n");
printf("3- Mul:\n");
printf("4- Div:\n");
printf("5- Mod:\n");
printf("Enter number a=");
scanf("%d",&a);
printf("Enter number b=");
scanf("%d",&b);
printf("Enter your choice=");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("Add of a and b %d",(a+b));
break;
case 2:
printf("Sub of a and b %d",(a-b));
break;
case 3:
printf("Mul of a and b %d",(a*b));
break;
case 4:
printf("Div of a and b %d",(a/b));
break;
case 5:
printf("Mod of a and b %d",(a%b));
break;
default:
printf("Wronf choice.");

}
}

Output: Select your choice:


1- Add:
2- Sub:

3- Mul:
4- Div:
5- Mod:
Enter number a=6

Enter number b=3


Enter your choice=3
Mul of a and b 18
WAP to print sum of even and odd numbers from 1 to N
numbers.
#include<stdio.h>
void main()
{
int i,n,sumEven=0,sumOdd=0;
printf("Enter number=");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
if(i%2==0)
{
sumEven=sumEven+i;
}
else
{
sumOdd=sumOdd+i;
}
}
printf("\nSum of even numbers=%d",sumEven);
printf("\nSum of odd numbers=%d",sumOdd);
}

Output: Enter number=10

Sum of even numbers=30


Sum of odd numbers=25
WAP to print the Fibonacci series.
#include<stdio.h>
void main()
{
int i,n,a=0,b=1,c;
printf("Enter number=");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("%d\n",a);
c=a+b;
a=b;
b=c;
}
}

Output:
Enter number=12
0
1

1
2
3
5
8

13
21
34
55
89
WAP to check whether the entered number is prime or
not.
#include<stdio.h>
void main()
{
int i,n,prime=1;
printf("Enter number=");
scanf("%d",&n);
for(i=2;i<=n/2;i++)
{
if(n%i==0)
{
prime=0;
break;
}
}
if(prime==1)
{
printf("Prime Number");
}
else
{
printf("Not Prime Number");
}
}

Output:
Enter number=44
Not Prime Number
WAP to find the sum of digits of the entered number.
#include<stdio.h>
void main()
{
int n,r,sumDigits=0;
printf("Enter number=");
scanf("%d",&n);
while(n>0)
{
r=n%10;
sumDigits=sumDigits+r;
n=n/10;
}
printf("Sum of Digits=%d",sumDigits);
}

Output:
Enter number=125

Sum of Digits=8
WAP to find the reverse of a number.
#include<stdio.h>
void main()
{
int n,r,rev=0;
printf("Enter number=");
scanf("%d",&n);
while(n>0)
{
r=n%10;
rev=rev*10+r;
n=n/10;
}
printf("Reverse number=%d",rev);
}

Output:

Enter number=124
\Reverse number=421
WAP to print Armstrong numbers from 1 to 1000.

A positive number is called armstrong number if it is equal


to the sum of cubes of its digits.
Examples: 153 is Armstrong

(1*1*1)+(5*5*5)+(3*3*3) = 153

#include<stdio.h>
void main()
{
int i=1,r,aNum=0,num;
for(i=1;i<=1000;i++)
{
num=i;
aNum=0;
while(num>0)
{
r=num%10;
aNum=aNum+r*r*r;
num=num/10;
}
if(i==aNum)
{
printf("Armstrong Number=%d\n",i);
}
}
}

Output:
Armstrong Number=1
Armstrong Number=153
Armstrong Number=370

Armstrong Number=371
Armstrong Number=407
WAP to convert binary number into decimal number and
vice versa.
#include<stdio.h>
#include
void main()
{
int n,r,rev=0,p=0;
printf("Enter binary number=");
scanf("%d",&n);
while(n>0)
{
r=n%10;
if(r!=0)
{
rev= rev+(int)pow(2,p);
}
n=n/10;
p++;
}
printf("Decimal number=%d",rev);
}

Output:

Enter binary number=11


Decimal number=3
AP that inputs two arrays and saves sum of corresponding
elements of these arrays in a third array and prints them.
#include<stdio.h>
void main()
{
int i,ar1[10],ar2[10],sum[10];
printf("Enter first array:-\n");
for(i=0;i<=9;i++)
{
printf("ar1[%d]=",i);
scanf("%d",&ar1[i]);
}
printf("Enter second array:-\n");
for(i=0;i<=9;i++)
{
printf("ar2[%d]=",i);
scanf("%d",&ar2[i]);
}

for(i=0;i<=9;i++)
{
sum[i]=ar1[i]+ar2[i];
}

printf("Sum of arrays:-");
for(i=0;i<=9;i++)
{
printf("\nsum[%d]=%d",i,sum[i]);
}

Output:
Enter first array:-
ar1[0]=1 1 1 1 1 1 1 1
ar1[1]=ar1[2]=ar1[3]=ar1[4]=ar1[5]=ar1[6]=ar1[7]=ar1[8]=11

ar1[9]=11
Enter second array:-
ar2[0]=1
ar2[1]=11
ar2[2]=11
ar2[3]=11
ar2[4]=11

ar2[5]=1
ar2[6]=11
ar2[7]=111
ar2[8]=2
ar2[9]=1

Sum of arrays:-
sum[0]=2
sum[1]=12
sum[2]=12
sum[3]=12

sum[4]=12
sum[5]=2
sum[6]=12
sum[7]=112
sum[8]=13
sum[9]=12
WAP to find the minimum and maximum element of the
array.
#include<stdio.h>
void main()
{
int i,ar[10],min,max;
printf("Enter array:-\n");
for(i=0;i<=9;i++)
{
printf("ar[%d]=",i);
scanf("%d",&ar[i]);
}
min=ar[0];
max=ar[0];
for(i=0;i<=9;i++)
{
if(min > ar[i])
{
min=ar[i];
}
if(max < ar[i])
{
max=ar[i];
}
}
printf("\nMin=%d",min);
printf("\nMax=%d",max);
}

Output:
Enter array:-

ar[0]=1 11 11 11 12 22 33 66 66
ar[1]=ar[2]=ar[3]=ar[4]=ar[5]=ar[6]=ar[7]=ar[8]=ar[9]=99
Min=1
Max=99
WAP to search an element in a array using Linear Search.
#include<stdio.h>
void main()
{
int i,ar[10],n,pos=-1;
printf("Enter array:-\n");
for(i=0;i<=9;i++)
{
printf("ar[%d]=",i);
scanf("%d",&ar[i]);
}
printf("Enter number to be search=");
scanf("%d",&n);
for(i=0;i<=9;i++)
{
if(n==ar[i])
{
pos=i+1;
}
}
if(pos==-1)
{
printf("Number not found.");
}
else
{
printf("Position=%d",pos);
}
}

Output:

Enter array:-
ar[0]=1 2 3 4 5 6 7 8 9
ar[1]=ar[2]=ar[3]=ar[4]=ar[5]=ar[6]=ar[7]=ar[8]=ar[9]=22
Enter number to be search=6
Position=6
WAP to sort the elements of the array in ascending order
using Bubble Sort technique.
#include<stdio.h>
void main()
{
int i,j,ar[10],temp;

printf("Enter array:-\n");
for(i=0;i<=9;i++)
{
printf("ar[%d]=",i);
scanf("%d",&ar[i]);

}
for(i=0;i<=9;i++)
{
for(j=0;j<=9-i;j++)
{

if(ar[j]>ar[j+1])
{
temp=ar[j];
ar[j]=ar[j+1];
ar[j+1]=temp;

}
}
}
printf("Sorted array:-");
for(i=0;i<=9;i++)

{
printf("\n%d",ar[i]);
}
}

Output:
Enter array:-
ar[0]=1 23 415 7788 899 2 6 6 6
ar[1]=ar[2]=ar[3]=ar[4]=ar[5]=ar[6]=ar[7]=ar[8]=ar[9]=33

Sorted array:-
1
2
6
6

6
23
33
415
899
7788
Multiply two matrices Program in C
#include<stdio.h>
void main()
{
int i,j,k,m1[3][3],m2[3][3],mul[3][3];
printf("Enter first matrix:-\n");
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
scanf("%d",&m1[i][j]);
}
}
printf("Enter second matrix:-\n");
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
scanf("%d",&m2[i][j]);
}
}
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
mul[i][j]=0;
for(k=0;k<=2;k++)
{
mul[i][j]=mul[i][j]+(m1[i][k]*m2[k][j]);
}
}
}
printf("Multiplication of metrices:-\n");
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
printf("%d ",mul[i][j]);
}
printf("\n");
}
}

Output
: Enter first matrix:-
1231
12 3 3 3
1222
Enter second matrix:-
12

12
12
2
3
Multiplication of metrices:-

10 12 13
20 32 23
11 14 12
WAP that finds the sum of diagonal elements of a mxn
matrix.

#include<stdio.h>
void main()

{
int i,j,k,matrix[3][3],sum=0;
printf("Enter matrix:-\n");
for(i=0;i<=2;i++)
{

for(j=0;j<=2;j++)
{
scanf("%d",&matrix[i][j]);
}
}

printf("Matrix is:-\n");
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{

printf("%d ",matrix[i][j]);
}
printf("\n");
}
for(i=0;i<=2;i++)

{
for(j=0;j<=2;j++)
{
if(i==j)

{
sum = sum + matrix[i][j];
}
}
}

printf("Sum of diagonal is = %d ",sum);

Output: Enter matrix:-

123
234
456
Matrix is:-
1 2 3
2 3 4
4 5 6
Sum of diagonal is = 10

You might also like