C Program
C Program
C Program
#include<stdio.h>
int main()
{
int n,r,sum=0,temp;
printf("enter the number=");
scanf("%d",&n);
temp=n;
while(n>0)
{
r=n%10;
sum=sum+(r*r*r);
n=n/10;
}
if(temp==sum)
printf("armstrong number ");
else
printf("not armstrong number");
return 0;
}
Output:
Enter the number=153
armstrong number
5. Ascending and descending order of numbers using arrays.(Largest and smallest numbers)
#include <stdio.h>
#include<conio.h>
int main()
{
int a[100],n,i,j;
printf("Array size: ");
scanf("%d",&n);
printf("Elements: ");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
if (a[j] > a[i])
{
int tmp = a[i];
a[i] = a[j];
a[j] = tmp;
}
}
}
printf("\n\nAscending : ");
for (int i = 0; i < n; i++)
{
printf(" %d ", a[i]);
}
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
if (a[j] < a[i])
{
int tmp = a[i];
a[i] = a[j];
a[j] = tmp;
}
}
}
printf("\n\nDescending : ");
for (int i = 0; i < n; i++)
{
printf(" %d ", a[i]);
}
return 0;
getch();
}
Output:
Array size: 10
Elements : 3 4 7 6 5 1 2 8 10 9
Ascending : 1 2 3 4 5 6 7 8 9 10
Descending : 10 9 8 7 6 5 4 3 2 1
6. Sorting of names in alphabetical order.
#include<stdio.h>
#include<string.h>
main(){
int i,j,n;
char str[100][100],s[100];
printf("Enter number of names :\n");
scanf("%d",&n);
printf("Enter names in any order:\n");
for(i=0;i<n;i++){
scanf("%s",str[i]);
}
for(i=0;i<n;i++){
for(j=i+1;j<n;j++){
if(strcmp(str[i],str[j])>0){
strcpy(s,str[i]);
strcpy(str[i],str[j]);
strcpy(str[j],s);
}
}
}
printf("\nThe sorted order of names are:\n");
for(i=0;i<n;i++){
printf("%s\n",str[i]);
}
}
Output:
Enter number of names:
5
Enter names in any order:
Pinky
Lucky
Ram
Appu
Bob
The sorted order of names is:
Appu
Bob
Lucky
Pinky
Ram
Output:
1. Enter the number of elements 6
Enter the elements one by one
4
6
1
2
5
3
Enter the element to be searched 6
Element is present in the array at position 2
1 2 3 4 (matrix 1 elements)
2 3 4 5 (matrix 2 elements)
3 5 (resultant matrix)
79
ii) Subtraction
#include <stdio.h>
int main()
{
int m, n;
scanf(“%d %d”,&m,&n);
int i, j;
int mat1[m][n], mat2[m][n], mat3[m][n];
for(i = 0; i < m; i++)
{
for(j = 0; j < n; j++)
scanf(“%d”,&mat1[i][j]);
}
for(i = 0; i < n; i++)
{
for(j = 0; j < n; j++)
scanf(“%d”,&mat2[i][j]);
}
for(i = 0; i < m; i++)
{
for(j = 0; j < n; j++)
{
mat3[i][j] = mat1[i][j] – mat2[i][j];
}
}
for(i = 0; i < m; i++)
{
for(j = 0; j < n; j++)
printf(“%d “, mat3[i][j]);
printf(“\n”);
}
return 0;
}
Output:
2 2 (order of the matrix)
5 6 7 8 (matrix 1 elements)
1 2 3 4 (matrix 2 elements)
4 4 (resultant matrix)
44
iii)Multiplication
#include <stdio.h>
int main()
{
int m, n, p, q, c, d, k, sum = 0;
int mat1[10][10], mat2[10][10], mat3[10][10];
printf(“Enter number of rows and columns of mat1 matrix\n”);
scanf(“%d%d”, &m, &n);
printf(“Enter elements of matrix 1\n”);
for (c = 0; c < m; c++)
for (d = 0; d < n; d++)
scanf(“%d”, &mat1[c][d]);
printf(“\nEnter number of rows and columns of mat2 matrix\n”);
scanf(“%d%d”, &p, &q);
if (n != p)
printf(“\nThe matrices can’t be multiplied with each other.\n”);
else
{
printf(“\nEnter elements of matrix2\n”);
for (c = 0; c < p; c++)
for (d = 0; d < q; d++)
scanf(“%d”, &mat2[c][d]);
for (c = 0; c < m; c++) {
for (d = 0; d < q; d++) {
for (k = 0; k < p; k++) {
sum = sum + mat1[c][k]*mat2[k][d];
}
mat3[c][d] = sum;
sum = 0;
}
}
printf(“\nProduct of the matrices:\n”);
for (c = 0; c < m; c++) {
for (d = 0; d < q; d++)
printf(“%d\t”, mat3[c][d]);
printf(“\n”);
}
}
return 0;
}
Output:
Enter number of rows and columns of mat1 matrix
22
2345
22
1234
11 16
19 28
iv) Transpose
#include <stdio.h>
int main() {
int a[10][10], transpose[10][10], r, c;
printf("Enter rows and columns: ");
scanf("%d %d", &r, &c);
printf("\nEnter matrix elements:\n");
for (int i = 0; i < r; ++i)
for (int j = 0; j < c; ++j) {
printf("Enter element a%d%d: ", i + 1, j + 1);
scanf("%d", &a[i][j]);
}
printf("\nEntered matrix: \n");
for (int i = 0; i < r; ++i)
for (int j = 0; j < c; ++j) {
printf("%d ", a[i][j]);
if (j == c - 1)
printf("\n");
}
for (int i = 0; i < r; ++i)
for (int j = 0; j < c; ++j) {
transpose[j][i] = a[i][j];
}
printf("\nTranspose of the matrix:\n");
for (int i = 0; i < c; ++i)
for (int j = 0; j < r; ++j) {
printf("%d ", transpose[i][j]);
if (j == r - 1)
printf("\n");
}
return 0;
}
Output:
Entered matrix:
1 4 0
-5 2 7
#include<stdio.h>
int factorial(int n)
{
if(n!=0)
return n*factorial(n-1); // general case
else
return 1; // base case
}
int main()
{
int num, result;
printf("Enter a positive number: ");
scanf("%d",&num);
result= factorial(num); //function call
printf("Result = %d\n",result);
return 0;
}
Output:
Enter a positive number: 5
Result = 120
Enter a positive number: 7
Result = 5040
Output:
Enter the number of terms: 5
Fibbonacci series:0 1 1 2 3
Output:
MENU
---------------------------------------
1. Find length of string
2. Find reverse of string
3. Concatenate strings
4. Copy string
5. Compare strings
6. Exit
---------------------------------------
Enter your choice: 3
Enter first string: Tutorial
Enter second string: Ride
String after concatenation: TutorialRide
MENU
---------------------------------------
1. Find length of string
2. Find reverse of string
3. Concatenate strings
4. Copy string
5. Compare strings
6. Exit
-----------------------------------------
Enter your choice: 6
Output:
1.Find Length
2.Concatenate
3.Compare
4.Copy
5.Exit
Enter your choice:1
Enter the string:Welcome to Programming
The length of string is 7
Output:
Enter the number: 151
151 is Palindrome number
scanf("%[^~]",&str);//scanf formatting
for(int i=0;str[i]!='\0';i++)
{
if(str[i] == ' ')
{
words++;
}
else if(str[i] == '\n')
{
newline++;
words++;//since with every next line new words start. corner case 1
}
else if(str[i] != ' ' && str[i] != '\n'){
characters++;
}
}
if(characters > 0)//Corner case 2,3.
{
words++;
newline++;
}
printf("Total number of words : %d\n",words);
printf("Total number of lines : %d\n",newline);
printf("Total number of characters : %d\n",characters);
return 0;
}
Output:
Input : Deepak Kumar is the
brother of Aditya Kumar~
Output : Total number of words : 8
Total number of lines : 2
Total number of characters : 36
while(1)
{
printf("\nWhat would you like to do now ? : \n");
printf("\n1.Display \t2.Search \t3.Modify \t4.Delete \t5.Exit");
scanf("%d",&c);
switch(c)
{
case 1:
fp=fopen("one.txt","r+");
display(fp);
fclose(fp);
break;
case 2:
fp=fopen("one.txt","r+");
printf("\nEnter ID to search : ");
scanf("%d",&id);
if(search(fp,id))
{
printf("\nThe record is as follows : ");
printf("\n%d\t%s",d.id,d.name);
}
else
printf("\nRecord not found");
fclose(fp);
break;
case 3:
fp=fopen("one.txt","r+");
printf("\nEnter ID to modify d record : ");
scanf("%d",&id);
if(search(fp,id))
{
printf("\nEnter new name");
scanf("%s",d.name);
fwrite(&d,sizeof(d),1,fp);
}
else
printf("\nSpecified record not found ");
fclose(fp);
break;
}
}
}