Sie sind auf Seite 1von 56

Write a C program to input marks of five subjects of a student and calculate total,

average and percentage of all subjects


Solution:

#include<stdio.h>

void main()

int n1,n2,n3,n4,n5,tot,avg;

printf("enter marks=");

scanf("%d%d%d%d%d",&n1,&n2,&n3,&n4,&n5);

tot=n1+n2+n3+n4+n5;

avg=tot/5;

printf("total=%d\naverage=%d\npercentage in n1=%d\npercentage in
n2=%d\npercentage in n3=%d\npercentage in n4=%d\npercentage in
n5=%d",tot,avg,n1,n2,n3,n4,n5);

}
C Program to Read Two Integers M and N & Swap their Values with and without
using a temporary variable

Solution

#include<stdio.h>

void main()

int m,n;

printf("enter values for m and n respectively=");

scanf("%d%d",&m,&n);

printf("m=%d\nn=%d",m,n);

m=m+n;

n=m-n;

m=m-n;

printf("\nm=%d\nn=%d",m,n);

}.
Write a C program to input radius of a circle from user and find diameter,
circumference and area of the circle
Solution

#include<stdio.h>

void main()

int r,diam;

float cir,area;

printf("enter radius=");

scanf("%d",&r);

diam=2*r;

cir=2*3.14*r;

area=3.14*r*r;

printf("diameter=%d\ncircumference=%f\narea=%f\n",diam,cir,area);
}

In a class, marks of 5 students are given as follows 80, 85, 70, 75, 60. WAP to
Calculate the average and display it.

Solution

#include<stdio.h>

void main()

float avg;

avg=(80+85+70+75+60)/5;

printf("avg=%f",avg);

}
Write a C program to input number of days from user and convert it to years,
weeks and days. How to convert days to years, weeks in C programming

Solution

#include<stdio.h>

void main()

int day,year,week;

printf("enter no. of days=");

scanf("%d",&day);

year=day/365;
week=day/7;

printf("year=%d\nweek=%d",year,week);

Write a C program to check whether a number is ODD or EVEN

Solution

#include<stdio.h>

void main()

int n;

printf("enter number=")

scanf("%d",&n);
if(n%2==0)

printf("even");}

else

printf("odd");}

Write a C program to find maximum and minimum number among three numbers
with and without using ternary operators.

#include<stdio.h>

void main()

{
int a,b,c;

printf("enter numbers=");

scanf("%d%d%d",&a,&b,&c);

(a>b&a>c)?printf("%d is greatest",a):(b>c)?printf("%d is greatest",b):printf("%d is


greatest",c);

(a<b&a<c)?printf("%d is smallest",a):(b<c)?printf("%d is smallest",b):printf("%d is


smallest",c);

3. Write a program to print the month name corresponding to the digit enter from
the user using switch statement. For example if user enter 1 then print “January” on
2 print “February”…..
Solution

#include<stdio.h>

void main()

int n;

printf("enter any no.=");

scanf("%d",&n);

switch(n)

case 1:printf("january");break;

case 2:printf("febraury");break;

case 3:printf("march");break;

case 4:printf("april");break;

case 5:printf("may");break;

case 6:printf("june");break;

case 7:printf("july");break;

case 8:printf("august");break;

case 9:printf("september");break;

case 10:printf("october");break;

case 11:printf("november");break;

case 12:printf("december");break;

default:printf("invalid");

}
4. Write a C program to input three sides of a triangle and check whether the
triangle is valid or not.

#include<stdio.h>

void main()

int a,b,c;

printf("enter three sides of the triangle");

scanf("%d%d%d",&a,&b,&c);

if(a+b>c&&b+c>a&&a+c>b)

printf("valid");

else

printf("invalid");

}
5. Write a C program to input marks of five subjects and calculate the
percentage. Also
Calculate Grade according to the following:
Percentage>= 90% A
Percentage>=80% B
Percentage>=70% C
Percentage>=60% D
Percentage>=40% E
Percentage<40% Fail

. #include<stdio.h>

void main()

int a,b,c,d,e;

float per;

printf("enter marks of five subjects");

scanf("%d%d%d%d%d",&a,&b,&c,&d,&e);

per=(a+b+c+d+e)/5;

if(per>=90)

printf("A");

if(per>=80&&per<=90)

printf("B");

if(per>=70&&per<=80)

printf("c");

if(per>=60&&per<=70)

printf("D");

if(per>=40&&per<=60)

printf("E");
if(per<=40)

printf("Fail");

}
WAP to find sum of even numbers which are divisible by 2,3 and 5 upto ‘n’
numbers where n is entered from user

