0% found this document useful (0 votes)
14 views40 pages

C Que & Sol

The document contains a series of C programming questions and solutions covering various topics such as input/output operations, conditionals, loops, and mathematical calculations. Each question includes a brief description and a code snippet demonstrating the required functionality. The content serves as a practical guide for learning and practicing C programming concepts.

Uploaded by

ak2o2o90yt
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views40 pages

C Que & Sol

The document contains a series of C programming questions and solutions covering various topics such as input/output operations, conditionals, loops, and mathematical calculations. Each question includes a brief description and a code snippet demonstrating the required functionality. The content serves as a practical guide for learning and practicing C programming concepts.

Uploaded by

ak2o2o90yt
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 40

C language question sheet

_____________________
INPUT/OUTPUT QUESTIONS
_____________________

1) Write a C program that prints the perimeter of a rectangle to take its height and width as input

int l,b;
printf("enter the value of length:");
scanf("%i",&l);
printf("enter the value of breadth:");
scanf("%i",&b);
printf("the perimeter of rectangle is %i",2*(l+b));

2)Write a C program that takes hours and minutes as input, and calculates the total number
of minutes.
Expected Output :
Input hours: 5
Input minutes: 37
Total: 337 minutes.

int hrs,min;
printf("enter the value of hrs:");
scanf("%i",&hrs);
5hrs 37mins
printf("enter the value of min:");
hrs=>hrs*60=minutes hours->min->mul by 60
scanf("%i",&min);
printf("total minutes are %i ",(hrs*60)+min);

3)Write a program in C that takes minutes as input, and display the total number of hours and
minutes.
Expected Output :
Input minutes: 546
9 Hours, 6 Minutes

int minutes;
printf("enter the value of minutes:");
546->hrs 546/60=9hrs
scanf("%i",&minutes);
min->hours->div by 60
int hours=minutes/60;
int remaining min=minutes%60;
printf("total hours are %i ",hours);
printf("\ntotal min are %i ",min);

4)Write a program that converts Centigrade to Fahrenheit.

float cent; .
cent-> frah
printf("enter the value of cent:");
scanf("%f",&cent);
printf("the temp in frah is %.2f",(1.8*cent)+32);

5) Write a C program that converts kilometers per hour to miles per hour.

float km;
printf("enter the value of (km/h):");
scanf("%f",&km);
printf("the temp in frah is %.2f",(km/1.609344));
__________________________
CONDITIONAL QUESTIONS
__________________________

6)Write a C program to accept two integers and check whether they are equal or not.

value lelo -> a=4, b=10; equal hai ya nhi hai


int a, b;
scanf("%i %i",&a,&b);
if(a==b)
printf("both are equal");
else
printf("both are not equal");

7)Write a C program to check whether a given number is even or odd.

int a;
printf("enter the value of a");
scanf("%i",&a);
if(a%2==0)
{
printf("given number is even");
}
else
{
printf("number is odd");
}

8)Write a C program to check whether a given number is positive or negative.

int n;
printf("enter the value of n:");
scanf("%i",&n);
if(n>0)
{
printf("the number is a positive number");
}
else
{
printf("the number is a negative number");
}

9)Write a C program to find whether a given year is a leap year or not.

// ( year%100==0)
// 2 type year- century year - 1900, 2000,1700 year%400==0-leap year else not leap
// non century year 1996 2004 2008 1224 %4==0
int n;
scanf("%i",&n);
if((n%400==0 || n%4==0 )&& n%100 != 0 ){
printf("year is laep year");
}
else{
printf("year is not a leap year");
}

10) Write a C program to read the age of a candidate and determine whether it is eligible
for casting his/her own vote.

int age;
printf("enter the age:");
scanf("%i",&age);
if(age>=18)
{
printf("he has the right to vote");
}
else
{
printf("he dont the right to vote");
}

11) Write a C program to read the value of an integer m and display the value of n is 1 when
m is
larger than 0, 0 when m is 0 and -1 when m is less than 0.

int m;
printf("enter the value of m:");
scanf("%i",&m);
if(m>0)
{
printf("the value of n is 1");
}
else if(m==0)
{
printf("the value of n is 0");
}
else
{
printf("the value of n is -1");
}

12)Write a C program to accept the height of a person in centimeter and categorize the person
according to their height if below 150 he is dwarf if above 150 and below 200 average and
above 200 tall.

