P.S. This lab is not related to numerical analysis. This lab is about basic C and is on students request.
Problem #1
Print a half pyramid
#include <stdio.h> int main() { int i, j, n; printf("\nNumber of line is "); scanf("%d",&n); for(i = 0; i < n; i++) { for (j = 0; j <= i; j++) { printf("*"); } printf("\n"); } getchar(); return 0; }
Problem #2
Draw a 5 level pyramid
#include <stdio.h> int main() { char line1[] = "*"; char line2[] = "**"; char line3[] = "***"; char line4[] = "****"; char line5[] = "*****"; printf("%40s\n",line1); printf("%40s*\n",line2); printf("%40s**\n",line3); printf("%40s***\n",line4); printf("%40s****\n",line5); return 0; }
Problem #3
Scan for n inputs and only take positive inputs
#include <stdio.h> int main() { int arr[100], n, i, temp; scanf("%d", &n); i = 0; while(i < n) { scanf("%d",&temp); if(temp > 0) { arr[i] = temp; i++; } } for(i = 0; i < n; i++) { printf("%d ", arr[i]); } return 0; }
Problem #4
Take a string input and check for number of vowels in the string
#include<stdio.h> int main() { int i, vowel=0; char str[1300]; printf("type ur string\n\n"); scanf("%[^\n]s", &str); for(i=0; str[i]!='\0'; i++){ if(str[i]=='a'||str[i]=='e'||str[i]=='i'||str[i]=='o'||str[i]=='u'||str[i]=='A'||str[i]=='E'||str[i]=='I'||str[i]=='O'||str[i]=='U') { vowel++; }} printf("no of vowel is %d", vowel); }
Problem #5
Take a string input and show the number of letters in the string and convert them to uppercase
#include <stdio.h> #include <ctype.h> int main() { char str[1000]; int i; scanf("%[^\n]s", str); for(i = 0; str[i]; i++) { str[i] = toupper(str[i]); } printf("Length = %d\n", i); printf("%s", str); return 0; }
Problem #6
Take a number input and check if the number is prime or not.
#include <stdio.h> int main() { int i, n, j=0; printf("The number to be tested is "); scanf("%d", &n); for (i=1; i <= n; i++) { if ( n % i == 0 ) {j++;} } if(j==2) {printf("%d is a prime number.", n); } else {printf("%d is not a prime number", n); } return 0; }