30. C program to find factorial of a given number using Recursion.
#include <stdio.h>
int factorial(int facto){
if (facto == 1 || facto == 0){
return 1;
}
else{
return facto * factorial (facto - 1);
}
}
int main(){
int facto;
printf("enter a number: ");
scanf("%d",&facto);
printf("factorial is %d", factorial (facto));
return 0;
}
#include <stdio.h>
int factorial(int facto){
if (facto == 1 || facto == 0){
return 1;
}
else{
return facto * factorial (facto - 1);
}
}
int main(){
int facto;
printf("enter a number: ");
scanf("%d",&facto);
printf("factorial is %d", factorial (facto));
return 0;
}
answer would be 'false' causes:
#explanation |Quiz. #4|
as we all know that when we use signed variable of any data type, first compiler will convert
it into unsigned value, proccess to convert signed into unsigned is that:
signed char = -23
1)binary of 23 is (00010111)
2)1's complement of (00010111) this : (11101000)
3)for convert it into unsigned add 1 into (11101000): 11101001 this is the binary of 233
and unsigned value of -23 is 233,
Now!
unsigned int i=23;
signed char c=-23;
if(i>c)
printf("true");
else
printf("false");
::: here we are comparing (23>233) which is false, so final answer would be false.
#explanation |Quiz. #4|
as we all know that when we use signed variable of any data type, first compiler will convert
it into unsigned value, proccess to convert signed into unsigned is that:
signed char = -23
1)binary of 23 is (00010111)
2)1's complement of (00010111) this : (11101000)
3)for convert it into unsigned add 1 into (11101000): 11101001 this is the binary of 233
and unsigned value of -23 is 233,
Now!
unsigned int i=23;
signed char c=-23;
if(i>c)
printf("true");
else
printf("false");
::: here we are comparing (23>233) which is false, so final answer would be false.
33. C program to convert numbers from decimal to binary.
#include <stdio.h>
int main() {
printf("***DECIMAL TO BINARY***\n");
int num ,i=0;
int arr[10];
scanf("%d",&num);
printf ("you have entered %d\nand its binary is: ",num);
while(num>0){
arr[i] = num % 2;
num = num / 2;
i++;
}
for(int j= i-1;j>=0; j--){
printf ("%d ",arr[j]);
}
return 0;
}
#include <stdio.h>
int main() {
printf("***DECIMAL TO BINARY***\n");
int num ,i=0;
int arr[10];
scanf("%d",&num);
printf ("you have entered %d\nand its binary is: ",num);
while(num>0){
arr[i] = num % 2;
num = num / 2;
i++;
}
for(int j= i-1;j>=0; j--){
printf ("%d ",arr[j]);
}
return 0;
}
35. C program to convert lower case character into upper case character (without using string functions).
#include <stdio.h>
int main()
{
char lower;
int upper;
printf("enter a lower case character: ");
scanf("%c",&lower);
if(lower >=97 && lower<= 122){
upper = lower - 32;
printf("%c",upper);
}
return 0;
}
#include <stdio.h>
int main()
{
char lower;
int upper;
printf("enter a lower case character: ");
scanf("%c",&lower);
if(lower >=97 && lower<= 122){
upper = lower - 32;
printf("%c",upper);
}
return 0;
}
36. C program to convert upper case character into lower case character (without using string functions).
#include <stdio.h>
int main()
{
int lower;
char upper;
printf("enter a upper case character: ");
scanf("%c",&upper);
if(upper >=65 && upper<= 90){
lower = upper + 32;
printf("%c", lower);
}
return 0;
}
#include <stdio.h>
int main()
{
int lower;
char upper;
printf("enter a upper case character: ");
scanf("%c",&upper);
if(upper >=65 && upper<= 90){
lower = upper + 32;
printf("%c", lower);
}
return 0;
}
37. Define a structure called VTUstudents , it comprise the fields: VTU No, Name, Branch of Study and Gender. Use array of structures for storing such records randomly. Design and develop a C program for clustering the records based on branch of study and gender (passing arrays aa argument).
#include <stdio.h>
#include <string.h>
struct vtu{
char sName[15];
char sBranch[4];
char sGender[15];
int vtu;
};
//struct vtu st;
void student(char name[15], char br[3], char gen[15], int vtu, int n){
struct vtu st;
int i;
char gender[10], branch[4];
printf("The rcords of vtu are: \n");
for(i=0; i<n; i++){
printf("%d\n", vtu);
printf("%s\n", name);
printf("%s\n", br);
printf("%s\n", gen);
}
printf("enter the gender to be printed: ");
scanf("%s", gender);
for(i=0; i<n; i++){
if(strcmp(gen, gender) == 0){
printf("%d\n", vtu);
printf("%s\n", name);
printf("%s\n", br);
printf("%s\n", gen);
}
}
printf("enter the branch to be printd: \n");
scanf("%s", branch);
for(i=0; i<n; i++){
if(strcmp(br, branch) == 0){
printf("%d\n",vtu);
printf("%s\n", name);
printf("%s\n", br);
printf("%s\n", gen);
}
}
}
int main()
{
struct vtu st[10];
int i,n;
// char gender[10], branch[4];
printf("Enter the number of records: ");
scanf("%d", &n);
for(i =0; i<n; i++){
printf("Enter details of student for record %d\n", i+1);
printf("enter vtu number: ");
scanf("%d", &st[i].vtu);
printf("enter name: ");
scanf("%s", st[i].sName);
printf("enter branch: ");
scanf("%s", st[i].sBranch);
printf("enter gemder: ");
scanf("%s", st[i].sGender);
student( st[i].sName, st[i].sBranch, st[i].sGender, st[i].vtu, n);
}
return 0;
}
#include <stdio.h>
#include <string.h>
struct vtu{
char sName[15];
char sBranch[4];
char sGender[15];
int vtu;
};
//struct vtu st;
void student(char name[15], char br[3], char gen[15], int vtu, int n){
struct vtu st;
int i;
char gender[10], branch[4];
printf("The rcords of vtu are: \n");
for(i=0; i<n; i++){
printf("%d\n", vtu);
printf("%s\n", name);
printf("%s\n", br);
printf("%s\n", gen);
}
printf("enter the gender to be printed: ");
scanf("%s", gender);
for(i=0; i<n; i++){
if(strcmp(gen, gender) == 0){
printf("%d\n", vtu);
printf("%s\n", name);
printf("%s\n", br);
printf("%s\n", gen);
}
}
printf("enter the branch to be printd: \n");
scanf("%s", branch);
for(i=0; i<n; i++){
if(strcmp(br, branch) == 0){
printf("%d\n",vtu);
printf("%s\n", name);
printf("%s\n", br);
printf("%s\n", gen);
}
}
}
int main()
{
struct vtu st[10];
int i,n;
// char gender[10], branch[4];
printf("Enter the number of records: ");
scanf("%d", &n);
for(i =0; i<n; i++){
printf("Enter details of student for record %d\n", i+1);
printf("enter vtu number: ");
scanf("%d", &st[i].vtu);
printf("enter name: ");
scanf("%s", st[i].sName);
printf("enter branch: ");
scanf("%s", st[i].sBranch);
printf("enter gemder: ");
scanf("%s", st[i].sGender);
student( st[i].sName, st[i].sBranch, st[i].sGender, st[i].vtu, n);
}
return 0;
}
👍1
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;
}