int h;
printf("enter the value of height:");
scanf("%i",&h);
if(h<150)
{
printf("dwarf");
}
else if(h>150 && h<200)
{
printf("average");
}
else
{
printf("he is tall");
}
13)Write a C program to find the largest of three numbers.

int a=5,b=2,c=0;
if(a>b && a>c)
{
printf("a is greatest");
}
else if(b>a && b>c){
printf("b is greatest");
}
else
{
printf("c is greatest");

14)accept marks of 4 subjects and calculate percentage if percentage is below 35% - F


below 45% - D below 55% - C below 75% - B above 75% - A

int m1,m2,m3,m4;
scanf("%i %i %i %i",&m1,&m2,&m3,&m4);
int average = (m1+m2+m3+m4)/4;
if(average<=35)
{
printf("grade is F");
}
else if(average<=45)
{
printf("grade is D");
}
else if(average<=55)
{
printf("grade is C");
}
else if(average<=75)
{
printf("grade is B");
}
else if(average>=75)
{
printf("grade is A");
}

15) Write a C program to check whether an alphabet is a vowel or consonant.


char c;
scanf("%c",c);
if(c==a || c==e || c==i || c==o || c==u)
{
printf("%c is vowel",c);
}
else{
printf("%c is consonants",i);
}
}

16)Write a C program to calculate profit and loss on a transaction accept cost price and
selling price.

int cp=200;
int sp=400;
if(sp>cp)
{
profit=sp-cp;
printf("%i ",profit);
}
else if(cp>sp)
{
loss=cp-sp;
printf("%i ",loss);
}
else
{
printf("no profit no loss");
}

17)Accept two numbers from user and swap their values

int a=12, b=24,c;


c=a;
a=b;
b=c;
printf("%i ",a);
printf("%i ",b);
18)int a=10;
int b=20;
a=a+b;
b=a-b;
a=a-b;
printf("%i",a);
printf("%i",b);

18)Accept two numbers from user and swap their values without using third variable

int a=10;
int b=20;
a=a+b;
b=a-b;
a=a-b;
printf("%i",a);
printf("%i",b);

______________________
LOOPS QUESTION
______________________

19 - Accept an integer and Print hello world n times

#include <stdio.h>
int main()
{
// hello world n times
int n;
printf("Enter n : ");
scanf("%i",&n);
for(int i=1; i<=n; i++){
printf(" Hello World\n");
}
return 0;
}

20 - Print natural number up to n.

#include <stdio.h>
int main()
{
// natural number upto n
int n;
printf("Enter n : ");
scanf("%i",&n);
for(int i=1; i<=n; i++){
printf("%i\n",i);
}
return 0;
}

21 - Reverse for loop. Print n to 1.

#include <stdio.h>
int main()
{
// natural number upto 1
int n;
printf("Enter n : ");
scanf("%i",&n);
for(int i=n; i>=1; i--){
printf("%i\n",i);
}
return 0;
}

22 - Take a number as input and print its table


5*1=5
5 * 2 = 10 ... up to 10 termm