#include<stdio.h>
main()
{
int n;
printf("Enter the number\n");
scanf("%d",&n);
int sum=0;
int i;
for(i=1;i<=n;i++)
{
if(i%2==0)
{
if(i%3==0 && i%5==0)
sum+=i;
}
}
printf("The sum of numbers which are divisible by 2,3 and 5 = %d",sum);
}
WAP to print Fibonacci series upto ‘n’ terms where n is entered from user.

#include <stdio.h>

int main()

int i, n, t1 = 0, t2 = 1, nextTerm;

printf("Enter the number of terms: ");

scanf("%d", &n);

printf("Fibonacci Series: ");

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

printf("%d, ", t1);

nextTerm = t1 + t2;

t1 = t2;

t2 = nextTerm;

return 0;

}
WAP to check whether the entered integer number is palindrome or not
#include<stdio.h>

int main()

int rev=0,num,m=0;

printf("enter the required number");

scanf("%d",&num);

while(num!=0)

m=num%10;

rev=rev*10+m;

num=num/10;

printf("reverse of number is =%d",rev);

if(rev==num)

printf("\n the number is a palindrome");

else printf("\nnumber is not a palindorme");

}
WAP to find the binary equivalent of an integer number entered from user also
count the number of 1’s and number of 0’s in the resultant binary number.
#include<stdio.h>

main()

int n;

printf("Enter the number\n");

scanf("%d",&n);int n1=n;

int b=0,r,b1=0,c=0,d=0,e=0;

while(n!=0)

r=n%2;

b=b*10+r;

if(b==0)

c++;

n=n/2;

while(b>0)

r=b%10;

if(r==1)

d++;

if(r==0)
e++;

b1=b1*10+r;

b=b/10;

int j;

for(j=1;j<=c;j++)

b1=b1*10;

printf("The binary equivalent of %d is %d\n",n1,b1);

printf("the number of 0s and 1s in binary is equal to %d and %d respectively",(c+e),d);

}
Write a program whether the enter number is prime or not. Also print all prime
numbers between 1 to n where n is entered from user.

#include<stdio.h>

main()

int n,n1;

printf("Enter the number which has to be checked for prime\n");

scanf("%d",&n);

int i,c=0;

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

if(n%i==0)

c++;

if(c==1)

printf("%d is a prime number\n",n);

else

printf("Not prime\n");

printf("Enter the number upto which prime numbers are to be checked\n");

scanf("%d",&n1);

int j,k;

for(j=2;j<=n1;j++)

int i,c=0;
for(i=1;i<=j;i++)

if(j%i==0)

c++;

if(c==2)

printf("%d is a prime number\n",j);

}
Write a C program to input radius of circle from user and find diameter,
circumference and area of the given circle using function

#include<stdio.h>

float dia(float);

float circumfrence(float);

float area(float);

int main()

float r,d,cr,ar;

printf("Enter the required radius");

scanf("%f",&r);

d=dia(r);

cr=circumfrence(r);

ar=area(r);

printf("The required diameter is=%f\nCircumfrence is=%f\narea=%f",d,cr,ar);

return 0;

float dia(float r)

float h;

h = 2*r;

return(h);

}
float circumfrence(float r)

float c;

c = 44*r/7;

return(c);

float area(float r)

float a;

a = 22*r*r/7;

