1.Printing Pattern
Input:
4
Output:
4 4 4 4 4 4 4
4 3 3 3 3 3 4
4 3 2 2 2 3 4
4 3 2 1 2 3 4
4 3 2 2 2 3 4
4 3 3 3 3 3 4
4 4 4 4 4 4 4
Input:
4
Output:
4 4 4 4 4 4 4
4 3 3 3 3 3 4
4 3 2 2 2 3 4
4 3 2 1 2 3 4
4 3 2 2 2 3 4
4 3 3 3 3 3 4
4 4 4 4 4 4 4
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main()
{
int i,j,n;
scanf("%d", &n);
for(i=n; i>1; i--)
{
for(j=n;j>=1;j--)
{
if(j>i) printf("%d ", j);
else printf("%d ", i);
}
for(j=2;j<=n;j++)
{
if(j>i) printf("%d ", j);
else
printf("%d ", i);
}
printf("\n");
}
for(i=1; i<=n; i++)
{
for(j=n;j>=1;j--)
{
if(j>i)
printf("%d ", j);
else printf("%d ", i);
}
for(j=2;j<=n;j++)
{
if(j>i) printf("%d ", j);
else printf("%d ", i);
}
printf("\n");
}
return 0;
}
2. Array Reversal
Input:
8
15 5 16 15 17 11 5 11
Output:
11 5 11 17 15 16 5 15
Input:
8
15 5 16 15 17 11 5 11
Output:
11 5 11 17 15 16 5 15
#include <stdio.h>
#include <stdlib.h>
int main()
{
int num, *arr, i;
scanf("%d", &num);
arr = (int*) malloc(num * sizeof(int));
for(i = 0; i < num; i++) {
scanf("%d", arr + i);
}
for(i = num-1; i >=0; i--)
printf("%d ", *(arr + i));
return 0;
}
3. 1D Array
Input:
7
1 13 15 20 12 13 2
Output:
76
Input:
7
1 13 15 20 12 13 2
Output:
76
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include<string.h>
int main()
{
int n,val,sum=0;
scanf("%d",&n);
while(n--)
{
scanf("%d",&val);
sum+=val;
}
printf("%i",sum);
}
No comments:
Post a Comment