C Programs
C Programs
C Programs
#include<stdio.h>
int main()
{
int num,sum=0,r;
printf("Enter a number: ");
scanf("%d",&num);
while(num)
{
r=num%10;
num=num/10;
sum=sum+r;
}
printf("Sum of digits of number: %d",sum);
return 0;
}
Sample output:
Enter a number: 1234
Sum of digits of number: 10
Q6 (i).
#include<stdio.h>
void main()
{
int a[10][10], b[10][10], m, n, i, j;
printf("\nEnter number of rows & columns of array : ");
scanf("%d %d", &m, &n);
printf("\nEnter elements of 2-D array:\n");
for(i=0; i<m; i++)
{
for(j=0; j<n; j++)
{
scanf("%d", &a[i][j]);
}
}
printf("\n\n2-D array before transposing:\n\n");
for(i=0; i<m; i++)
{
for(j=0; j<n; j++)
{
printf("\t%d", a[i][j]);
}
printf("\n\n");
}
/* Transposing array */
for(i=0; i<m; i++)
{
for(j=0; j<n; j++)
{
b[j][i] = a[i][j];
}
}
printf("\n\n2-D array after transposing:\n\n");
for(i=0; i<n; i++)
{
for(j=0; j<m; j++)
{
printf("\t%d", b[i][j]);
}
printf("\n\n");
}
getch();
}
Sample output:
Enter number of rows & columns of array : 3 x 3
25 12 4
62 34 23
6
25 62 6
12 34 4
4
23
Q7 (i).
#include <stdio.h>
int main()
{
int i, j, rows=4;
for(i=1; i<=rows; ++i)
{
for(j=1; j<=i; ++j)
{
printf("%d", j*10);
}
printf("\n");
}
return 0;
}
Output:
10
10
20
10
20
30
10
20
30
40