return(a);

}
Write a C program to swap two numbers using pointers and functions. How to
swap two numbers using call by value and call by reference method.

#include<stdio.h>

void swap(int, int);

int main()

int x, y;

printf("Enter the value of x and y\n");

scanf("%d%d",&x,&y);

printf("Before Swapping\nx = %d\ny = %d\n", x, y);

swap(x, y);

printf("After Swapping\nx = %d\ny = %d\n", x, y);

return 0;

void swap(int a, int b)

int temp;
temp = b;

b = a;

a = temp;

printf("Values of a and b is %d %d\n",a,b);

}
Write a C program to swap two numbers using pointers and functions. How to
swap two numbers using call by value and call by reference method.

#include<stdio.h>
int swap(int*,int*);
void main()
{
int x,y;
printf("enter any two numbers x, y respectively");
scanf("%d%d",&x,&y);
swap(&x,&y);
printf("The value of x=%d and y=%d",x,y);

}
int swap(int*x,int*y)
{
int t;
t=*x;
*x=*y;
*y=t;
}
A 5-digit positive integer is entered through the keyboard, write a function to
calculate sum of digits of the 5-digit number: (1) Without using recursion (2)
Using recursion
#include<stdio.h>

int sum(int);

void main()

int f,l;

printf("\nInput a 5 digit number");

scanf("%d",&f);

l=sum(f);

printf("\nThe sum of the digits is %d",l);

sum(int n)

if(n==0)
return 0;

else

return(n%10+sum(n/10));

A 5-digit positive integer is entered through the keyboard, write a function to


calculate sum of digits of the 5-digit number: (1) Without using recursion (2)
Using recursion

#include<stdio.h>

int sum(int);

void main()

int f,l;

printf("\nInput a 5 digit number");

scanf("%d",&f);

l=sum(f);

printf("\nThe sum of the digits is %d",l);


}

sum(int n)

int s=0,y=0;

while(n!=0)

y=n%10;

s=s+y;

n=n/10;

return (s);

}
A positive integer is entered through the keyboard, write a program to obtain the
prime factors of the number. Modify the function suitably to obtain the prime
factors recursively

#include<stdio.h>

prime(int);

void main()

int x;

printf("\nInput an integer\n");

scanf("%d",&x);

prime(x);

prime(int x)

int a;

for(a=2;a<=x;a++)

if(x%a==0)

printf("%d ",a);

prime(x/a);

break;

}
}

#include<stdio.h>

prime(int);

void main()

int x;

printf("\nInput an integer\n");

scanf("%d",&x);

prime(x);

prime(int x)

int a;

for(a=2;a<=x;a++)

if(x%a==0)
{

printf("%d ",a);

x/=a;

a--;

}
Write a C program to find sum of elements of an array where you need to
initialize, print and perform sum of elements in two ways (1) without using
function (2) with using function

#include<stdio.h>

prime(int);

void main()

int x;

printf("\nInput an integer\n");

scanf("%d",&x);

prime(x);

prime(int x)

int a;

for(a=2;a<=x;a++)

if(x%a==0)

printf("%d ",a);

x/=a;

a--;
}

#include <stdio.h>

int sumofarray(int a[],int n,int i)

if(i<n)

return a[i]+sumofarray(a,n,++i);

else

return 0;

int main()

int a[1000],i,n,sum;
printf("Enter size of the array : ");

scanf("%d", &n);

printf("Enter elements in array : ");

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

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

sum=sumofarray(a,n,0);

printf("sum of array is :%d",sum);

}
Write a C program to input elements in array from user and write two functions
to count even and odd elements of the array.

#include<stdio.h>

int main()

int Size, i, a[10];

int Even_Sum = 0, Odd_Sum = 0;

printf("\n Please Enter the Size of an Array : ");

scanf("%d", &Size);

printf("\nPlease Enter the Array Elements\n");

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

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

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

