Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 23

Institute of Management Studies

Thapathali Marg, Kathmandu

Assignment – 2
On
C Programming

Submitted By:
Alina Niraula
BIM 2nd Sem
Roll No – 1

Submitted To:
Deepika Bajracharya
 Program to find the sum of two numbers without using arithmetic operators.

#include<stdio.h>
#include<conio.h>
int main()
{
int a,b;
printf("Enter two numbers\n");
scanf("%d %d",&a,&b);
while(a!=0){
b++;
a--;
}
printf("The sum of two numbers without using arithmetic operators is %d",b);
return 0;
}
 Program to reverse the digits of the entered number.

#include<stdio.h>
#include<conio.h>
int main()
{
int a,rev=0,n;
printf("Enter the number more than one digits\n");
scanf("%d",&a);
while(a!=0){
n=a%10;
rev=rev*10+n;
a=a/10;
}
printf("The reverse of the number is %d",rev);
return 0;
}


 Program to find the number of digits in the entered number.

#include<stdio.h>
#include<conio.h>
int main()
{
int a, n, c=0;
printf("Enter the number\n");
scanf("%d",&a);
while(a!=0){
n=a%10;
a=a/10;
c++;
}
printf("The total digits in the number is %d",c);
return 0;
}
 Program to calculate the sum of all the numbers in array and find the
average of them.

