245 subscribers
6 photos
3 files
4 links
This group will help beginners to learn programming in C language.

Discussion: @c_coding_discussion
Download Telegram
55. C program to print Fibonacci series of n terms.
#include <stdio.h>

int main(){
int n,n1=0,n3,n2=1;
scanf("%d",&n);
for(int k=0; k<n; k++){
if(k<=1)
n3=k;
else{
n3=n1+n2;
n1=n2;
n2=n3;
}
printf("%d ",n3);
}
return 0;
}
56. C program to find the term of Fibonacci series on a given index.
#include <stdio.h>
//0 1 1 2 3 5 8 13......n
int fib(int n){
if(n==1 || n==0)
{
return 1;
}
else{
return fib(n-2) + fib(n-1);
}
}

int main(){
int n, fTerm;
scanf("%d",&n);

fTerm=fib(n-1);
printf("%d",fTerm);
return 0;
}
57. C program to check whether a character is a Vowel / Constant / Number / Spacial symbol / space of a given string.
#include<stdio.h>
#include<string.h>
int main()
{
char s[200];
int len;
printf("enter a string: ");
scanf("%[^\n]%*c",&s);
len=strlen(s);

for(int p=0; p< len; p++)
{
if(s[p]==97 s[p]==101 s[p]==105 s[p]==111 s[p]==117)
{
printf("%c is a vowel\n",s[p]);
continue;
}

else if(s[p]==' '){
printf("space\n");
}
else if(s[p] >= 48 && s[p] <= 57)
{
printf("%c is a number\n", s[p]);
}
else if((s[p]>=33 && s[p]<= 47) (s[p]>= 58 && s[p]<= 64) (s[p]>= 91 && s[p]<= 96) || (s[p] >= 123 && s[p] <= 126))
{
printf("%c is a special symbol\n",s[p]);
}
else{
printf("%c is a constent\n",s[p]);
}
}
return 0;
}
58. C program to print sparse matrix of a given matrix.

#include<stdio.h>

int main()
{
int a[10][10],spmt[10][3],m,n,temp=0,i,j;
printf("Enter the range of matix: ");
scanf("%d%d",&n,&m);
printf("enter the elements of the matrix:\n");
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("your matrix matrix is:\n");
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
printf("%d ",a[i][j]);
}
printf("\n");
}
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
if(a[i][j]!=0)
{
spmt[temp][0]=a[i][j];
spmt[temp][1]=i;
spmt[temp][2]=j;
temp++;
}
}
}
printf("\nThe sparse matrix:\n");
for(i=0;i<temp;i++)
{
for(j=0;j<3;j++)
{
printf("%d ",spmt[i][j]);
}
printf("\n");
}
return 0;
}
C Coding pinned «C_Coding_Discussion This is only for discussion about C programming.. https://t.me/c_coding_discussion»
59. C program to reverse a single string without using 'strrev()' library function.
#include <stdio.h>

int main()
{
int n;
printf("enter the length of the string: ");
scanf("%d", &n);
char str[n];
printf("enter the string\n");

for(int j=0; j<=n; j++){
scanf("%c", &str[j]);
}
for(int i =n; i>=0; --i){
printf("%c", str[i]);
}
return 0;
}
60. C program to find roots of quadratic equation.

#include <stdio.h>
#include <math.h>

int main(){
int a,b,c;
printf("enter the value of ab and c: ");
scanf("%d %d %d",&a, &b, &c);

float r1, r2;
float s = (b*b) - 4 * a * c;

if(s>0){
printf("roots are real:\n");
r1 = ((-b + sqrt(s)) / (2*a));
r2 = ((-b - sqrt(s)) / (2*a));
printf("(%.f, %.f)", r1, r2);
}
else{
printf("roots are imaginary");
r1 = -b / 2*a;
r2 = sqrt(s) / 2*a;
printf("(%.f+%.fi), (%.f+%.fi)",r1, r2, r1, r2);
}

return 0;
}
|Quiz. #8|
What is the output?
#include <stdio.h> int main() { printf ("%d & %d", (36 & 28), (36 | 28)); return 0; }
Final Results
13%
36 28
16%
36 & 28
29%
4 & 60
42%
Error!!
#Explanation |Quiz. #8|
First of all, for both operations we have to convert these numbers in the of 8 bits(binary form) then apply to operations of '&' and '|' they are:
> & Will return 1 when both conditions are true i.e 1, otherwise it will return false i.e 0.
> | Will return 1 when any one condition is true I.e 1, otherwise it'll return false I.e 0
____________________________
(2^n..........32 16 8 4 2 1)
36 & 28:
36 -> 0 0 1 0 0 1 0 0
28 -> 0 0 0 1 1 1 0 0
______________
0 0 0 0 0 1 0 0 -> 4 ans.
36 | 28:
36 -> 0 0 1 0 0 1 0 0
28 -> 0 0 0 1 1 1 0 0
______________
0 0 1 1 1 1 0 0 -> 60 ans.