if(a[i] % 2 == 0)
{

Even_Sum = Even_Sum + a[i];

else

Odd_Sum = Odd_Sum + a[i];

printf("\n The Sum of Even Numbers in this Array = %d ", Even_Sum);

printf("\n The Sum of Odd Numbers in this Array = %d ", Odd_Sum);

return 0;

}
Write a recursive C program to find maximum and minimum element in the given
array.
#include <stdio.h>

#define MAX_SIZE 100

int maximum(int array[], int index, int len);

int minimum(int array[], int index, int len);

int main()

int array[MAX_SIZE], N, max, min;

int i;

printf("Enter size of the array: ");


scanf("%d", &N);

printf("Enter %d elements in array: ", N);

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

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

max = maximum(array, 0, N);

min = minimum(array, 0, N);

printf("Minimum element in array = %d\n", min);

printf("Maximum element in array = %d\n", max);

return 0;

int maximum(int array[], int index, int len)

int max;

if(index >= len-2)


{

if(array[index] > array[index + 1])

return array[index];

else

return array[index + 1];

max = maximum(array, index + 1, len);

if(array[index] > max)

return array[index];

else

return max;

int minimum(int array[], int index, int len)

int min;
if(index >= len-2)

if(array[index] < array[index + 1])

return array[index];

else

return array[index + 1];

min = minimum(array, index + 1, len);

if(array[index] < min)

return array[index];

else

return min;

}
Write a C program to input elements in array and write a function to find
frequency of each element in array.
#include <stdio.h>

int main()
{
int arr[100], freq[100];
int size, i, j, count;

printf("Enter size of array: ");


scanf("%d", &size);

printf("Enter elements in array: ");


for(i=0; i<size; i++)
{
scanf("%d", &arr[i]);

freq[i] = -1;
}

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


{
count = 1;
for(j=i+1; j<size; j++)
{

if(arr[i]==arr[j])
{
count++;

freq[j] = 0;
}
}

if(freq[i] != 0)
{
freq[i] = count;
}
}

printf("\nFrequency of all elements of array : \n");


for(i=0; i<size; i++)
{
if(freq[i] != 0)
{
printf("%d occurs %d times\n", arr[i], freq[i]);
}
}

return 0;
}
Write a C program to delete all duplicate elements from an array

#include <stdio.h>

int main()

int n, a[100], b[100], count = 0, c, d;

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

scanf("%d", &n);

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

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

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

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

for (d = 0; d < count; d++)

if(a[c] == b[d])

break;

if (d == count)
{

b[count] = a[c];

count++;

printf("Array obtained after removing duplicate elements:\n");

for (c = 0; c < count; c++)

printf("%d\n", b[c]);

return 0;

}
WAP to find the multiplication of matrices A and B into matrix C
#include<stdio.h>

void Matrix_Display(int a[][20],int n)

int i,j;

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

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

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

printf("\n");

int main()

int n,i,j,k;

int a[20][20];

int b[20][20];

int c[20][20];

printf("\n Enter the dimensions of the 2 Square matrices: ");


scanf("%d",&n);

printf("\n Enter elements of Matrix A: ");

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

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

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

printf("\n Enter elements of Matrix B: ");

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

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

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

printf("\n Matrix A: \n");

Matrix_Display(a,n);

printf("\n\n Matrix B: \n");

Matrix_Display(b,n);

//Addition

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

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

c[i][j]=a[i][j]+b[i][j];

printf("\n\n Addition of A and B gives: \n");


Matrix_Display(c,n);

//Subtraction

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

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

c[i][j]=a[i][j]-b[i][j];

printf("\n\n Subtraction of A and B gives: \n");

Matrix_Display(c,n);

//Multiplication

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

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

c[i][j]=0;

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

c[i][j]+=a[i][k]*b[k][j];

printf("\n\n Multiplication of A and B gives: \n");

Matrix_Display(c,n);

}
#include <stdio.h>

void enterData(int firstMatrix[][10], int secondMatrix[][10], int rowFirst, int


columnFirst, int rowSecond, int columnSecond);