#include<stdio.h>
#include<conio.h>
int main()
{
int a[20],sum=0,n,i,ave;
printf("Enter the number of an array\n");
scanf("%d",&n);
printf("Enter the numbers\n");
for(i=0;i<n;i++){
scanf("%d",&a[i]);
sum=sum+a[i];
}
ave=sum/n;
printf("The sum of all the numbers in array is %d and the average of them is
%d",sum,ave);
return 0;
}
 Program to find the smallest element in the given array.

#include<stdio.h>
#include<conio.h>
int main()
{
int a[20],j,n,i,ave;
printf("Enter the number of an array\n");
scanf("%d",&n);
printf("Enter the numbers\n");
for(i=0;i<n;i++){
scanf("%d",&a[i]);
}
for(i=1;i<n;i++){
for(j=0;j<i;j++){
if(a[j]>a[i]){
ave=a[i];
a[i]=a[j];
a[j]=ave;
}
}
}
printf("The smallest element of the array is %d",a[0]);
return 0;
}
 Program to find the sum of two matrix of any size using array.

#include<stdio.h>
#include<conio.h>
int main()
{
int a[20][20],b[20][20],j,r,c,i,sum[20][20];
printf("Enter the row of two matrixs\n");
scanf("%d",&r);
printf("Enter the column of two mtrixs\n");
scanf("%d",&c);
printf("Enter the elements of first matrix\n");
for(i=0;i<r;i++){
for(j=0;j<c;j++){
scanf("%d",&a[i][j]);
}
}
printf("Enter the elements of second matrix\n");
for(i=0;i<r;i++){
for(j=0;j<c;j++){
scanf("%d",&b[i][j]);
}
}
printf("The entered matrix are \n");
for(i=0;i<r;i++){
for(j=0;j<c;j++){
printf("%d\t",a[i][j]);
}
printf("\n");
}
printf("\n");
for(i=0;i<r;i++){
for(j=0;j<c;j++){
printf("%d\t",b[i][j]);
}
printf("\n");
}
printf("\n The sum of the matrixs are\n") ;
for(i=0;i<c;i++){
for(j=0;j<r;j++){
sum[i][j]=a[i][j]+b[i][j];
}
}
for(i=0;i<r;i++){
for(j=0;j<c;j++){
printf("%d\t",sum[i][j]);
}
printf("\n");
}
return 0;
}
 Program to find matrix multiplication with array.

#include <stdio.h>
int main()
{
int m, n, p, q, i, j, k, sum = 0;
int first[10][10], second[10][10], multiply[10][10];
printf("Enter number of rows and columns of first matrix\n");
scanf("%d%d", &m, &n);
printf("Enter elements of first matrix\n");
for (i=0;i<m;i++){
for (j=0;j<n;j++){
scanf("%d", &first[i][j]);
}
}
printf("Enter number of rows and columns of second matrix\n");
scanf("%d%d", &p, &q);
printf("Enter elements of second matrix\n");
for (i = 0; i < p; i++){
for (j = 0; j < q; j++){
scanf("%d", &second[i][j]);
}
}
for (i = 0; i < m; i++) {
for (j = 0; j < q; j++) {
for (k = 0; k < p; k++) {
sum = sum + first[i][k]*second[k][j];
}
multiply[i][j] = sum;
sum = 0;
}
}
printf("Product of the matrices:\n");
for (i = 0; i < m; i++) {
for (j = 0; j < q; j++){
printf("%d\t", multiply[i][j]);
}
printf("\n");
}
return 0;
}
 A program to display only the diagonal elements of the matrix.

#include<stdio.h>
#include<conio.h>
int main()
{
int a[20][20],b[20][20],j,m,n,i,k,c[20][20];
printf("Enter the row of the matrix");
scanf("%d",&m);
printf("Enter the column of the mtrixs");
scanf("%d",&n);
printf("Enter the elements of the matrix\n");
for(i=0;i<m;i++){
for(j=0;j<n;j++){
scanf("%d",&a[i][j]);
}
}
printf("The entered matrixs is\n");
for(i=0;i<m;i++){
for(j=0;j<n;j++){
printf("%d\t",a[i][j]);
}
printf("\n");
}
printf("\n The diagonal elemeents of the matrixs is\n");
for(i=0;i<m;i++){
for(j=0;j<n;j++){
if(i=j){
printf("%d",a[i][j]);
}
else{
printf("\t");
}
}
printf("\n");
}
return 0;
}
 Program to generates following formats.
1
11 21
31 41 51
61 71 81 91

#include<stdio.h>
#include<conio.h>
int main()
{
int i, j,a;
for(i=1;i<=5;i++){
for(j=1;j<=i;j++){
printf("%d\t",a);
a=a+10;
}
printf("\n");
}
return 0;
}

 2
24

246

2468

2 4 6 8 10

#include<stdio.h>
#include<conio.h>
int main()
{
int i, j,a;
for(i=1;i<=5;i++){
a=2;
for(j=1;j<=i;j++){
printf("%d\t",a);
a=a+2;
}
printf("\n");
}
return 0;
}

 1

23
456

789

10 11 12 13 14

#include<stdio.h>
#include<conio.h>
int main()
{
int i, j,a=1;
for(i=1;i<=5;i++){
for(j=1;j<=i;j++){
printf("%d\t",a);
a++;
}
printf("\n");
}
return 0;
}

 *

**
***

****

*****

#include<stdio.h>
#include<conio.h>
int main()
{
int I, j;
for(i=5;i>=1;i--){
for(j=1;j<=5;j++){
if(i<=j){
printf(“*”);
}
else{
printf(“ “);
}
}
printf(“\n”);
}
return 0;
}

 @@@@@

@@@@
@@@

@@

#include<stdio.h>
#include<conio.h>
int main()
{
int i, j;
for(i=5;i>=1;i--){
for(j=5;j>=1;j--){
if(j<=i){
printf("@");
}
else{
printf(" ");
}
}
printf("\n");
}
return 0;
}

 1

0 1
1 0 1

0 1 0 1

1 0 1 0 1

0 1 0 1 0 1

1 0 1 0 1 0 1

#include<stdio.h>
#include<conio.h>
int main()
{
int i, j;
for(i=1;i<=7;i++){
for(j=1;j<=i;j++){
if((i+j)%2==0){
printf("1");
}
else{
printf(" 0 ");
}
}
printf("\n");
}
return 0;
}

 1 2 3 4 5 6 7 8 9 10

2 4 6 8 10 12 14 16 18 20
3 6 9 12 15 18 21 24 27 30

4 8 12 16 20 24 28 32 36 40

5 10 15 20 25 30 35 40 45 50

#include<stdio.h>
#include<conio.h>
int main()
{
int i, j;
for(i=1;i<=5;i++){
for(j=1;j<=10;j++){
printf("%d\t",i*j);
}
printf("\n");
}
return 0;
}

 2

3 5
7 11 13

17 19 23 29

31 37 41 43 47

#include<stdio.h>
#include<conio.h>
int main()
{
char i, j, a=2,z,b,c;
for(i=1;i<=5;i++){
z=1;
for(j=1;j<=i;j++){
while(z<=i){
c=0;
for(b=2;b<a;b++){
if(a%b==0){
c++;
}
}
if(c==0){
printf("%d",a);
z++;
}
a++;
}
}
printf("\n");
}
return 0;
}

 Find factorial of the number using function.


#include<stdio.h>
int fact(int a);
int main()
{
int x;
printf("The enter number");
scanf("%d",&x);
printf("The factorial of the number is %d",fact(x));
return 0;
}
int fact(int a){
int z=1;
while(a!=0){
z=z*a;
a--;
}
return z;
}

You might also like