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

Bsc c Programs Honours II Sem

The document contains multiple C programming examples demonstrating various algorithms and concepts, including calculating the sum of digits, checking for Armstrong numbers, generating Fibonacci sequences, finding the largest and smallest numbers in an array, and performing matrix addition. It also covers function usage, such as call by value and call by reference, string operations, factorial calculation using recursion, and employee salary calculations using structures. Each example includes code snippets and sample outputs for clarity.

Uploaded by

p58338671
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

Bsc c Programs Honours II Sem

The document contains multiple C programming examples demonstrating various algorithms and concepts, including calculating the sum of digits, checking for Armstrong numbers, generating Fibonacci sequences, finding the largest and smallest numbers in an array, and performing matrix addition. It also covers function usage, such as call by value and call by reference, string operations, factorial calculation using recursion, and employee salary calculations using structures. Each example includes code snippets and sample outputs for clarity.

Uploaded by

p58338671
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

1. 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
2.write a program to check whether the given number is armstrong or not.

#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
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

b) call by reference

#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 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
9. 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
10. 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

a) without arguments, without returning 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

b) with arguments, without returning values

void sum(int, int);


void main()
{
int a,b;
clrscr();
printf("enter values of a,b");
scanf("%d%d",&a,&b);
sum(a,b);
getch();
}
void sum(int x, int y)
{
int z;
z=x+y;
printf("sum is: %d", z);
}

output:
enter values of a,b
5
6
sum is: 11

c) with arguments, with returning values

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

d) without arguments, with returning values

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

You might also like