Skip to main content

Posts

Showing posts from 2023

What will be output of following c code using for loop?

  Output of following c code using for loop #include<stdio.h>  int main(){     static int i;     for(++i;++i;++i) {          printf("%d ",i);          if(i==4) break;     }     return 0; }   Output: 24 Explanation: Default value of static int variable in c is zero. So, initial value of variable i = 0 First iteration: For loop starts value: ++i i.e. i = 0 + 1 = 1  For loop condition: ++i i.e. i = 1 + 1 = 2 i.e. loop condition is true. Hence printf statement will print 2 Loop incrimination: ++I i.e. i = 2 + 1 =3 Second iteration: For loop condition: ++i i.e. i = 3 + 1 = 4 i.e. loop condition is true. Hence printf statement will print 4. Since is equal to for so if condition is also true. But due to break keyword program control will come out of the for loop. Join us on Facebook:  Click Here  

What will be output of following c code while loop increment decrement?

  Output of following c code while loop                                increment decrement? #include<stdio.h> int main(){     int i,j;     i=j=2,3;     while(--i&&j++)          printf("%d %d",i,j);     return 0; } Output: 13 Explanation : Initial value of variable  i = 2 j = 2 Consider the while condition : --i && j++ In first iteration:  --i && j++ = 1 && 2 //In c any non-zero number represents true. = 1 (True) So while loop condition is true. Hence printf function will print value of i = 1 and j = 3 (Due to post increment operator) In second iteration: --i && j++ = 0 && 3  //In c zero represents false = 0  //False So while loop condition is false. Hence program control will come out of the for loop. Join us on Facebook:  Click Here  

What will be output of following c code using while loop ?

  Output of c code using while loop #include<stdio.h> extern int x; int main(){     do{         do{              printf("%o",x);          }          while(!-2);     }     while(0);     return 0; } int x=8; Output: 10 Explanation: Here variable x is extern type. So it will search the definition of variable x. which is present at the end of the code. So value of variable x =8 There are two do-while loops in the above code.  AS we know do-while executes at least one time even that condition is false.  So program control will reach  at printf statement at it will print octal number 10 which is equal to decimal number 8.  Note: %o is used to print the number in octal format.  In inner do- while loop while condition is ! -2 = 0 In C zero means false.  Hence program control will come out of the inner do-while l...

A gold smith sells a one gram of 24 carat gold for Rs 5500 .WAP to read the weight of gold in grams and calculate its cost in 18,20,22, and 24 carat in c++ ?

  The weight of gold in grams and calculate its cost in 18,20,22, and 24 carat in c++. #include<iostream.h> #include<conio.h> void main() { float weight,carat,carat18,carat20,carat22,carat24; clrscr(); cout<<endl<<"Enter the weight of gold in grams :"; cin>>weight; carat=5500/24.0; carat18=carat*18*weight; carat20=carat*20*weight; carat22=carat*22*weight; carat24=5500*weight; cout<<endl<<"Cost in 18 carats = " <<carat18; cout<<endl<<"Cost in 20 carats = " <<carat20; cout<<endl<<"Cost in 22 carats = " <<carat22; cout<<endl<<"Cost in 24 carats = " <<carat24; getch(); } Join us on Facebook:  Click Here  

Write a program to read the five digit number and find the reverse in c++ ?

  The five digit number and find the reverse in c++   num = 12345 rev=54321 #include<iostream.h> #include<conio.h> void main() { long num,rev; clrscr(); //read the five -digit number  cout<<endl<<"Enter the five digit number :"; cin>>num; //find the reverse  rev=num%10; num=num/10; rev=rev*10+num%10; num=num/10; rev=rev*10+num%10; num=num/10; rev=rev*10+num; cout<<endl<<"Reverse  = " <<rev; getch(); } Join us on Facebook:  Click Here  

WAP to read the three digit number and find the sum of the first and the last digit in C++ ?

  The three digit number and find the sum of the first and the last digit in C++ e.g.  num = 345  sum = 3+5=8 #include<iostream.h> #include<conio.h> void main() { int num,sum; clrscr(); //read the three digit number  cout<<endl<<"Enter the three digit number :"; cin>>num ; //extract the first and the last digit  int a,b; a=num/100; b=num%10; sum =a+b; cout<<endl<<"Sum of digits = " <<sum; getch(); } Join us on Facebook:  Click Here  

WAP to swap two numbers without using the third variable in C++ ?

  Swap two numbers without using the third variable in c++ ? #include<iostream.h> #include<conio.h> void main() { int a,b; clrscr(); //read the two numbers  cout<<endl<<"Enter the value of a and b :"; cin>>a>>b; //swap the values  cout<<endl<<"Before Swap , a= " <<a<<" b= " <<b; a=a+b; b=a-b; a=a-b; cout<<endl<<"After Swap , a="<<a<<" b= "<<b; getch(); } Join us on Facebook:  Click Here  

WAP to find the average of three numbers in C++ ?

    Average of three numbers in C++ #include<iostream.h> #include<conio.h> void main() { int a,b,c; float avg; clrscr(); //read the three numbers  cout<<endl<<"Enter the value of a ,b, and c:"; cin>>a>>b>>c; //calculate the average  avg=(a+b+c)/3.0; //display the average  cout<<endl<<"Average = " <<avg; getch(); } Join us on Facebook:  Click Here  

Write a "C" program to generate the pattern using string function ?

  To generate the pattern using string function String : jack Pattern j ja jac jack #include<stdio.h> #include<conio.h> #include<string.h> void main() { char str[50]; int i,j,n; clrscr(); printf("\nEnter the string :"); gets(str); /*calculate the length of the  string */ n=strlen(str)-1; for(i=1;i<=n;i++) { for(j=0;j<i;j++) { printf("%c",str[j]); } printf("\n"); } getch();  } Join us on Facebook:  Click Here    

