Professional Documents
Culture Documents
BSC C Programs11
BSC C Programs11
#include<stdio.h>
void main()
{
int n,t,s=0,r;
clrscr();
printf("enter value of n");
scanf("%d",&n);
t=n;
do
{
r=n%10;
s=s+(r*r*r);
n=n/10;
}
while(n!=0);
if(t==s)
{
printf("given no is armstrong");
}
else
{
printf("given no is not armstrong");
}
getch();
}
output:
enter value of n
153
given no is armstrong
2. write a program to find the sum of individual digits of a positive integer.
#include<stdio.h>
void main()
{
int n,sum=0,rem;
clrscr();
printf("enter value of n");
scanf("%d",&n);
while(n!=0)
{
rem=n%10;
sum=sum+rem;
n=n/10;
}
printf("%d",sum);
getch();
}
output:
enter value of n
123
6
3.write a program to generate the first n terms of the fibonacci sequence.
#include<stdio.h>
void main()
{
int a=0,b=1,c=1,n,i;
clrscr();
printf("enter value of n");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("%d",a);
c=a+b;
a=b;
b=c;
}
getch();
}
output:
enter value of n
5
0 1123
4.write a program to find both the largest and smallest number in a list of integer values
#include <stdio.h>
void main()
{
int a[5], i, large, small, n;
clrscr();
printf("enter the size of array");
scanf("%d", &n);
printf("enter elements\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
large=a[0];
small=a[0];
for(i=1; i<n;i++)
{
if(a[i]>large)
{
large=a[i];
}
if(a[i]<small)
{
small=a[i];
}
}
printf("largest element is %d", large);
printf("smallest element is %d", small);
getch();
}
output:
enter the size of array
5
enter elements
8
5
1
4
3
largest element is 8
smallest element is 1
5.write a program to demonstrate passing of parameters using a) call by value b) call by
reference
a) call by value
#include<stdio.h>
void sum(int, int);
void main( )
{
int n1,n2;
clrscr();
printf("enter values of n1 n2 \n");
scanf("%d%d",&n1,&n2);
sum(n1,n2);
getch();
}
void sum(int x, int y)
{
int z;
z=x+y;
printf("sum is %d",z);
}
output:
enter values of n1 ,n2
5
6
sum is 11
6. write a program to perform various string operations.
#include<stdio.h>
void main()
{
char str1[10],str2[10];
clrscr(0;
printf("enter two strings");
gets(str1);
gets(str2);
printf("%d\n",strlen(str1));
printf("%s\n", strupr(str1));
printf("%s\n", strlwr(str2));
printf("%s\n", strrev(str1));
printf("%s\n", strcat(str1,str2));
printf("%s\n", strcpy(str1,str2));
if (strcmp(str1, str2) ==0)
{
printf("string 1 and string 2 are equal");
}
else
{
printf("string 1 and string 2 are not equal");
}
getch();
}
output:
enter two strings
apple
james
5
apple
james
elppa
elppajames
james
string 1 and string2 are equal
7. write a program to find factorial of given integer value using recursive function
#include<stdio.h>
void main()
{
int x,n;
clrscr();
printf("enter the number");
scanf("%d",&n);
x=fact(n);
printf("factorial is %d",x);
getch();
}
int fact(int n)
{
if(n==0)
return(1);
return(n*fact(n-1));
}
output:
enter the number
4
factorial is 24
8. write a program to sort a given list of integers in ascending order.
#include <stdio.h>
void main()
{
int i, j, a, n, number[30];
clrscr();
printf("enter the value of n");
scanf("%d", &n);
printf("enter the numbers");
for (i = 0; i < n; ++i)
scanf("%d", &number[i]);
for (i = 0; i < n; ++i)
{
for (j = i + 1; j < n; ++j)
{
if (number[i] > number[j])
{
a = number[i];
number[i] = number[j];
number[j] = a;
}
}
}
printf("the numbers arranged in ascending order \n");
for (i = 0; i < n; ++i)
printf("%d\n", number[i]);
getch();
}
output:
enter the value of n
5
1
7
3
5
4
the numbers arranged in ascending
1
3
4
5
7
9. write a program to illustrate pointer arithmetic.
#include<stdio.h>
void main()
{
int number=50;
int *p;
clrscr(0;
p=&number;//stores the address of number variable
printf("address of p variable is %u \n",p);
p=p+1;
printf("after increment: address of p variable is %u \n",p);
getch();
}
output:
address of p variable is 65524
after increment: address of p variable is 65526
10. write a program that uses two array variables to add two matrices.
#include<stdio.h>
void main()
{
int a[2][2],b[2][2],c[2][2],i,j;
clrscr();
printf(“enter values of a\n”);
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
scanf(“%d”,&a[i][j]);
}
printf(“enter values of b\n”);
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
scanf(“%d”,&b[i][j]);
}
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
c[i][j]=a[i][j]+b[i][j];
}
for(i=0;i<2;i++)
{
printf(“\n”);
for(j=0;j<2;j++)
{
printf(“matrix addition is%d\t”,c[i][j]);
}
}
getch();
}
output:
enter values of a
1
2
3
4
enter values of b
5
6
7
8
matrix addition is
68
10 12
11. write a program to calculate the salaries of all employees using employee (id, name,
designation, basic pay, da, hra, gross salary, deduction, net salary) structure.
a. da is 30 % of basic pay
b. hra is 15% of basic pay
c. deduction is 10% of (basic pay + da)
d. gross salary = basic pay + da+ hra
e. net salary = gross salary - deduction
#include<stdio.h>
struct employee
{
int id;
char name[10],desg[10];
float bpay,da,hra,gross, ded, nsal;
}
e;
void main()
{
clrscr();
printf("enter id, name, desg, bpay\n");
scanf("%d%s%s%f", &e.id, &e.name, &e.desg, &e.bpay);
e.da=e.bpay*0.3;
e.hra=e.bpay*0.15;
e.ded=(e.bpay+e.da)*0.1;
e.gross=e.bpay+e.da+e.hra;
e.nsal=e.gross-e.ded;
printf("id:%d\n",e.id );
printf("name:%s\n",e.name );
printf("desg:%s\n",e.desg );
printf("bpay:%f\n",e.bpay );
printf("da:%f\n",e.da );
printf("hra:%f\n",e.hra );
printf("ded:%f\n",e.ded );
printf("gross:%f\n",e.gross );
printf("nsal:%f\n",e.nsal );
getch();
}
output:
enter id, name, desg, bpay
2
rohit
manager
15000
id:2
name:rohit
desg:manager
bpay:15000
da:4500
hra:2250
ded:1950
gross:21750
nsal:19800
12. write a program to demonstrate four categories of functions.
a) without arguments, without returning values
b) with arguments, without return values
c) with arguments, with return values
d)without arguments, with return values
#include<stdio.h>
void ();
void main()
{
sum();
}
void sum()
{
print a,b,c;
printf(“enter values of a,b”);
scanf(“%d%d”,&a,&b);
c=a+b;
printf(“sum is: %d”, c);
}
output:
enter values of a,b
5
6
sum is: 11
output:
enter values of a,b
5
6
sum is: 11
void main()
{
int a,b;
clrscr();
printf("enter values of a,b");
scanf("%d%d",&a,&b);
printf("sum is: %d", sum(a,b));
getch();
}
sum(int x, int y)
{
int z;
z=x+y;
return z;
}
output:
enter values of a,b
5
6
sum is: 11
void main()
{
int d;
clrscr();
d=sum();
printf("sum is: %d", d);
getch();
}
sum()
{
int a,b,c;
printf("enter values of a,b");
scanf("%d%d",&a,&b);
c=a+b;
return c;
}
output:
enter values of a,b
5
6
sum is: 11
void autodemo();
void staticdemo();
void registerdemo();
void externdemo();
#include<stdio.h>
void main()
{
int i=1,a,b,c;//automatic and local variables in main()
extern int x;
clrscr();
a=5;c=3;
b=a*1+c*2;
autodemo();
while(i<=5)
{
staticdemo(); //calling staticdemo() 5 times from main()
i++;
}
registerdemo();
externdemo();
printf("\n value of b in main() is %d",b);
getch();
}
void autodemo()
{
auto int a,b,c;//local and automatic variables-gets created and destroyed automatically
a=2;b=3;c=4;
printf("\n ---- automatic storage class function -----");
c=a*5+b*4;
printf("\n value of c in autodemo() is %d",c);
}
void staticdemo()
{
static int i=1;//static variables gets initialized only once and preserves the values during
function calls
printf("\n i in static storage classs demo() is %d",i);
i++;
}
#include<stdio.h>
#define size 20
void main(){
char str[size],*stg,str2[size],*str3; int i;
file *fp,*fp1,*fp2,*fp3;
clrscr();
fp=fopen("fprintf.txt","w+");
fp=fopen("fprintf.txt","r+");
printf("enter string for fprintf\n");
gets(str);fprintf(fp,"%s",str);
fscanf(fp,"%s",str);
puts(str);
fp1=fopen("fputs.txt","w+");
printf("enter pointer string for fpusts\n");
gets(stg);
fputs(stg,fp1);
fp2=fopen("fptc.txt","w+");
puts("enter second string for fputc");
scanf("%s",str2);
for(i=0;i<20;i++)
fputc(str2[i],fp2);
fp3=fopen("fwrite.txt","w+");
puts("enter string to fwrite");
scanf("%s",str3);
fwrite(str3,1,10,fp3);
fread(str3,1,10,fp3);
for(i=0;i<20;i++)
printf("%c",str3[i]);
puts(str3);
getch();
}
output:
fprintf.txt, fputs.txt, fputc.txt, fwrite.txt files will be created in the parent folder