int n;
printf("enter the value of n:");
scanf("%i",&n);
for(int i=1; i<11; i++){
printf("%i*%i = %i\n",n,i,n*i);

23 - Sum up to n terms.

int n;
printf("enter the value of n:");
scanf("%i",&n);
int sum=0;
for(int i=1; i<n; i++){
sum=sum+i;
}
printf("%i",sum);

24 - Factorial of a number
int n;
scanf("%i",&n);
int fact=1;
for(int i=n; i>=0; i--){
fact=fact*i;
}
printf("the value of fact is %i",fact);

25 - Print all the numbers which are either divisible by 3 or 5 in a range

#include <stdio.h>
int main(){
// 3/5
int n;
printf("Enter range:");
scanf("%i",&n);
for(int i=1; i<=n; i++){
if(i%3==0||i%5==0){
printf(" %i ",i);
}
}
return 0;
}

26 - Print all the factors of a number.

#include<stdio.h>
int main(){
// factors
int n,fact;
printf("Enter n:");
scanf("%i",&n);
for(int i=1; i<=n; i++){
fact=n%i;
if(fact==0){
printf("%i\n",i);
}
}
return 0;
}

27 - Print the sum of all factors of a number, 50 - 1 + 2 + 5 + 10 + 25 = 43


int n;
scanf("%i",&n);
int sum=0;
for(int i=1; i<=n/2; i++)
{
if(n%i==0)
{
sum=sum+i;
if(i==n/2)
{
printf("%i = ",i);
}
else
{
printf("%i + ",i);
}
}
}
printf("%i",sum);

28 - Print the sum of all even & odd numbers seperately.

int n;
scanf("%i",&n);
int evensum=0,oddsum=0;
for(int i=1; i<n; i++)
{
if(i%2==0)
{
evensum=evensum+i;
}
else
{
oddsum=oddsum+i;
}
}
printf("%i %i ",evensum,oddsum);

29 - Sum of digits of a number, 936 = 18

int n;
scanf("%i",&n);
int copy=n;
int sum=0;
while(n>0) {
int rem=n%1
0;
sum=sum+rem; n=n/10; } printf("sum is %i",sum);

30 - Check if the number is Prime or not.

#include<stdio.h>
int main(){
// prime
int n,count=0;
printf("Enter any number : ");
scanf("%i",&n);
for(int i=1; i<=n; i++){
if(n%i==0){
count++;
}
}
if(count==2){
printf("prime number ");
}
else{
printf("Not a prime number ");
}
10

return 0;
}

31 - Accept a number and print its reverse

#include<stdio.h>
int main(){
// prime
int n,rem;
printf("Enter any number : ");
scanf("%i",&n);

while(n>0){
rem=n%10;
printf("%i",rem);
n=n/10;
}
return 0;
}pp

32 - Accept a number and check if it is a pallindromic number (If number and its reverse are
equal)
Ex - 12321 - Rerverse - 12321

int n;
scanf("%i",&n);
int copy=n;
int sum=0;
int rev=0;
while(n>0)
{
int rem=n%10; 123 321 121 121
rev=rev*10+rem;
n=n/10;
}
if(copy==rev)
{
printf("its an pal number");
}

33 - Accept a number and check if it is a armstrong number (Sum of cube of all digits will be
equal to origional number)
Ex - 407 = 64 + 0 + 343 = 407
153 = 1 + 125 + 27 = 153

int n;
scanf("%i",&n);
int copy=n;
int sum=0;
while(n>0)
{153
int rem=n%10;
sum=sum+rem*rem*rem;
n=n/10;
}
if(copy==sum)
{
printf("its an armstrong number");
}
34 - Accept a number and check if it a perfect number or not.
A number whose sum of factors(excluding number itself) = Number
Ex - 6 = 1, 2, 3 = 6

int n;
scanf("%i",&n);
int sum=0;
for(int i=1; i<=n/2; i++) // 6=1 2 3
12=6 10=5
{
if(n%i==0)
{
printf("%i ",i);
sum=sum+i;
}
}
if(sum==n)
{
printf("its a perfect number");
}

35 - Accept a number and check if it is a strong number or not (Sum of factorial of each digit)
Ex- 145 = 1! + 4! + 5! = 145

int n;
scanf("%i",&n);
int sum=0;
int copy=n;
while(n>0)
{
int rem=n%10; 1 4 5 // 1! + 4!+5!
int fact=1;
for(int i=1; i<=rem; i++) // 4=4*3*2*1
{
fact=fact*i;
}
sum=sum+fact;
n=n/10;
}
if(sum==copy)
{
printf("its a strong number");
}

36 - Accept a number and check if it is a Harshad number


Harshad number is a number or an integer which is completely divisible by sum of its digits.
Ex - 24 = Sum of 2+4 = 6 & 24%6 = 0

#include <stdio.h>
int main()
{
//harshad number
int n,rem;
printf("Enter any number : ");
scanf("%i",&n);
while(n>0)
{
rem=n%10;
printf("%i \n",rem);
n=n/10;
}
return 0;
}

37 - Fibonacci series - 0, 1, 1, 2, 3, 5, 8, 13, 21…

#include <stdio.h>
int main ()
{
int num, prev = 0, curr = 1, next;
printf("Enter max length : ");
scanf("%i",&num);
printf("%i %i ",prev,curr);
for (int i = 1; i <= num-2; i++)
{
next=prev+curr;
prev=curr;
curr=next;
printf ("%i ", next);
}

return 0;
}

38 - Take 2 numbers as inputs and find their HCF.


int n1,n2,s;
scanf("%i %i",&n1,&n2);
int hcf;
if(n1>n2)
{
s=n2;
} // hcf = highest common factor
factors voh hote jo completely divide krta ek numberko
// 4= 1,2,4 6=1,2,3,6 sbse chota common factor=1; hcf ek aisha
factor jo A AUR B DONO KO DIVIDE KRE
// RANGE= MINIMUN=1; MAXIMUM=4;
else
{
s=n1;
}
for(int i=4; i>=1; i--)
{
if(4%2==0 && 8%2==0).
{
hcf=i;
break;
}
}
printf("%i ",hcf);

39 - Find the LCM of two numbers

int n1,n2;
scanf("%i %i",&n1,&n2);
int lcm,g;
if(n1>n2)
{
g=n1;
}
else
{
g=n2;
}
// lcm = least common multiple
lcm=voh number jo aur b dono se divide ho
// 2= 2,4,6,8,10,12 6=6,12,18,24
a*b=tk le jyenge ; 6 se chota nhi ho skta answer
// RANGE= MINIMUN=largest of two;
MAXIMUM=multiply kr do dono ko;
for(int i=g; i<=n1*n2; i++)
{
if(6%2==0 && 6%6==0)
{
lcm=i;
break;
}
}
printf("the lcm is %i",lcm);

40 - Write a program in C to find the sum of the series 1 +11 + 111 + 1111 + .. n terms.

int n;
scanf("%i",&n);
int sum=0;
int prev=0;
for(int i=0; i<n; i++) // 10*1=10 111
10*2=100
{
prev=pow(10,2)+prev; // 10*0=1 1+10+100
//
1+11+111+1111=1234
if(i==n-1)
{
printf("%i = ",prev);
}
else
{
printf("%i +",prev);
}
sum=sum+prev;
}
______________________
ARRAY PROBLEMS
______________________

41 - Accept array elements and reprint it

#include <stdio.h>
int main(){
int a[5];
printf("Enter array elements\n");
for(int i=0; i<5; i++){
scanf("%i",&a[i]);
}
printf("Array elememts are\n");
for(int i=0; i<5; i++){
printf("%i ",a[i]);
}
return 0;
}

42 - Print array elements in reverse order

#include <stdio.h>
int main()
{
int a[5];
printf("Enter array elements\n");
for(int i=0; i<5; i++){
scanf("%i",&a[i]);
}
printf("Elements in reverse order\n");
for(int i=4; i>=0; i--){
printf("%i ",a[i]);
}
return 0;
}

43 - Print positive and negative elements of an array

#include<stdio.h>
int main(int argc, char const *argv[])
{
int size;
printf("enter the size of an array\n");
scanf("%i", &size);
int arr[size];
for (int i = 0; i < size; i++)
{
scanf("%i", &arr[i]);
}
printf("positives:");
for (int i = 0; i < size; i++)
{
if (arr[i]>=0)
{
printf("%i ", arr[i]);
}
}
printf("\nnegatives:");
for (int i = 0; i < size; i++)
{
if (arr[i]<0)
{
printf("%i ", arr[i]);
}
}
return 0;
}

44 - Print array in ascending or descending order

#include <stdio.h>
int main(int argc, char const *argv[])
{
int size;
printf("enter the size of an array\n");
scanf("%i", &size);
int arr[size];
for (int i = 0; i < size; i++)
{
scanf("%i", &arr[i]);
}
printf("ascending:");
for (int i = 0; i < size; i++)
{
printf("%i ", arr[i]);
}
printf("\ndescending:");
for (int i = size - 1; i >= 0; i--)
{
printf("%i ", arr[i]);
}
return 0;
}
45 - Accept size n from user and create a n size array then take n inputs into the and finally
Print the sum of all elements in the array in the following manner
10 + 20 + 30 = 60

int n;
scanf("%i",&n);
int arr[n];
int sum=0;
for(int i=0; i<n; i++)
{
scanf("%i",&arr[i]);
}
for(int i=0; i<n; i++)
{
printf("%i",arr[i]);
sum=sum+arr[i];
}
printf("the sum is %i",sum);

46 - Mean of array elements.

#include <stdio.h>
int main()
{
int size, sum=0;
scanf("%i",&size);
int arr[size];
for(int i=0; i<size; i++){
scanf("%i",&arr[i]);
}
for(int i=0;i<size;i++){
sum=sum+arr[i];
}
printf("mean is %i",sum/size);
return 0;
}

47 - Find the greatest element and print its index too.


{2, 96, 69, 77, 145, 20} = Max element = 145 found at 4 index

#include <stdio.h>
int main()
{
int size, gi;
int arr[size];
printf("Enter size\n");
scanf("%i",&size);
for(int i=0; i<size; i++){
scanf("%i",&arr[i]);
}
gi = 0;
for(int i=1; i<size; i++){
if(arr[gi]<arr[i]){
gi=i;
}
}
printf("greatest is %i and its index is %i",arr[gi],gi);
return 0;
}

48 - Find the smallest element and print its index too.


{2, 96, 69, 77, 145, 20} = Min element = 2 found at 0 index

int main()
{
int n;
scanf("%i",&n);
int arr[n];
for(int i=0; i<n; i++)
{
scanf("%i",&arr[i]);
}
int gi=0;
for(int i=0; i<n; i++)
{
if(arr[i]>arr[gi])
{
gi=i;
}
}
printf("the greatest index is found at %i and the number is %i ",gi,arr[gi]);
int sgi=1;
for(int j=0; j<n; j++)
{
if(arr[j]!=arr[gi])
{
sgi=j;
printf("%i",sgi);
}
}
printf("the second greatest index is found at %i and the number is %i",sgi,arr[sgi]);

49 - Find the second greatest element


{2, 96, 69, 77, 145, 20} = Second greatest element = 96

int arr[]={2,1,96,69,77,145,20};
int count=sizeof(arr)/sizeof(int);
int si=0;
for(int i=1; i<count; i++)
{
if(arr[si]>arr[i])
{
si=i;
}
}
printf("the smallest index is found at %i and the number is %i",si,arr[si]);

50 - Pallindromic array - Write a program to check if elements of an array are same or not it read
from front or back
Example : arr= [2,3,15,15,3,2]

int n;
scanf("%i",&n);
int arr[n];
int check=0;
int last=n-1;
for(int i=0; i<n; i++)
{
scanf("%i",&arr[i]);
}
printf("%i",last);
for(int i=0; i<=n/2; i++)
{
if(arr[i] != arr[last])
{
check=1;
}
last--;
}
if(check==1)
{
printf("its not a pal");
}

51 - Array left Rotation by 1

//left rotat by 1
int main()
{
int n;
scanf("%i",&n);
int arr[n];
for(int i=0; i<n; i++)
{
scanf("%i",&arr[i]);
}
int copy=arr[0];
for(int i=0; i<n; i++)
{
arr[i]=arr[i+1];
}
arr[n-1]=copy;
for(int i=0; i<n; i++)
{
printf("%i ",arr[i]);
}
return 0;
}

52 - Array right Rotation by 1

// right r by one
int n;
scanf("%i",&n);
int arr[n];
for(int i=0; i<n; i++)
{
scanf("%i",&arr[i]);
}
int copy=arr[n-1];
for(int i=n-1; i>=0; i--)
{
arr[i]=arr[i-1];
}
arr[0]=copy;
for(int i=0; i<n; i++)
{
printf("%i ",arr[i]);
}

53 - Array left rotation by K elements

//kth left rotation


int n=5;
int arr[5]={1,2,3,4,5};
int k=2;
for(int i=0;i<k;i++)
{
int copy=arr[0];
for(int i=0; i<n; i++)
{
arr[i]=arr[i+1];
}
arr[n-1]=copy;
}
for(int i=0; i<5;i++)
{
printf("%i ",arr[i]);
}

54 - Array rigth rotation by K elements

//kth right rotation


int n=5;
int arr[5]={1,2,3,4,5};
int k=2;
for(int i=0;i<k;i++)
{
int copy=arr[n-1];
for(int i=n-1; i>=0; i--)
{
arr[i]=arr[i-1];
}
arr[0]=copy;
}
for(int i=0; i<5;i++)
{
printf("%i ",arr[i]);
}

55 - Linear Search an array - If element found print the index else -1

//linear search
int n;
scanf("%i",&n);
int arr[n];
int check=0;
int last=n-1;
for(int i=0; i<n; i++)
{
scanf("%i",&arr[i]);
}
int s=4;
for(int i=0; i<n; i++)
{
if(s==arr[i])
{
printf("element is found at %i index",i);
check=1;
}
}
if(check==0)
{
printf("the number is not found ");
}

56 - Binary Search. If element found print the index else -1

//binary search
int n;
scanf("%i",&n);
int arr[n];
int check=0;
for(int i=0; i<n; i++)
{
scanf("%i",&arr[i]);
}
int start=0;
int end=n-1;
int middle=(start+end)/2;
int s=8;
printf("%i",middle);
while(start<=end)
{
if(s==arr[middle])
{
printf("the number is found at %i",middle);
check=1;
break;
}
else if(s>arr[middle])
{
start=middle+1;
middle=(start+end)/2;
}
else if(s<arr[middle])
{
end=middle-1;
middle=(start+end)/2;
}
}

57 - Move all the negative elements on left side and positive elements on right side

//sort
int arr[]={25,-1,-2,3,1};
int size=sizeof(arr)/sizeof(int);
for(int j=1; j<size; j++)
{
for(int i=0; i<size-1; i++)
{
if(arr[i]>arr[i+1])
{
int temp=arr[i+1];
arr[i+1]=arr[i];
arr[i]=temp;
}
}
}
for(int i=0; i<size; i++)
{
printf("%i ",arr[i]);
}

58 - Bubble Sort.

int arr[]={25,-1,-2,3,1};
int size=sizeof(arr)/sizeof(int);
for(int j=1; j<size; j++)
{
for(int i=size-1; i>0; i--)
{
if(arr[i]<arr[i-1])
{
int temp=arr[i-1];
arr[i-1]=arr[i];
arr[i]=temp;
}
}
}
for(int i=0; i<size; i++)
{
printf("%i ",arr[i]);
}

59 - Median of Array elements

//median
#include <stdio.h>
int
main ()
{
int size;
printf ("Enter size of an array\n");
scanf ("%i", &size);
int arr[size];
for (int i = 0; i < size; i++)
{
scanf ("%i", &arr[i]);
}
for (int j = 0; j < size; j++)
{
for (int i = 0; i < size - 1; i++)
{
if (arr[i] > arr[i + 1])
{
int temp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = temp;
}
}
}
for (int i = 0; i < size; i++)
{
printf ("%i ", arr[i]);
}
int mid;
mid = (size - 1) / 2;
if (size % 2 == 0)
{
float newmid = (float) (arr[mid] + arr[mid + 1]) / 2;
printf ("\nmedian is %.2f", newmid);
}
else
{
printf ("\nmedian is %i", arr[mid]);
}
return 0;
}

60 - given an array input a number and print how many times it has appeared

int a=1;
int arr[5]={1,1,1,2,3};
int n=5;
int check=0;
int count=0;
for(int i=0; i<n;i++)
{
if(arr[i]==a)
{
count++;
check=1;
}
}
if(check==0)
{
printf("element not found");
}
else
{
printf("element is found and apperaed %i times",count);
}

61 - print how many times each number is repeated

#include <stdio.h>
#include<stdlib.h>
int main() {
// Write C code here
int arr[]={1,2,2,2,2,5,99,100,1,1,1};
int size=sizeof(arr)/sizeof(int);
int *occ=calloc(100,sizeof(int));
for(int i=0;i<size;i++)
{
occ[arr[i]]++;
}
for(int i=1;i<=100;i++)
{
if(occ[i]>0)
{
printf("%i comes %i times\n",i,occ[i]);
}
}
return 0;
}
_______________________
STRING QUESTIONS
_______________________

62 - Write a program in C to input a string and print it

#include <stdio.h>
int main() {
char str[100];
fgets(str,100,stdin);
printf("%s ",str);
return 0;
}
63 - Write a program in C to find the length of a string

#include <stdio.h>
int main() {
char str[100];
fgets(str,100,stdin);
int i=0;
while(str[i]!='\0')
{
i++;
}
printf("the length of string is %i",i);
// we have to count \0 as well
return 0;
}

64 - Write a program in C to separate the individual characters from a string.

#include <stdio.h>
int main() {
char str[100];
fgets(str,100,stdin);
int i=0;
while(str[i]!='\0')
{
printf("%c ",str[i]);
i++;
}
return 0;
}

65 - Write a program in C to print a string in reverse

#include <stdio.h>
int main() {
// Write C code here
char str[100];
fgets(str,100,stdin);
int count=0;
int i=0;
while(str[i]!='\0')
{
count++;
i++;
// sirf i bhi le skte ho gar count varaible nhi lena hai toh
}
for(int j=count-1; j>=0;j--)
{
printf("%c",str[j]);
}
return 0;
}

66 - Write a program in C to count the total number of characters in a string.

#include <stdio.h>
int main() {
char str[100];
fgets(str,100,stdin);
int i=0;
while(str[i]!='\0')
{
i++;
}
printf("the length of string is %i",i-1);
// \0 ko include nhi krna esliye humne i-1 kiya hai
return 0;
}

67 - compare two strings if identical print (identical) else (not identical)

#include <stdio.h>
// agar while loop se length nikal to yaad rkhna humesha i-1 krna pdhta hai
int main() {
// Write C code here
char str1[100];
fgets(str1,100,stdin);
char str2[100];
fgets(str2,100,stdin);
int check=0;
//size ko calculate krne k liye while loop ka use kr skte hai mai ek built in function ka use kr rhi
hu
int len1=strlen(str1);
int len2=strlen(str1);
if(len1 != len2)
{
printf("the are not equal");
}
else
{
int i=0;
while(str1[i] !='\0')
// yha pe str1 str2 dono likh skte hai kyunki dono ki length same hai
{
if(str1[i] != str2[i])
{
printf("they are not equal");
check=1;
break;
}
i++;
}
}

if(check==0)
{
printf("they are equal");
}
// else mai kuch likhne ki need nhi hai kyunki hum pehle hi print krva chuke hai strings are not
equal
return 0;
}

68 - Write a program in C to copy one string to another string.

#include <stdio.h>
int main() {
// Write C code here
char str[100];
fgets(str,100,stdin);
int count=0;
int i=0;
while(str[i]!='\0')
{
i++;
}
char str1[i];
// printf("%i ",i);
for(int j=0; j<i;j++)
{
str1[j]=str[j];
// printf("%i ",j);
}
str1[i]='\0';
printf("%s ",str1);
return 0;
}

!!! 69 !!! - Write a program in C to find maximum occurring character in a string.

#include <stdio.h>
#include <stdlib.h>
int main() {
// Write C code here
char str[100];
fgets(str,100,stdin);
int *arr = calloc(200,sizeof(int));
int len=strlen(str);
for(int i=0; i<len;i++)
{
arr[str[i]]++;
}
// neeche ka code is same as findoig the largest number qno. 47
int li=0;

for(int i=1; i<200;i++)


{
if(arr[i]>arr[li])
{
li=i;
}
}
printf("the max occuring character occured %i times and the char is %c",arr[li],li);

return 0;
}

70 -Write a program in C to find the number of times a given word 'the' appears in the given
string

71 - Write a program in C to Find the Frequency of Characters. take both as input (string)
as well as the char you want to find frequency of
#include <stdio.h>
int main() {
// Write C code here
char str[100];
fgets(str,100,stdin);
char ch;
scanf("%c",&ch);
int i=0;
int count=0;
while(str[i] != '\0')
{
if(str[i]==ch)
{
count++;
}
i++;
}
if(count ==0)
{
printf(" element sis not found");
}
else
{
printf("element occured %i times",count);
}

return 0;
}

72 - Write a program in C to Concatenate Two Strings Manually.

#include <stdio.h>
int main() {
// Write C code here
char str[100];
fgets(str,100,stdin);
char str1[100];
fgets(str1,100,stdin);

int i=0;
while(str[i] != '\0')
{
i++;
}
int j=0;
while(str1[j] != '\0')
{
j++;

}
char str2[i+j-1];
int l=i+j-1;
// i-1 total charcter count of str
// j-1 total character count of str1
// str2 ki length honi chahiye i-1+j-1 + 1 extra 1 for \0
// str2=i-1+j-1+1= i+j-1
int m=0;
for(int k=0; k<i;k++)
{
str2[m]= str[k];
m++;
}
for(int k=0; k<j;k++)
{
str2[m]= str1[k];
m++;
}
str2[m]='\0';
printf("%s ",str2);
return 0;
}

73 - Write a program in C to find the largest and smallest word in a string.

74 - Write a program in C to replace the spaces of a string with a specific character.


take the specific character and string as input

#include <stdio.h>
int main() {
char str[100];
fgets(str,100,stdin);
char ch;
scanf("%c",&ch);
int i=0;
while(str[i]!='\0')
{
if(str[i]==' ')
{
str[i]=ch;
}
i++;
}
printf("%s",str);
return 0;
}

75 - remove the spaces of a string

#include <stdio.h>
int main() {
char str[100];
fgets(str,100,stdin);
int i=0;
while(str[i]!='\0')
{ int j=i;

if(str[i]==' ')
{
while(str[j+1] !='\0')
{

str[j]=str[j+1];
j++;
}
}
i++;
}
printf("%s",str);
return 0;
}

76 - get two words and add these strings eg-


input 1 - "abcdef"
input 2 - "pqrst"

otput - "apbqcrdset"
#include <stdio.h>
int main() {
char str[]="abcdef";
char str1[]="pqrst";
int i=0;
while(str[i] != '\0')
{
i++;
}
int j=0;
while(str1[j] != '\0')
{
j++;
}
char str2[i+j+1];
int l=i+j;
printf("%i",l);
// i total charcter count of str
// j total character count of str1
// str2 ki length honi chahiye i+j + 1 extra 1 for \0
// str2=i+j+1
int m=0;
for(int k=0; k<l;k+=2)
{
str2[k]= str[m];
// printf("%c",str[m]);
m++;
}
// printf("%s",str2);
m=0;
for(int k=1; k<l;k+=2)
{
str2[k]= str1[m];
m++;
}
str2[l]='\0';
printf("%s ",str2);
return 0;
}

77 - check wether string is pallindrome or not


#include <stdio.h>
#include <stdlib.h>
int main() {
char str[100];
fgets(str,100,stdin);
// char str[]="aba";
int check=0;
int len=strlen(str);
printf("%i ",len);
int last=len-2;
//last=len-2 kyunki last character present hai 3rd index pe
// printf("%c",str[last]);
for(int i=0; i<=len/2;i++)
{
if(str[i]!=str[last])
{
printf("its not pal");
check=1;
break;
}
last--;
}
if(check==0)
{
printf("its a pal");
}
return 0;
}

______________________
MIXED QUESTIONS
______________________

78 - Write a C program to get the indices of the two numbers of a given array of integers,
such that the sum of the two numbers equal to a specific target.
Original Array: 4 2 1 5
Target Value: 7
Indices of the two numbers whose sum equal to target value: 7
13

79 - accept 2 arrays merge them and find the median.


80 - remove duplicates from a array
81 - accept a number and find all the multiples of 5 and 7 below that number
82 - The sum of the squares of the first n natural number
83 - accept an array then accept an index value and split the array into 2 seprate
parts according to the index and then arrange both the arrays in ascending
order
84 - Write a program in C to find Disarium numbers between 1 to 1000.
check google to see what is a disarium number
85 - Write a program in C to find pronic numbers between 1 to 1000.
chech google for pronic number
86 - input a range check all the prime withen it and add all the prime numbers

_____________________
PATTERNS
_____________________

87 -
Right Triangle - Star
*
**
***
****
*****

#include <stdio.h>
int main() {
int n;
scanf("%i",&n);
for(int i=1; i<n;i++)
{
for(int j=1; j<=i;j++)
{
printf(" * ");
}
printf("\n");
}
return 0;
}

88 - Right Triangle - Number


1
12
123
1234
12345
#include <stdio.h>
int main() {
int n;
scanf("%i",&n);
for(int i=1; i<n;i++)
{
for(int j=1; j<=i;j++)
{
printf("%i ",j);
}
printf("\n");
}
return 0;
}

89 - Inverted Right Triangle


*****
****
***
**
*

#include <stdio.h>
int main() {
int n;
scanf("%i",&n);
for(int i=1; i<n;i++)
{
for(int j=n-i; j>=1;j--)
{
printf(" * ");
}
printf("\n");
}
return 0;
}

90 - Mirrored Right Triangle


*
**
***
****
*****

#include <stdio.h>
int main()
{
int n;
scanf("%i",&n);
for(int i=1; i<n;i++)
{
for(int j=n-i; j>=1;j--)
{
printf(" ");
}
for(int k=1; k<=i;k++)
{
printf("* ");
}
printf("\n");
}
return 0;
}

You might also like