Write a "C" program to reverse the string using string functions ?

  Program to reverse the string using string functions #include<stdio.h> #include<conio.h> #include<string.h> void main( ) { char str[50],rev[50]; int i,n,j; /*read the string */ printf("\nEnter the string : "); gets(str); /*find the length of the string */ n=strlen(str)-1; i=n; j=0; while(i>=0) { rev[j]=str[i]; j++; i--; } rev[j]='\0'; printf("\nReverse = %s",rev); getch(); } Example : String : kapil Reverse:lipak Join us on Facebook:  Click Here    

Write a "C" program to concatenate the one string into another without using the strcat functions ?

  To concatenate two strings in c without using strcat function #include<stdio.h> #include<conio.h> void main( ) { char str1[50],str2[50]; int i,j; clrscr( ); printf("\nEnter the first string :"); gets(str1); printf("\nEnter the second string :"); gets(str2); /*move to the end of the first string*/ i=0; while(str1[i]!='\0') { i=i+1; } /*Copy second string into first string*/ j=0; while(str2[j]!='\0') { str1[i]=str2[j]; i=i+1; j=j+1; } str1[i]='\0'; printf("\nResult : %s",str1); getch( ); } Join us on Facebook:  Click Here    

Write a 'c' program to concatenate two string using strcat function ?

  Strcat() function is used to concatenate two string strcat( ) :This function is used to concatenate two string. By concatenation , we means that                          the second string is added at the end of the first string. The general form is : void strcat(string1,string2) ; Consider the following program : #include<stdio.h> #include<string.h> #include<conio.h> void main( ) { char str1[50],str2[50]; int i; clrscr( ); printf("\nEnter the first string :"); scanf("%s",str1); printf("\nEnter the second string :"); scanf("%s",str2); strcat(str1,str2); printf("\nResult : %s",str1); getch( ); } output : Enter the first string : Jatin Enter the second string :Lalit Result: JatinLalit  Join us on Facebook:  Click Here    

Write a "C" program to copy the one string into another without using the library functions ?

  Copy the one string into another without using   functions #include<stdio.h> #include<conio.h> void main( ) { char str1[50],str2[50]; int i; clrscr( ); printf("\nEnter the string :"); gets(str1); i=0; while(str1[i]!='\0') { str2[i]=str1[i]; i=i+1; } str2[i]='\0'; printf("\nResult : %s",str2); getch( ); } output : Enter the string : java Result : java Join us on Facebook:  Click Here    

Write a 'c' program to copy the one string into another using strcpy() function?

  To copy the one string into another using function  strcpy( ) : This function is used to copy the one string into another. The general form is ,void                         strcpy(target,source); Consider the following program , #include<stdio.h> #include<string.h> #include<conio.h> void main( ) { char str1[50],str2[50]; int i; clrscr( ); printf("\nEnter the string :"); scanf("%s",str1); strcpy(str2,str1); printf("\nResult : %s",str2); getch( ); } output : Enter the string : computer Result : computer Join us on Facebook:  Click Here    

Write a 'C' program to read the string and convert it into the uppercase ?

  Read the string and convert it into the uppercase String : Ajay output :AJAY 'a'  : ASCII value 97 'A' : ASCII value 65 #include<stdio.h> #include<conio.h> void main( ) { char s[50]; int i; clrscr( ); printf("\nEnter the string :"); gets(s); i=0; while(s[i]!='\0') { if(s[i]>='a'&&s[i]<='z') { /*lowercase,so convert it into uppercase*/ s[i]=s[i]-32; } i=i+1; } printf("\nResult : %s",s); getch( ); } Join us on Facebook:  Click Here    

Write a 'c' program to calculate length of string without using strlen() function ?

   Calculate length of string without using strlen() function #include<stdio.h>  #include<conio.h> void main( ) { char s[50]; int i; clrscr( ); printf("\nEnter the string :"); gets(s); i=0; while(s[i]!='\0') { i++; } printf("\nLength=%d",i); getch( ); } output : Enter the string : ram Length = 3 Join us on Facebook:  Click Here    

Write a 'c' program to calculate the length of the string ?

    Calculate the length of the string strlen( ) :  This function will calculate the length of the  string.by length,we means the number of characters present in the string,excluding the NULL character. The general form is , int strlen(char [ ]) Consider the following , #include<stdio.h> #include<string.h> #include<conio.h> void main() { char s[50]; int len; clrscr( ); printf("\nEnter the String :"); scanf("%s",s); len=strlen(s); printf("\nLength=%d",len); getch( ); } output : Enter the string : ajay Length = 4 Join us on Facebook:  Click Here    

WAP read the multiple words string using the gets( ) function in C ?

  Gets() function The multiple words string using the gets( ) function  Because the scanf( ) function will get terminated by pressing the space. But if you want to read the multiple words string , we have to make use of the gets( ) function. gets( ) : This function is present in the header file stdio.h and  is used to read the string which can even contain the spaces. Thus, the gets( ) will get terminated only by pressing the enter. The general form is ,   gets(string name); Consider the following program, #include<stdio.h> #include<conio.h> void main( ) { char s[50]; clrscr( ); printf("\nEnter the string :"); gets(s); printf("\nYour string : %s",s); getch(); } output : Enter the string : computer science Your string : computer science In "C" programming language , we are provided with a separate file "string.h" which will be used for the string handling. It contains various pre-defined functions , which will used for ...