Important Program of C language in O level exam january 2019
Important Program of C language in O level exam january 2019
Write a function which
accepts an array of size n containing integer values and returns average of all
values. Call the function from main program.
#include<stdio.h>
#include<conio.h>
int avg(int[],int);
void main()
{
int ar[5],i;
clrscr();
for(i=0;i<5;i++)
{
printf("Enter %d number
>>
",i+1);
scanf("%d",&ar[i]);
}
printf("\n\tAverage
= %d",avg(ar,5));
getch();
}
int avg(int ar[],int s)
{
int i=0,sum=0;
for(i=0;i<s;i++)
sum+=ar[i];
return(sum/s);;
}
Write a function to display the multiplication table of the number.
#include<stdio.h>
void table(int);
void main()
{
int i;
printf("Enter
number>> ");
scanf("%d",&i);
table(i);
}
void table(int n)
{
int i;
for(i=1;i<=10;i++)
printf("\n%d*%d=%d",n,i,n*i);
}
Write a
program to determine the sum of the following series:
S = 1 – 3 + 5 – 7 + ...(n terms)
S = 1 – 3 + 5 – 7 + ...(n terms)
#include<stdio.h>
main()
{
int n, s=0,i,sign=1;
printf("\nEnter value of n=");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
s=s+sign*(2*i-1);
sign=sign*(-1);
}
printf("\nThe sum of the series=%d",s);
}
main()
{
int n, s=0,i,sign=1;
printf("\nEnter value of n=");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
s=s+sign*(2*i-1);
sign=sign*(-1);
}
printf("\nThe sum of the series=%d",s);
}
Write
a program to copy a file into another file.
#include <stdio.h>
main()
{
FILE *fp1, *fp2;
char ch;
fp1 = fopen("abc.txt", "r");
fp2 = fopen("xyz.txt", "w");
while((ch = getc(fp1)) != EOF)
putc(ch, fp2);
fclose(fp1);
fclose(fp2);
getch();
}
|
Write a program to display all the
prime numbers between two positive integers m and n. The values of m and n are
to be taken from the user.
Solution :
#include<stdio.h>
void main()
{
int n,m,p;
printf("Enter
range starting number>> ");
scanf("%d",&m);
printf("Enter
range ending number>> ");
scanf("%d",&n);
if(m>n)
printf("\n\tRange start should be less than range end");
else
{
while(m<=n)
{
p=2;
while(p<m)
{
if(m%p==0)
break;
p++;
}
if(p==m)
printf("\t%d",p);
m++;
}
}
}
If this article is helpful to you then please like share and comments.. thank you..
Comments
Post a Comment