Beruflich Dokumente
Kultur Dokumente
#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;
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;
scanf("%d",&day);
year=day/365;
week=day/7;
printf("year=%d\nweek=%d",year,week);
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);
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;
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;
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;
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;
scanf("%d", &n);
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;
scanf("%d",&num);
while(num!=0)
m=num%10;
rev=rev*10+m;
num=num/10;
if(rev==num)
}
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;
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;
}
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;
scanf("%d",&n);
int i,c=0;
for(i=1;i<n;i++)
if(n%i==0)
c++;
if(c==1)
else
printf("Not prime\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)
}
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;
scanf("%f",&r);
d=dia(r);
cr=circumfrence(r);
ar=area(r);
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>
int main()
int x, y;
scanf("%d%d",&x,&y);
swap(x, y);
return 0;
int temp;
temp = b;
b = a;
a = temp;
}
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;
scanf("%d",&f);
l=sum(f);
sum(int n)
if(n==0)
return 0;
else
return(n%10+sum(n/10));
#include<stdio.h>
int sum(int);
void main()
int f,l;
scanf("%d",&f);
l=sum(f);
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>
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);
scanf("%d", &a[i]);
sum=sumofarray(a,n,0);
}
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()
scanf("%d", &Size);
scanf("%d", &a[i]);
if(a[i] % 2 == 0)
{
else
return 0;
}
Write a recursive C program to find maximum and minimum element in the given
array.
#include <stdio.h>
int main()
int i;
scanf("%d", &array[i]);
return 0;
int max;
return array[index];
else
return array[index];
else
return max;
int min;
if(index >= len-2)
return array[index];
else
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;
freq[i] = -1;
}
if(arr[i]==arr[j])
{
count++;
freq[j] = 0;
}
}
if(freq[i] != 0)
{
freq[i] = count;
}
}
return 0;
}
Write a C program to delete all duplicate elements from an array
#include <stdio.h>
int main()
scanf("%d", &n);
scanf("%d", &a[c]);
if(a[c] == b[d])
break;
if (d == count)
{
b[count] = a[c];
count++;
printf("%d\n", b[c]);
return 0;
}
WAP to find the multiplication of matrices A and B into matrix C
#include<stdio.h>
int i,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];
scanf("%d",&a[i][j]);
scanf("%d",&b[i][j]);
Matrix_Display(a,n);
Matrix_Display(b,n);
//Addition
c[i][j]=a[i][j]+b[i][j];
//Subtraction
c[i][j]=a[i][j]-b[i][j];
Matrix_Display(c,n);
//Multiplication
c[i][j]=0;
c[i][j]+=a[i][k]*b[k][j];
Matrix_Display(c,n);
}
#include <stdio.h>
int main()
// If colum of first matrix in not equal to row of second matrix, asking user to
enter the size of matrix again.
return 0;
int i, j;
scanf("%d", &firstMatrix[i][j]);
scanf("%d", &secondMatrix[i][j]);
int i, j, k;
mult[i][j] = 0;
{
for(j = 0; j < columnSecond; ++j)
int i, j;
printf("\nOutput Matrix:\n");
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;