65. C programme to convert lower case string into upper case string and upper case string into lower case string.
(Without using library functions)
#include <stdio.h>
void upperToLower(char *upper){
char lower[100];
for(int a=0; a<strlen(upper); a++)
{
if(upper[a] >=65 && upper[a]<= 90){
lower[a] = upper[a]+32;
printf("%c\n", lower[a]);
}
}
}
void lowerToUpper(char *lower){
char upper[100];
for(int a=0; a<strlen(lower); a++)
{
if(lower[a] >=97 && lower[a]<= 122){
upper[a] = lower[a]-32;
printf("%c\n", upper[a]);
}
}
}
int main()
{
char upper[100],lower[100];
printf("enter a upper case string: \n");
scanf("%s",&upper);
upperToLower(upper);
printf("enter a lower case string: \n");
scanf("%s",&lower);
lowerToUpper(lower);
return 0;
}
(Without using library functions)
#include <stdio.h>
void upperToLower(char *upper){
char lower[100];
for(int a=0; a<strlen(upper); a++)
{
if(upper[a] >=65 && upper[a]<= 90){
lower[a] = upper[a]+32;
printf("%c\n", lower[a]);
}
}
}
void lowerToUpper(char *lower){
char upper[100];
for(int a=0; a<strlen(lower); a++)
{
if(lower[a] >=97 && lower[a]<= 122){
upper[a] = lower[a]-32;
printf("%c\n", upper[a]);
}
}
}
int main()
{
char upper[100],lower[100];
printf("enter a upper case string: \n");
scanf("%s",&upper);
upperToLower(upper);
printf("enter a lower case string: \n");
scanf("%s",&lower);
lowerToUpper(lower);
return 0;
}
66. C programme to find minimum and maximum of two number without using CONDITIONAL STATEMENTS.
#include <stdio.h>
int retMax(int n1,int n2){
int ret = n1 ^ ((n1^n2) & -(n1<n2));
return ret;
}
int retMin(int n1, int n2){
int ret = n2 ^ ((n1^n2) & -(n1<n2));
return ret;
}
int main()
{
int x=5, y=3;
int min = retMin(x, y);
int max = retMax(x, y);
printf("minimum between %d and %d is: %d\n",x,y, min);
printf("maximum between %d and %d is: %d\n",x,y, max);
return 0;
}
#include <stdio.h>
int retMax(int n1,int n2){
int ret = n1 ^ ((n1^n2) & -(n1<n2));
return ret;
}
int retMin(int n1, int n2){
int ret = n2 ^ ((n1^n2) & -(n1<n2));
return ret;
}
int main()
{
int x=5, y=3;
int min = retMin(x, y);
int max = retMax(x, y);
printf("minimum between %d and %d is: %d\n",x,y, min);
printf("maximum between %d and %d is: %d\n",x,y, max);
return 0;
}
#DSA_Series
67. C programme to create Linked list using user defined function.
#include<stdio.h>
#include<stdlib.h>
typedef struct link{
int data;
struct link *next;
}link;
void show(link *ptr){
while(ptr != NULL){
printf("%d ", ptr->data);
ptr=ptr->next;
}
printf("\n");
}
link * insert(link *head, int data){
link *ptr = (link *) malloc(sizeof(link));
ptr->data=data;
ptr->next=head;
if(head==NULL){
head=ptr;
ptr->next=NULL;
return ptr;
}
else{
link *p;
p=head;
while(p->next != NULL){
p=p->next;
}
p->next=ptr;
ptr->next=NULL;
return head;
}
}
int main()
{
link *head=(link *) malloc(sizeof(link));
head = NULL;
int n;
printf("enter number of nodes: ");
scanf("%d",&n);
int ar[n];
printf("enter data: \n");
for(int a=0; a<n; a++){
printf("enter the value at index %d\n",a);
scanf("%d",&ar[a]);
}
for(int a=0; a<n; a++){
head = insert(head, ar[a]);
}
printf("your entered elements of linked list are : \n");
show(head);
return 0;
}
67. C programme to create Linked list using user defined function.
#include<stdio.h>
#include<stdlib.h>
typedef struct link{
int data;
struct link *next;
}link;
void show(link *ptr){
while(ptr != NULL){
printf("%d ", ptr->data);
ptr=ptr->next;
}
printf("\n");
}
link * insert(link *head, int data){
link *ptr = (link *) malloc(sizeof(link));
ptr->data=data;
ptr->next=head;
if(head==NULL){
head=ptr;
ptr->next=NULL;
return ptr;
}
else{
link *p;
p=head;
while(p->next != NULL){
p=p->next;
}
p->next=ptr;
ptr->next=NULL;
return head;
}
}
int main()
{
link *head=(link *) malloc(sizeof(link));
head = NULL;
int n;
printf("enter number of nodes: ");
scanf("%d",&n);
int ar[n];
printf("enter data: \n");
for(int a=0; a<n; a++){
printf("enter the value at index %d\n",a);
scanf("%d",&ar[a]);
}
for(int a=0; a<n; a++){
head = insert(head, ar[a]);
}
printf("your entered elements of linked list are : \n");
show(head);
return 0;
}
68. C programme to count how many characters, words, vowels, consonants, digits, spaces and special characters in a given string.
#include <stdio.h>
#include <stdlib.h>
int main()
{
char str[500];
int len, num=0, sym=0, vowel=0, cons=0, wrd=0, total=0;
printf("enter a string: ");
scanf("%[^\n]%*c", &str);
len = strlen(str);
for(int i=0; i<len; i++){
if(str[i] >= 48 && str[i] <= 57)
num++;
else if(str[i] >= 33 && str[i] <=64 str[i]>=91 && str[i] <= 96)
sym++;
else if(str[i]==97 str[i]==101 str[i]==105 str[i]==111 str[i]==117)
vowel++;
else if(str[i]>=65 && str[i] <= 90 str[i] >= 97 && str[i] <= 122)
cons++;
else if(str[i] == ' ')
wrd++;
else
printf("didn't found\n'");
}
printf("numbers: %d\n", num);
printf("speceial symbol: %d\n", sym);
printf("vowel: %d\n", vowel);
printf("constent: %d\n", cons);
printf("words: %d\n", wrd);
printf("total number of characters: %d\n", len);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
int main()
{
char str[500];
int len, num=0, sym=0, vowel=0, cons=0, wrd=0, total=0;
printf("enter a string: ");
scanf("%[^\n]%*c", &str);
len = strlen(str);
for(int i=0; i<len; i++){
if(str[i] >= 48 && str[i] <= 57)
num++;
else if(str[i] >= 33 && str[i] <=64
sym++;
else if(str[i]==97
vowel++;
else if(str[i]>=65 && str[i] <= 90
cons++;
else if(str[i] == ' ')
wrd++;
else
printf("didn't found\n'");
}
printf("numbers: %d\n", num);
printf("speceial symbol: %d\n", sym);
printf("vowel: %d\n", vowel);
printf("constent: %d\n", cons);
printf("words: %d\n", wrd);
printf("total number of characters: %d\n", len);
return 0;
}
#DSA_Series
69. C programme for INSERT (at the beginning, at a given index, at the end), an element in a given LINKED LIST.
#include <stdio.h>
#include <stdlib.h>
typedef struct lList{
int data;
struct lList * next; /*a struct type pointer*/
}lList;
void likedListTraversal(lList *ptr){
while(ptr != NULL){
printf("%d \n",ptr->data);
ptr = ptr->next;
}
}
lList * lList_beginning_insertion(lList * head, int data){
lList *ptr = (lList*) malloc (sizeof(lList));
ptr->next = head;
ptr->data = data;
return ptr;
}
lList * lList_between_insertion(lList * head, int data, int index){
lList *ptr = (lList*) malloc (sizeof(lList));
lList *p = head;
int i=0;
while(i != index-1){
p = p->next;
i++;
}
ptr->data = data;
ptr->next = p->next;
p->next = ptr;
return head;
}
lList * lList_atEnd_insertion(lList * head, int data){
lList *ptr = (lList*) malloc (sizeof(lList));
lList *p =head;
ptr->data = data;
ptr->next = head;
while(p->next != NULL){
p = p->next;
}
p->next = ptr;
ptr->next =NULL;
return head;
}
int main()
{
lList * head;
lList * second;
lList * thired;
lList * fourth;
head = (lList*) malloc (sizeof(lList));
second = (lList*) malloc (sizeof(lList));
thired = (lList*) malloc (sizeof(lList));
fourth = (lList*) malloc (sizeof(lList));
head ->data = 7;
head ->next = thired;
second ->data = 9;
second ->next = thired;
thired ->data = 78;
thired ->next = fourth;
fourth ->data = 8;
fourth ->next = NULL;
printf("Before beginning insertion\n");
likedListTraversal(head);
printf("After beginning insertion\n");
head = lList_beginning_insertion(head,23);
likedListTraversal(head);
printf("Before between insertion\n");
likedListTraversal(head);
printf("After between insertion\n");
head = lList_between_insertion(head,89,2);
likedListTraversal(head);
printf("Before end insertion\n");
likedListTraversal(head);
printf("After end insertion\n");
head = lList_atEnd_insertion(head,900);
likedListTraversal(head);
return 0;
}
69. C programme for INSERT (at the beginning, at a given index, at the end), an element in a given LINKED LIST.
#include <stdio.h>
#include <stdlib.h>
typedef struct lList{
int data;
struct lList * next; /*a struct type pointer*/
}lList;
void likedListTraversal(lList *ptr){
while(ptr != NULL){
printf("%d \n",ptr->data);
ptr = ptr->next;
}
}
lList * lList_beginning_insertion(lList * head, int data){
lList *ptr = (lList*) malloc (sizeof(lList));
ptr->next = head;
ptr->data = data;
return ptr;
}
lList * lList_between_insertion(lList * head, int data, int index){
lList *ptr = (lList*) malloc (sizeof(lList));
lList *p = head;
int i=0;
while(i != index-1){
p = p->next;
i++;
}
ptr->data = data;
ptr->next = p->next;
p->next = ptr;
return head;
}
lList * lList_atEnd_insertion(lList * head, int data){
lList *ptr = (lList*) malloc (sizeof(lList));
lList *p =head;
ptr->data = data;
ptr->next = head;
while(p->next != NULL){
p = p->next;
}
p->next = ptr;
ptr->next =NULL;
return head;
}
int main()
{
lList * head;
lList * second;
lList * thired;
lList * fourth;
head = (lList*) malloc (sizeof(lList));
second = (lList*) malloc (sizeof(lList));
thired = (lList*) malloc (sizeof(lList));
fourth = (lList*) malloc (sizeof(lList));
head ->data = 7;
head ->next = thired;
second ->data = 9;
second ->next = thired;
thired ->data = 78;
thired ->next = fourth;
fourth ->data = 8;
fourth ->next = NULL;
printf("Before beginning insertion\n");
likedListTraversal(head);
printf("After beginning insertion\n");
head = lList_beginning_insertion(head,23);
likedListTraversal(head);
printf("Before between insertion\n");
likedListTraversal(head);
printf("After between insertion\n");
head = lList_between_insertion(head,89,2);
likedListTraversal(head);
printf("Before end insertion\n");
likedListTraversal(head);
printf("After end insertion\n");
head = lList_atEnd_insertion(head,900);
likedListTraversal(head);
return 0;
}
70. C programme to create an array DYNAMICALLY and searches a value whether the entered value is present in array or not.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
int n;
scanf ("%d",&n);
int *arr = (int *)malloc(n*sizeof(int));
printf("enter the values of the array: ");
int val;
for (int i=0; i<n; i++){
scanf("%d",&arr[i]);
}
printf ("enter the element to be searched: ");
scanf ("%d",&val);
for (int i=0; i<n; i++){
if(arr[i] == val){
printf ("entred element %d have been searched on index %d",val,i);
break ;
}
else{
printf ("element is not matching to index %d\n",i);
}
}
free(arr);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
int n;
scanf ("%d",&n);
int *arr = (int *)malloc(n*sizeof(int));
printf("enter the values of the array: ");
int val;
for (int i=0; i<n; i++){
scanf("%d",&arr[i]);
}
printf ("enter the element to be searched: ");
scanf ("%d",&val);
for (int i=0; i<n; i++){
if(arr[i] == val){
printf ("entred element %d have been searched on index %d",val,i);
break ;
}
else{
printf ("element is not matching to index %d\n",i);
}
}
free(arr);
return 0;
}
71. C program to sort an integer array in ascending order(using bubble sort).
#include <stdio.h>
void printArr(int *arr, int size){
for(int i=0; i<=size; i++){
printf("%d ", arr[i]);
}
printf("\n");
}
void sort(int *arr, int size){
int temp;
int isSorted=0;
for(int i=0; i<size-1; i++){
isSorted =1;
for(int j=0; j<=size-i-1; j++){
if(arr[j] > arr[j+1]){
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
isSorted =0;
}
}
if(isSorted){
return;
}
}
}
int main()
{
int arr[]={2,5,3,1,4,7,6};
int size =6;
printf("before sorting\n");
printArr(arr, size);
sort(arr, size);
printf("after sorting\n");
printArr(arr, size);
return 0;
}
#include <stdio.h>
void printArr(int *arr, int size){
for(int i=0; i<=size; i++){
printf("%d ", arr[i]);
}
printf("\n");
}
void sort(int *arr, int size){
int temp;
int isSorted=0;
for(int i=0; i<size-1; i++){
isSorted =1;
for(int j=0; j<=size-i-1; j++){
if(arr[j] > arr[j+1]){
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
isSorted =0;
}
}
if(isSorted){
return;
}
}
}
int main()
{
int arr[]={2,5,3,1,4,7,6};
int size =6;
printf("before sorting\n");
printArr(arr, size);
sort(arr, size);
printf("after sorting\n");
printArr(arr, size);
return 0;
}
72. C programme to convert decimal number to hexadecimal, using recursion.
#include <stdio.h>
#include <math.h>
void con(int n){
int rem, q,p, j=1;
p=n;
int ar[100];
if(n==0)
return;
else{
rem = p % 16;
if(rem < 10)
rem += 48;
else
rem += 55;
ar[j++] = rem;
q = p /16;
con(q);
for(int i=j-1; i>0; i--)
printf("%c", ar[i]);
}
}
int main()
{
char ar[100];
int rem;
int hex=1;
int n;
printf("enter a positive number: ");
scanf("%d", &n);
con(n);
return 0;
}
#include <stdio.h>
#include <math.h>
void con(int n){
int rem, q,p, j=1;
p=n;
int ar[100];
if(n==0)
return;
else{
rem = p % 16;
if(rem < 10)
rem += 48;
else
rem += 55;
ar[j++] = rem;
q = p /16;
con(q);
for(int i=j-1; i>0; i--)
printf("%c", ar[i]);
}
}
int main()
{
char ar[100];
int rem;
int hex=1;
int n;
printf("enter a positive number: ");
scanf("%d", &n);
con(n);
return 0;
}
73. C programme to check whether a matrices is symmetric matric or not.
#include <stdio.h>
int main()
{
int m, n, i, j;
printf("enter thr value of rows: ");
scanf("%d", &m);
printf("enter the value of columns: ");
scanf("%d", &n);
char mat[m][n];
char sym[m][n];
printf("enter the value of the matrices:\n");
for(i=0; i<m; i++){
for(j=0; j<n; j++){
scanf("%d", &mat[i][j]);
}
}
for(i=0; i<m; i++){
for(j=0; j<n; j++){
sym[j][i] = mat[i][j];
}
}
if(m==n){
for(i=0; i<m; i++){
for(j=0; j<m; j++){
if(mat[i][j] != sym[i][j]){
break;
}
}
if(j != m){
break;
}
}
}
if(i==m){
printf("matices is symmetric!");
}
else{
printf("matrices is not symmetric!");
}
return 0;
}
#include <stdio.h>
int main()
{
int m, n, i, j;
printf("enter thr value of rows: ");
scanf("%d", &m);
printf("enter the value of columns: ");
scanf("%d", &n);
char mat[m][n];
char sym[m][n];
printf("enter the value of the matrices:\n");
for(i=0; i<m; i++){
for(j=0; j<n; j++){
scanf("%d", &mat[i][j]);
}
}
for(i=0; i<m; i++){
for(j=0; j<n; j++){
sym[j][i] = mat[i][j];
}
}
if(m==n){
for(i=0; i<m; i++){
for(j=0; j<m; j++){
if(mat[i][j] != sym[i][j]){
break;
}
}
if(j != m){
break;
}
}
}
if(i==m){
printf("matices is symmetric!");
}
else{
printf("matrices is not symmetric!");
}
return 0;
}
74. C program to perform the arithmetic operations (Addition, subtraction, multiplication) on two matrices and print's result in the existing file. (These matrices are taken from file.)
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE *fptr;
FILE *fptr2;
int r,c,g;
r=c=g=2;
int m1[r][c], m2[r][c],sum[r][c],sub[r][c],mult[r][c];
int mat[2];
fptr2 = fopen("pwf2.txt", "rt");
for(int i=0; i<r; i++){
for(int j=0; j<c; j++){
fscanf(fptr2,"%d", &m1[i][j]);
}
for(int k=0; k<g; k++)
fscanf(fptr2, "%d", &mat[k]);
}
fseek(fptr2,0,SEEK_SET);
for(int i=0; i<r; i++){
for(int k=0; k<g; k++)
fscanf(fptr2, "%d", &mat[k]);
for(int j=0; j<c; j++){
fscanf(fptr2,"%d", &m2[i][j]);
}
}
printf("\a\a\aplease check your file\n\n");
fclose(fptr2);
fptr = fopen("pwf2.txt", "a");
fprintf(fptr, "\nsum of the matrices:\n");
for(int i=0; i<r; i++){
for(int j=0; j<c; j++){
sum[i][j] = m1[i][j] + m2[i][j];
fprintf(fptr, "%d ", sum[i][j]);
}
fprintf(fptr,"\n");
}
fprintf(fptr, "\nmultiplication of the matrices:\n");
for(int i=0; i<r; i++){
for(int j=0; j<c; j++){
mult[i][j] = m1[i][j] * m2[i][j];
fprintf(fptr, "%d ", mult[i][j]);
}
fprintf(fptr,"\n");
}
fprintf(fptr, "\nsubtraction of the matrices:\n");
for(int i=0; i<r; i++){
for(int j=0; j<c; j++){
sub[i][j] = m1[i][j] - m2[i][j];
fprintf(fptr, "%d ", sub[i][j]);
}
fprintf(fptr,"\n");
}
fclose(fptr);
return 0;
}
Programmed by: @miss_rupali
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE *fptr;
FILE *fptr2;
int r,c,g;
r=c=g=2;
int m1[r][c], m2[r][c],sum[r][c],sub[r][c],mult[r][c];
int mat[2];
fptr2 = fopen("pwf2.txt", "rt");
for(int i=0; i<r; i++){
for(int j=0; j<c; j++){
fscanf(fptr2,"%d", &m1[i][j]);
}
for(int k=0; k<g; k++)
fscanf(fptr2, "%d", &mat[k]);
}
fseek(fptr2,0,SEEK_SET);
for(int i=0; i<r; i++){
for(int k=0; k<g; k++)
fscanf(fptr2, "%d", &mat[k]);
for(int j=0; j<c; j++){
fscanf(fptr2,"%d", &m2[i][j]);
}
}
printf("\a\a\aplease check your file\n\n");
fclose(fptr2);
fptr = fopen("pwf2.txt", "a");
fprintf(fptr, "\nsum of the matrices:\n");
for(int i=0; i<r; i++){
for(int j=0; j<c; j++){
sum[i][j] = m1[i][j] + m2[i][j];
fprintf(fptr, "%d ", sum[i][j]);
}
fprintf(fptr,"\n");
}
fprintf(fptr, "\nmultiplication of the matrices:\n");
for(int i=0; i<r; i++){
for(int j=0; j<c; j++){
mult[i][j] = m1[i][j] * m2[i][j];
fprintf(fptr, "%d ", mult[i][j]);
}
fprintf(fptr,"\n");
}
fprintf(fptr, "\nsubtraction of the matrices:\n");
for(int i=0; i<r; i++){
for(int j=0; j<c; j++){
sub[i][j] = m1[i][j] - m2[i][j];
fprintf(fptr, "%d ", sub[i][j]);
}
fprintf(fptr,"\n");
}
fclose(fptr);
return 0;
}
Programmed by: @miss_rupali
75. C program to DELETE and MODIFY the records of the student, using structure.
Amit
#include <stdio.h>
#include <string.h>
typedef struct student{
char name[20];
char phNo[10];
int id;
}student;
int main()
{
student s;
int choose;
int choice;
printf("enter the name of the student:\n ");
scanf("%s", s.name);
printf("enter the phone number of student: \n");
scanf("%s", s.phNo);
printf("enter the id of the student: \n");
scanf("%d", &s.id);
printf("press 1 for display the student records\n");
printf("press 2 for delete the student records\n");
printf("press 3 for modify the student records\n");
scanf("%d", &choose);
switch(choose){
case 1:{
printf("name: %s\n", s.name);
printf("id: %d\n", s.id);
printf("phNo: %s\n", s.phNo);
break;
}
case 2:{
printf("enter the record to be deleted: \n");
printf("press 1 for delete the name of the student: \n");
printf("press 2 for delete the id of the student: \n");
printf("press 3 for delete the phone number of the student: \n");
printf("enter your choice: ");
scanf("%d", &choice);
if(choice==1){
for(int i=0; i<strlen(s.name); i++){
s.name[i] = NULL;
}
}
else if(choice ==2)
s.id = NULL;
else if(choice ==3){
for(int i=0; i<strlen(s.phNo); i++){
s.phNo[i] = NULL;
}
}
else
printf("invalid choice\n");
}
printf("STRUDENT RECORDS\nAfter deletion\n");
printf("name: %s\n", s.name);
printf("id: %d\n", s.id);
printf("phNo: %s\n", s.phNo);
break;
case 3:{
printf("enter the new name: \n");
scanf("%s", s.name);
printf("enter the new id: \n");
scanf("%d", &s.id);
printf("enter the new phone number: \n");
scanf("%s", s.phNo);
printf("NEW RECORDS OF STUDENT ARE: \n");
printf("name: %s\n", s.name);
printf("id: %d\n", s.id);
printf("phNo: %s\n", s.phNo);
break;
}
default:{
printf("please enter a valid choice!\n");
}
}
return 0;
}
Programmed by: @miss_rupali
Amit
#include <stdio.h>
#include <string.h>
typedef struct student{
char name[20];
char phNo[10];
int id;
}student;
int main()
{
student s;
int choose;
int choice;
printf("enter the name of the student:\n ");
scanf("%s", s.name);
printf("enter the phone number of student: \n");
scanf("%s", s.phNo);
printf("enter the id of the student: \n");
scanf("%d", &s.id);
printf("press 1 for display the student records\n");
printf("press 2 for delete the student records\n");
printf("press 3 for modify the student records\n");
scanf("%d", &choose);
switch(choose){
case 1:{
printf("name: %s\n", s.name);
printf("id: %d\n", s.id);
printf("phNo: %s\n", s.phNo);
break;
}
case 2:{
printf("enter the record to be deleted: \n");
printf("press 1 for delete the name of the student: \n");
printf("press 2 for delete the id of the student: \n");
printf("press 3 for delete the phone number of the student: \n");
printf("enter your choice: ");
scanf("%d", &choice);
if(choice==1){
for(int i=0; i<strlen(s.name); i++){
s.name[i] = NULL;
}
}
else if(choice ==2)
s.id = NULL;
else if(choice ==3){
for(int i=0; i<strlen(s.phNo); i++){
s.phNo[i] = NULL;
}
}
else
printf("invalid choice\n");
}
printf("STRUDENT RECORDS\nAfter deletion\n");
printf("name: %s\n", s.name);
printf("id: %d\n", s.id);
printf("phNo: %s\n", s.phNo);
break;
case 3:{
printf("enter the new name: \n");
scanf("%s", s.name);
printf("enter the new id: \n");
scanf("%d", &s.id);
printf("enter the new phone number: \n");
scanf("%s", s.phNo);
printf("NEW RECORDS OF STUDENT ARE: \n");
printf("name: %s\n", s.name);
printf("id: %d\n", s.id);
printf("phNo: %s\n", s.phNo);
break;
}
default:{
printf("please enter a valid choice!\n");
}
}
return 0;
}
Programmed by: @miss_rupali
76. C program to delete duplicate elements from a sorted array.
#include <stdio.h>
int rem_duplicate_ele(int arr[],int size){
int j =0, i, k;
if(size == 0 || size == 1)
return size;
int temp[size];
for(i = 0; i < size -1; i++)
if(arr[i] != arr[i+1])
temp[j++] = arr[i];
temp[j++] = arr[size-1];
for(k =0; k < j; k++)
arr[k]=temp[k];
return j;
}
int main()
{
int arr[] = {1,1,3,3,5,6,6,6,6,};
int n = sizeof(arr) / sizeof(arr[0]);
for(int i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
n = rem_duplicate_ele(arr, n);
for (int i=0; i<n; i++)
printf("%d ", arr[i]);
return 0;
}
Programmed by: @miss_rupali
#include <stdio.h>
int rem_duplicate_ele(int arr[],int size){
int j =0, i, k;
if(size == 0 || size == 1)
return size;
int temp[size];
for(i = 0; i < size -1; i++)
if(arr[i] != arr[i+1])
temp[j++] = arr[i];
temp[j++] = arr[size-1];
for(k =0; k < j; k++)
arr[k]=temp[k];
return j;
}
int main()
{
int arr[] = {1,1,3,3,5,6,6,6,6,};
int n = sizeof(arr) / sizeof(arr[0]);
for(int i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
n = rem_duplicate_ele(arr, n);
for (int i=0; i<n; i++)
printf("%d ", arr[i]);
return 0;
}
Programmed by: @miss_rupali
77. C program to check whether number is positive or negative without using Comparison operators.
#include <stdio.h>
int main()
{
int x;
printf("enter a number: ");
scanf("%d",&x);
if(x>>31)
printf("negative");
else
printf("positive");
return 0;
}
Programmed by: @miss_rupali
#include <stdio.h>
int main()
{
int x;
printf("enter a number: ");
scanf("%d",&x);
if(x>>31)
printf("negative");
else
printf("positive");
return 0;
}
Programmed by: @miss_rupali
#patter_series
78(a). C program to print the following pattern.
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
#include<stdio.h>
int main()
{
int row=5;
int col=4;
int pr=1;
for(int a=1;a<=row;a++){
for(int b=1;b<=col;b++){
printf("%d ",pr);
pr++;
}
printf("\n");
}
return 0;
}
Programmed by: @miss_rupali
78(a). C program to print the following pattern.
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
#include<stdio.h>
int main()
{
int row=5;
int col=4;
int pr=1;
for(int a=1;a<=row;a++){
for(int b=1;b<=col;b++){
printf("%d ",pr);
pr++;
}
printf("\n");
}
return 0;
}
Programmed by: @miss_rupali
#patter_series
78(b). C program to print the following pattern
5
5 4
5 4 3
5 4 3 2
5 4 3 2 1
#include<stdio.h>
int main()
{
for(int a=5;a>=1;a--){
for(int b=5;b>=1;b--){
if(b>=a)
printf("%d ",b);
}
printf("\n");
}
return 0;
}
Programmed by: @miss_rupali
78(b). C program to print the following pattern
5
5 4
5 4 3
5 4 3 2
5 4 3 2 1
#include<stdio.h>
int main()
{
for(int a=5;a>=1;a--){
for(int b=5;b>=1;b--){
if(b>=a)
printf("%d ",b);
}
printf("\n");
}
return 0;
}
Programmed by: @miss_rupali
#patter_series
78(c). C program to print the following pattern
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
#include<stdio.h>
int main()
{
for(int a=1;a<=5;a++){
for(int b=1;b<=5;b++){
if(b<=a)
printf("%d ",b);
}
printf("\n");
}
return 0;
}
Programmed by: @miss_rupali
78(c). C program to print the following pattern
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
#include<stdio.h>
int main()
{
for(int a=1;a<=5;a++){
for(int b=1;b<=5;b++){
if(b<=a)
printf("%d ",b);
}
printf("\n");
}
return 0;
}
Programmed by: @miss_rupali
79. C program to find sum of even and odd numbers between 0 to n.
#include <stdio.h>
int main() {
int n=100, num2, sum1 = 0,sum2=0;
for(int i = 0; i < n; i++){
if(i % 2 ==0){
sum1= sum1+i;
}
else{
sum2=sum2+i;
}
}
printf("sum even numbers: %d\n sum of odd numbers: %d",sum1,sum2);
return 0;
}
Programmed by: @miss_rupali
#include <stdio.h>
int main() {
int n=100, num2, sum1 = 0,sum2=0;
for(int i = 0; i < n; i++){
if(i % 2 ==0){
sum1= sum1+i;
}
else{
sum2=sum2+i;
}
}
printf("sum even numbers: %d\n sum of odd numbers: %d",sum1,sum2);
return 0;
}
Programmed by: @miss_rupali
80. C program to find maximum and minimum of a integer array.
#include <stdio.h>
int main()
{
int ar[5]={3,2,6,12,8};
int min,max;
min=max=ar[0];
int i,j;
int *ptr = &ar[0];
for(i=0; i<5; i++){
if(min>ptr[i])
min=ptr[i];
if(max <ptr[i])
max=ptr[i];
}
printf("\nmaximum element of the array is %d\nminimum element of the array is %d\n",max,min);
return 0;
}
Programmed by: @miss_rupali
#include <stdio.h>
int main()
{
int ar[5]={3,2,6,12,8};
int min,max;
min=max=ar[0];
int i,j;
int *ptr = &ar[0];
for(i=0; i<5; i++){
if(min>ptr[i])
min=ptr[i];
if(max <ptr[i])
max=ptr[i];
}
printf("\nmaximum element of the array is %d\nminimum element of the array is %d\n",max,min);
return 0;
}
Programmed by: @miss_rupali
81. C program to generate n random numbers between 0 to 100 and print lowest and highest number between these random numbers.
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
void genRandNum(int n)
{
int arr[n];
int randomNum,low,high;
srand(time(NULL));
for(int a=1; a<=n; a++){
randomNum=rand()%100;
printf("%d\n", randomNum);
arr[a]= randomNum;
}
high=low=arr[1];
for(int a=1;a<=n;a++){
if(low>arr[a])
low=arr[a];
if(high<arr[a])
high=arr[a];
}
printf("highest number between these random numbers is :%d\nlowest number between these random numbers is %d",high,low);
}
int main(){
genRandNum(50);
return 0;
}
Programmed by: @miss_rupali
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
void genRandNum(int n)
{
int arr[n];
int randomNum,low,high;
srand(time(NULL));
for(int a=1; a<=n; a++){
randomNum=rand()%100;
printf("%d\n", randomNum);
arr[a]= randomNum;
}
high=low=arr[1];
for(int a=1;a<=n;a++){
if(low>arr[a])
low=arr[a];
if(high<arr[a])
high=arr[a];
}
printf("highest number between these random numbers is :%d\nlowest number between these random numbers is %d",high,low);
}
int main(){
genRandNum(50);
return 0;
}
Programmed by: @miss_rupali
👍1
82. C program to convert string into machine language.
#include <stdio.h>
int main() {
printf("***|CONVERTING STRING INTO MACHINE LANGUAGE|***\n");
int i=0;
int arr[10];
int num[100];
char str[100];
int len;
printf("enter a string: ");
scanf("%[^\n]%*c",&str);
len=strlen(str);
printf("Binary\n");
for(i=0;i<len;i++)
num[i]=str[i];
for(int a=0;a<len;a++){
i=0;
while(num[a]>0){
arr[i] = num[a] % 2;
num[a] = num[a] / 2;
i++;
}
for(int j= i-1;j>=0; j--){
printf ("%d",arr[j]);
}
printf(" ");
}
return 0;
}
Programmed by: @miss_rupali
#include <stdio.h>
int main() {
printf("***|CONVERTING STRING INTO MACHINE LANGUAGE|***\n");
int i=0;
int arr[10];
int num[100];
char str[100];
int len;
printf("enter a string: ");
scanf("%[^\n]%*c",&str);
len=strlen(str);
printf("Binary\n");
for(i=0;i<len;i++)
num[i]=str[i];
for(int a=0;a<len;a++){
i=0;
while(num[a]>0){
arr[i] = num[a] % 2;
num[a] = num[a] / 2;
i++;
}
for(int j= i-1;j>=0; j--){
printf ("%d",arr[j]);
}
printf(" ");
}
return 0;
}
Programmed by: @miss_rupali
#DSA_Series
83. C program to search an element in array by using Binary Search.
#include <stdio.h>
int binary_search(int arr[], int size, int element);
int main()
{
int arr[] = {1,2,7,12,23,27,28,39,45,67};
int element = 45;
int size = sizeof(arr) / sizeof(int);
int search_ind = binary_search(arr,size ,element);
printf("The element %d was found at index %d\n",element, search_ind);
return 0;
}
int binary_search(int arr[], int size, int element){
int low, mid, high;
low = 0;
high = size-1;
while(low <= high){
mid = (low + high) / 2;
if(arr[mid] == element){
return mid;
}
else if(arr[mid] < element){
low = mid+1;
}
else{
high = mid-1;
}
}
return -1;
}
Programmed by: @miss_rupali
83. C program to search an element in array by using Binary Search.
#include <stdio.h>
int binary_search(int arr[], int size, int element);
int main()
{
int arr[] = {1,2,7,12,23,27,28,39,45,67};
int element = 45;
int size = sizeof(arr) / sizeof(int);
int search_ind = binary_search(arr,size ,element);
printf("The element %d was found at index %d\n",element, search_ind);
return 0;
}
int binary_search(int arr[], int size, int element){
int low, mid, high;
low = 0;
high = size-1;
while(low <= high){
mid = (low + high) / 2;
if(arr[mid] == element){
return mid;
}
else if(arr[mid] < element){
low = mid+1;
}
else{
high = mid-1;
}
}
return -1;
}
Programmed by: @miss_rupali