38. C program to remove alphabets from a string.
#include <stdio.h>
#include <string.h>
int main()
{
int i=0, j=0, length;
char str[100];
printf("Enter a string with spacial symbols or numbers: ");
gets(str);
length = (int) strlen(str);
while(i != length){
if(str[i] >= 33 && str[i] <= 64)
str[j++] = str[i];
i++;
}
str[j]=0;
printf("string is %s", str);
return 0;
}
#include <stdio.h>
#include <string.h>
int main()
{
int i=0, j=0, length;
char str[100];
printf("Enter a string with spacial symbols or numbers: ");
gets(str);
length = (int) strlen(str);
while(i != length){
if(str[i] >= 33 && str[i] <= 64)
str[j++] = str[i];
i++;
}
str[j]=0;
printf("string is %s", str);
return 0;
}
39. C program to remove whitespace from a string.
#include <stdio.h>
#include <string.h>
int main()
{
int i=0, j=0, length;
char str[100];
printf("Enter a string with whitespaces: ");
gets(str);
length = (int) strlen(str);
while(i != length){
if(str[i] != ' ')
str[j++] = str[i];
i++;
}
str[j]=0;
printf("string is %s", str);
return 0;
}
#include <stdio.h>
#include <string.h>
int main()
{
int i=0, j=0, length;
char str[100];
printf("Enter a string with whitespaces: ");
gets(str);
length = (int) strlen(str);
while(i != length){
if(str[i] != ' ')
str[j++] = str[i];
i++;
}
str[j]=0;
printf("string is %s", str);
return 0;
}
40. C program to check whether a number is prime number or not a prime number.
#include<stdio.h>
int main(){
int num,prime=0;
printf ("enter a number: ");
scanf("%d",&num);
for(int i=2; i<= num/2; i++){
if(num % i == 0){
prime =1;
break;
}
}
if(num==1){
printf ("prime");
}
else {
if(prime == 1){
printf ("%d is not a prime number",num);
}
else{
printf ("%d is a prime number");
}
}
return 0;
}
#include<stdio.h>
int main(){
int num,prime=0;
printf ("enter a number: ");
scanf("%d",&num);
for(int i=2; i<= num/2; i++){
if(num % i == 0){
prime =1;
break;
}
}
if(num==1){
printf ("prime");
}
else {
if(prime == 1){
printf ("%d is not a prime number",num);
}
else{
printf ("%d is a prime number");
}
}
return 0;
}
41. C program to remove HTML tags from a given string.
#include <stdio.h>
#include <string.h>
void remove_tags(char* str){
int i, in, index=0;
for(i=0;i < strlen(str);i++)
{
if(str[i]=='<'){
in=1;
continue;
}
else if(str[i]=='>'){
in=0;
continue;
}
if(in==0){
str[index]=str[i];
index++;
}
}
str[index]='\0';
};
int main()
{
char str[400];
printf("Enter a string with html tags: ");
gets(str);
printf("you have entered: %s\n", str);
remove_tags(str);
printf("\nnow the nes string is :\n %s",str);
return 0;
}
When input: <h1> I am heading</h1>
Output will be: I am heading
#include <stdio.h>
#include <string.h>
void remove_tags(char* str){
int i, in, index=0;
for(i=0;i < strlen(str);i++)
{
if(str[i]=='<'){
in=1;
continue;
}
else if(str[i]=='>'){
in=0;
continue;
}
if(in==0){
str[index]=str[i];
index++;
}
}
str[index]='\0';
};
int main()
{
char str[400];
printf("Enter a string with html tags: ");
gets(str);
printf("you have entered: %s\n", str);
remove_tags(str);
printf("\nnow the nes string is :\n %s",str);
return 0;
}
Output will be: I am heading
42. C program to calculate the distance between two points (√(x2-x1)^2 + (y2-y1)^2).
#include <stdio.h>
#include <math.h>
int main()
{
double x1, x2;
double y1,y2;
double p = 2.0;
double dist;
printf("Enter the value of x1: ");
scanf("%lf", &x1);
printf("Enter the value of y1: ");
scanf("%lf", &y1);
printf("Enter the value of x2: ");
scanf("%lf", &x2);
printf("Enter the value of y2: ");
scanf("%lf", &y2);
double d1, d2;
d1 = x2 -x1;
d2 = y2-y1;
dist = pow(d1,p) + pow(d2,p);
printf("distance between (x1,y1) and (x2, y2) is: %.2lf sqr unit", sqrt(dist));
return 0;
}
#include <stdio.h>
#include <math.h>
int main()
{
double x1, x2;
double y1,y2;
double p = 2.0;
double dist;
printf("Enter the value of x1: ");
scanf("%lf", &x1);
printf("Enter the value of y1: ");
scanf("%lf", &y1);
printf("Enter the value of x2: ");
scanf("%lf", &x2);
printf("Enter the value of y2: ");
scanf("%lf", &y2);
double d1, d2;
d1 = x2 -x1;
d2 = y2-y1;
dist = pow(d1,p) + pow(d2,p);
printf("distance between (x1,y1) and (x2, y2) is: %.2lf sqr unit", sqrt(dist));
return 0;
}
46. C program for count number of digits.
#include<stdio.h>
int main()
{
int n;
int var=0;
printf("enter a sequence of numbers: ");
scanf("%d",&n);
while(n != 0){
var++;
n /= 10;
}
printf("%d",var);
return 0;
}
Counting digits using recursion:-
#include<stdio.h>
int seq(int n)
{
int ret=0;
while(n != 0){
ret++;
n = n/10;
seq(n);
}
return ret;
}
int main()
{
int n;
int var=0;
printf("enter a sequence of numbers: ");
scanf("%d",&n);
printf("%d", seq(n));
return 0;
}
#include<stdio.h>
int main()
{
int n;
int var=0;
printf("enter a sequence of numbers: ");
scanf("%d",&n);
while(n != 0){
var++;
n /= 10;
}
printf("%d",var);
return 0;
}
Counting digits using recursion:-
#include<stdio.h>
int seq(int n)
{
int ret=0;
while(n != 0){
ret++;
n = n/10;
seq(n);
}
return ret;
}
int main()
{
int n;
int var=0;
printf("enter a sequence of numbers: ");
scanf("%d",&n);
printf("%d", seq(n));
return 0;
}
|Quiz. #5| what is the output?
#include <stdio.h>
int main() { int a=0*10+0110+10; printf("%d",a); return 0; }
#include <stdio.h>
int main() { int a=0*10+0110+10; printf("%d",a); return 0; }
Final Results
8%
130
54%
120
19%
82
19%
Error!
#Explanation |Quiz. #5|
The correct option is 82 because when a number starts from 0, it is considered as octal number, the octal value of 0110 is 82 ((0*8^3 + 1*8^2 + 1*8^1 + 0*8^0) + 10).
The correct option is 82 because when a number starts from 0, it is considered as octal number, the octal value of 0110 is 82 ((0*8^3 + 1*8^2 + 1*8^1 + 0*8^0) + 10).
#pattern's
47. C program to print this pattern :-
* *
* *
*
* *
* *
* *
* *
*
* *
* *
* *
* *
*
* *
* *
* *
* *
*
* *
* *
* *
* *
*
* *
* *
______
#include <stdio.h>
int main()
{
for(int k=1; k<=5; k++){
for(int i=1; i<=5; i++){
for(int j=1; j<=5; j++){
if(j>=6-i && j<=6-i || i*i == j*j){
printf("*");
}
else{
printf(" ");
}
}
printf("\n");
}
}
return 0;
}
47. C program to print this pattern :-
* *
* *
*
* *
* *
* *
* *
*
* *
* *
* *
* *
*
* *
* *
* *
* *
*
* *
* *
* *
* *
*
* *
* *
______
#include <stdio.h>
int main()
{
for(int k=1; k<=5; k++){
for(int i=1; i<=5; i++){
for(int j=1; j<=5; j++){
if(j>=6-i && j<=6-i || i*i == j*j){
printf("*");
}
else{
printf(" ");
}
}
printf("\n");
}
}
return 0;
}
48. C program to sum of entered digit's (using recursion).
#include <stdio.h>
int sum(int n){
int s;
if(n <= 0){
return 0;
}
else{
return n%10 + sum(n/10);
}
}
int main()
{
printf("Enter some numbers in sequence: ");
int n;
scanf("%d", &n);
int res = sum(n);
printf("sum of these all numbers is: %d ", res);
return 0;
}
#include <stdio.h>
int sum(int n){
int s;
if(n <= 0){
return 0;
}
else{
return n%10 + sum(n/10);
}
}
int main()
{
printf("Enter some numbers in sequence: ");
int n;
scanf("%d", &n);
int res = sum(n);
printf("sum of these all numbers is: %d ", res);
return 0;
}
#DSA_Series
49. C Program to insert an element in array on the given index.
#include <stdio.h>
void display(int *arr, int size){
for(int i=0; i<=size; i++){
printf("%d ", arr[i]);
}
printf("\n");
}
int insert_arr(int *arr, int val, int size, int capacity, int ind){
if(size>capacity){
printf("Overflow!");
return -1;
}
else{
for(int i=size; i>=ind-1; i--){
arr[i+1] = arr[i];
}
arr[ind] = val;
return 1;
}
}
int main()
{
int arr[100] = {1,2,6,7};
int size = 4, element = 9, index = 1;
printf("Before insertion\n");
display(arr, size);
insert_arr(arr,234, size, 100, index);
size += 1;
printf("After insertion\n");
display(arr, size);
return 0;
}
49. C Program to insert an element in array on the given index.
#include <stdio.h>
void display(int *arr, int size){
for(int i=0; i<=size; i++){
printf("%d ", arr[i]);
}
printf("\n");
}
int insert_arr(int *arr, int val, int size, int capacity, int ind){
if(size>capacity){
printf("Overflow!");
return -1;
}
else{
for(int i=size; i>=ind-1; i--){
arr[i+1] = arr[i];
}
arr[ind] = val;
return 1;
}
}
int main()
{
int arr[100] = {1,2,6,7};
int size = 4, element = 9, index = 1;
printf("Before insertion\n");
display(arr, size);
insert_arr(arr,234, size, 100, index);
size += 1;
printf("After insertion\n");
display(arr, size);
return 0;
}
|Quiz. #6| |Basic|
what is the output of this code?
#include<stdio.h> int main() { int var=5; int *ptr =&var; printf("%d",*ptr++); }
what is the output of this code?
#include<stdio.h> int main() { int var=5; int *ptr =&var; printf("%d",*ptr++); }
Final Results
25%
Address of var
13%
6
25%
5
38%
Error!
# Explanation |Quiz. #6|
Here ,ptr is a pointer,and it'll take the address of variable var i.e it'll store 5, then in printf statement, the value will be print first and then would be increment, so the correct answer is 5.
Here ,ptr is a pointer,and it'll take the address of variable var i.e it'll store 5, then in printf statement, the value will be print first and then would be increment, so the correct answer is 5.
#DSA_Series
50. C program to delete an element in array from a given index.
#include <stdio.h>
void display(int *arr, int size){
for(int i=0; i<=size; i++){
printf("%d ", arr[i]);
}
printf("\n");
}
int delInd_arr(int *arr,int size, int ind){
if(size==-1){
printf("Underflow!");
return -1;
}
else{
for(int i=ind; i<=size; i++){
arr[i] = arr[i+1];
}
return 1;
}
}
int main()
{
int arr[100] = {11,22,65,72};
int size = 3, index = 1;
printf("Before deletion\n");
display(arr, size);
delInd_arr(arr,size, index);
size -= 1;
printf("After deletion\n");
display(arr, size);
return 0;
}
50. C program to delete an element in array from a given index.
#include <stdio.h>
void display(int *arr, int size){
for(int i=0; i<=size; i++){
printf("%d ", arr[i]);
}
printf("\n");
}
int delInd_arr(int *arr,int size, int ind){
if(size==-1){
printf("Underflow!");
return -1;
}
else{
for(int i=ind; i<=size; i++){
arr[i] = arr[i+1];
}
return 1;
}
}
int main()
{
int arr[100] = {11,22,65,72};
int size = 3, index = 1;
printf("Before deletion\n");
display(arr, size);
delInd_arr(arr,size, index);
size -= 1;
printf("After deletion\n");
display(arr, size);
return 0;
}