Hope you all understood.
Happy coding :)
61. C program to find number of characters and number of spaces in a given string.

#include <stdio.h>
int main()
{
char str[]="do not do everything for something, just do something you will achieve everything\n";
printf("%s\n", str);
int countNum=0, countSp=0;
for(int a=0; a< strlen(str); a++)
{
if(str[a] != ' ') {
countNum++;
}
else{
countSp++;
}
}
printf("number of characters is: %d\nnumber of spaces is: %d\n",countNum, countSp);
return 0;
}
👍1
|Quiz. #9|
What is the output?
#include<stdio.h> int main(){ int ar[6]={4,1,9,8,5,9,5}; int *ptr1=ar; int *ptr2=&ar[4]; int res=ptr2 - ptr1; printf("%d",ar[res]); return 0; }
Anonymous Quiz
21%
1
21%
4
34%
5
23%
Error!
62. C program to compare first n characters of two given strings.
#include<stdio.h>
#include<string.h>
int main()
{
char str1[100], str2[100];
int n, cmp;
printf("enter first steing: ");
scanf("%[^\n]%*c", &str1);
printf("enter second string: ");
scanf("%[^\n]%*c", &str2);
printf("%s \n%s\n", str1, str2);

printf("enter number of characters: ");
scanf("%d", &n);

cmp=strncmp(str1, str2, n);
if(cmp==0)
printf("%d characters of these strings are equal",n);
else
printf("not equal");
return 0;
}
63. C program to check how many characters are equal in two given strings. (Without using library function).
#include <stdio.h>

void cmpStr(char *str1, char *str2, int n){
int cmp=0;
for(int i=0; i<n; i++){
if(((str1[i] >=65 && str1[i]<=90) (str1[i] >= 97 && str1[i]<=122)) == ((str2[i] >=65 && str2[i]<=90) (str2[i] >= 97 && str2[i]<=122))){
if(str1[i] == str2[i]){
cmp=1;
}
else{
cmp=0;
}
}
if(cmp==1){
printf("character no. %d are equal\n", i);
}
else
printf("character no. %d are not equal\n", i);
}
}
int main()
{
char str1[100], str2[100];
int n;
printf("enter 1st srting: ");
scanf("%[^\n]%*c", &str1);
printf("enter 2nd srting: ");
scanf("%[^\n]%*c", &str2);
printf("enter number of character you ant to compare: ");
scanf("%d", &n);

cmpStr(str1, str2, n);
return 0;
}
64. C program to compare n characters of two strings without case sensitivity.(without using library function)
#include <stdio.h>

void cmpStr(char *str1, char *str2, int n){
int cmp=0;
for(int i=0; i<n; i++){
if(((str1[i] >=65 && str1[i]<=90) (str1[i] >= 97 && str1[i]<=122)) == ((str2[i] >=65 && str2[i]<=90) (str2[i] >= 97 && str2[i]<=122))){
if(str1[i] == str2[i]){
cmp=1;
}
else{
cmp=0;
}
}
}
if(cmp==1){
printf("equal\n");
}
else
printf("not equal\n");
}

int main()
{
char str1[100], str2[100];
int n;
printf("enter 1st srting: ");
scanf("%[^\n]%*c", &str1);
printf("enter 2nd srting: ");
scanf("%[^\n]%*c", &str2);
printf("enter number of character you ant to compare: ");
scanf("%d", &n);

cmpStr(str1, str2, n);
return 0;
}
C Coding pinned Deleted message
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;
}
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;
}
#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;
}
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;
}
#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;
}
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;
}