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
54. C program to find sum of first five prime numbers.
#include <stdio.h>
int isprime(int j){
int p=0;
for(int i = 2 ; i <= j/2; i++){
if(j%i == 0){
p = 1;
}
}
if(p == 0){
return 1;
}
else
return 0;
}
int main(void) {
int num = 1;
int i=0, j= 1;
int sum = 0;
while(num<=5){
j++;
if(isprime(j)){
sum += j;
num++;
}
}
printf("sum =%d",sum);
return 0;
}
|Quiz. #7|
What will be the output?
Anonymous Quiz
12%
5
43%
7
27%
1
18%
Error!
Do you want another group for doubts or queries about these programs and C language?
Anonymous Poll
87%
Yes
13%
No
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