Ticker

6/recent/ticker-posts

Header Ads Widget

Contact Us For Advertisement: 9858091920

C Programming Part 2

C Programming:

1) Fibonacci
2) To convert Lower case letters into Upper case letters
3) Arranging in ascending order
4) Transpose of Matrix




/* Fibonacci series */ 
/* Program Coded By Chetan Thapa Magar */ 

#include<stdio.h>
#include<conio.h>
void main()
{
int n, i;
printf("Enter the terms to generate:\t");
scanf("%d", &n);
for(i=1; i<=n; i++)
printf("\n%d", fib(i));
getch();
}
int fib(int x)
{
if(x==1)
return 0;
else
if(x==2)
return 1;
else
return (fib(x-1)+fib(x-2));
}

Output / Execution:
Enter the terms to generate: 7
0
1
1
2
3
5
8

/* To convert Lower case letters into Upper case letters */ 
/* Program Coded By Chetan Thapa Magar. */ 

#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<ctype.h>
void main()
{
char a[50], b[50];
int i;
clrscr();
puts("Enter the string:\t");
gets(a);
for(i=0; a[i]!='\0'; i++)
b[i]=toupper(a[i]);
b[i]='\0';
printf("\nThe upper case string is %s\t", b);
getch();
}

Output / Execution:
Enter the string: chetan thapa magar
The upper case string is: CHETAN THAPA MAGAR


/* Insertion sort */ 
/* Program Coded By Chetan Thapa Magar */ 

#include<stdio.h>
#include<conio.h>
void main()
{
int a[20], n, temp, i, j;
clrscr();
printf("Enter the number of terms:\n");
scanf("%d", &n);
printf("\nEnter the elements for the array:\n");
for(i=0; i<n; i++)
{
scanf("%d", &a[i]);
}
for(i=1; i<n; i++)
{
temp=a[i];
j=i-1;
while(temp<a[j] && j>=0)
{
a[j+1]=a[j];
j=j-1;
}
a[j+1]=temp;
}
printf("\nThe ascending order for the given array is:\n\t");
for(i=0; i<n; i++)
printf("\n%d", a[i]);
getch();
}

Output / Execution:
Enter the number of terms:
5

Enter the elements for the array:
2617
2605
2609
2632
2610

The ascending order for the given array is:

2605
2609
2610
2617
2632


/* Transpose of Matrix */ 
/* Program Coded By Chetan Thapa Magar */ 

#include<stdio.h>
#include<conio.h>
void main()
{
int a[9][9], i, j, d=1;
printf("Enter the matrix values:\n");
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
scanf("%d", &a[i][j]);
}
printf("\nThe transpose of the given matrix is:\n");
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
printf("\t\t%d", a[j][i]);
printf("\n");
}
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
if(i==j)
d=d*a[i][j];
}
}
printf("\nThe diagonal value is %d", d);
getch();
}

Output / Execution:

Enter the matrix values:
1
2
3
4
5
6
7
8
9

The transpose of the given matrix is:

1 4 7
2 5 8
3 6 9


The diagonal value is 45 

Post a comment

0 Comments