void multiplyMatrices(int firstMatrix[][10], int secondMatrix[][10], int


multResult[][10], int rowFirst, int columnFirst, int rowSecond, int columnSecond);

void display(int mult[][10], int rowFirst, int columnSecond);

int main()

int firstMatrix[10][10], secondMatrix[10][10], mult[10][10], rowFirst,


columnFirst, rowSecond, columnSecond, i, j, k;

printf("Enter rows and column for first matrix: ");

scanf("%d %d", &rowFirst, &columnFirst);

printf("Enter rows and column for second matrix: ");

scanf("%d %d", &rowSecond, &columnSecond);

// If colum of first matrix in not equal to row of second matrix, asking user to
enter the size of matrix again.

while (columnFirst != rowSecond)

printf("Error! column of first matrix not equal to row of second.\n");

printf("Enter rows and column for first matrix: ");

scanf("%d%d", &rowFirst, &columnFirst);

printf("Enter rows and column for second matrix: ");

scanf("%d%d", &rowSecond, &columnSecond);


}

// Function to take matrices data

enterData(firstMatrix, secondMatrix, rowFirst, columnFirst, rowSecond,


columnSecond);

// Function to multiply two matrices.

multiplyMatrices(firstMatrix, secondMatrix, mult, rowFirst, columnFirst,


rowSecond, columnSecond);

// Function to display resultant matrix after multiplication.

display(mult, rowFirst, columnSecond);

return 0;

void enterData(int firstMatrix[][10], int secondMatrix[][10], int rowFirst, int


columnFirst, int rowSecond, int columnSecond)

int i, j;

printf("\nEnter elements of matrix 1:\n");

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

for(j = 0; j < columnFirst; ++j)

printf("Enter elements a%d%d: ", i + 1, j + 1);

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

printf("\nEnter elements of matrix 2:\n");


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

for(j = 0; j < columnSecond; ++j)

printf("Enter elements b%d%d: ", i + 1, j + 1);

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

void multiplyMatrices(int firstMatrix[][10], int secondMatrix[][10], int mult[][10], int


rowFirst, int columnFirst, int rowSecond, int columnSecond)

int i, j, k;

// Initializing elements of matrix mult to 0.

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

for(j = 0; j < columnSecond; ++j)

mult[i][j] = 0;

// Multiplying matrix firstMatrix and secondMatrix and storing in array mult.

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

{
for(j = 0; j < columnSecond; ++j)

for(k=0; k<columnFirst; ++k)

mult[i][j] += firstMatrix[i][k] * secondMatrix[k][j];

void display(int mult[][10], int rowFirst, int columnSecond)

int i, j;

printf("\nOutput Matrix:\n");

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

for(j = 0; j < columnSecond; ++j)

printf("%d ", mult[i][j]);

if(j == columnSecond - 1)

printf("\n\n");

}
WAP to interchange diagonals of a matrix
#include <stdio.h>

void main ()
{
static int array[10][10];
int i, j, m, n, a;

printf("Enter the order of the matix \n");


scanf("%d %d", &m, &n);
if (m == n)
{
printf("Enter the co-efficients of the matrix\n");
for (i = 0; i < m; ++i)
{
for (j = 0; j < n; ++j)
{
scanf("%dx%d", &array[i][j]);
}
}
printf("The given matrix is \n");
for (i = 0; i < m; ++i)
{
for (j = 0; j < n; ++j)
{
printf(" %d", array[i][j]);
}
printf("\n");
}
for (i = 0; i < m; ++i)
{
a = array[i][i];
array[i][i] = array[i][m - i - 1];
array[i][m - i - 1] = a;
}
printf("The matrix after changing the \n");
printf("main diagonal & secondary diagonal\n");
for (i = 0; i < m; ++i)
{
for (j = 0; j < n; ++j)
{
printf(" %d", array[i][j]);
}
printf("\n");
}
}
else
printf("The given order is not square matrix\n");
}

Das könnte Ihnen auch gefallen