Skip to content

Instantly share code, notes, and snippets.

@anisur3036
Last active November 17, 2022 04:41
Show Gist options
  • Save anisur3036/8de949d5e34f1746cdf40557c9dd818e to your computer and use it in GitHub Desktop.
Save anisur3036/8de949d5e34f1746cdf40557c9dd818e to your computer and use it in GitHub Desktop.
Problem solving in C language
/*
You are given an integer n or –n .If you are given n , print n to –n , if you are given –n, print –n to n.
See the sample output for more clarification.
Sample Input : Sample Output :
5 5 4 3 2 1 0 -1 -2 -3 -4 -5
-4 -4 -3 -2 -1 0 1 2 3 4
*/
#include <stdio.h>
int main() {
// Write C code here
int i, n;
scanf("%d", &n);
if(n<0) {
for(i=n;i<=-(n);i++) {
if(i<=0) {
printf("%d ", i);
}
if(i>0) {
printf("%d ", i);
}
}
}
if(n>0) {
for(i=n;i>=-n;i--) {
if(i>=0) {
printf("%d ", i);
}
if(i<0) {
printf("%d ", i);
}
}
}
return 0;
}
/*
Print the following pattern
For example n = 4
Sample Output
****
****
****
****
*/
#include <stdio.h>
int main() {
// Write C code here
int i, j, k, n;
scanf("%d", &n);
for(i=1; i<=n; i++) {
for(j=n-i; j>0; j--) {
printf(" ");
}
for(k=0; k<n; k++) {
printf("*");
}
printf("\n");
}
return 0;
}
/*
You are given a positive integer n and after that a positive integer array of size n. Now print the sum of last digit
of the given n integers.
Sample Input : Sample Output :
5 Sum = 12
22 44 143 101 12
*/
#include <stdio.h>
int main() {
// Write C code here
int i, j, k, n;
scanf("%d", &n);
int arr[n];
int sum = 0;
for(i=0; i<n; i++) {
scanf("%d", &arr[i]);
}
for(i=0;i<n;i++) {
sum = sum + arr[i] % 10;
}
printf("Sum = %d", sum);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment