C programming 2016

2nd Semester

2016

2. Display the following pattern using loop.
a. BBA
b. BIM
c. BIM
d. BBA
e. BBA
f. BIM

#include<stdio.h>
void main()
{
   int i;
   char c='a';
   for(i=1;i&lt;=6;i++)
   {
      if(i==1||i==4||i==5)
         printf("%c. BBA\n",c);
      else
         printf("%c. BIM\n",c);
      c++;
   }
}

3. Write a program to count white spaces in a given sentence.

#include<stdio.h>
#include<string.h>
void main()
{
   char s[100];
   int i=0,c=0,l=0;
   printf("Enter a string:\n");
   gets(s);
   while(s[i]!='\0')
   {
      l++;
      i++;
   }
   for(i=0;i&lt;l;i++)
   {
      if(s[i]==' ')
        c++;
   }
   printf("Total number of white spaces in the given sentence is: %d.",c);
}
Output:
Enter a string:
Hello My Name is
Total number of white spaces in the given sentence is: 3.

4. Write a program to calculate the sum of the following numbers: -3, -4, 8, 9, 10, -17.

#include<stdio.h>
void main()
{
 int a[6]={(-3),(-4),8,9,10,(-17)};
 int i,sum=0;
for(i=0;i<6;i++)
{
 sum+=a[i]; }
printf("The sum of (-3,-4,8,9,10,-17) is %d",sum);
 }

5. Write a function to check if the entered prime number is greater than 90 and is odd.

#include <stdio.h>
 int main()
{
int n, i, flag = 0;
printf("Enter a prime number: ");
 scanf("%d",&n);
for(i=2; i<=n/2; i++) { if(n%i==0)
{
 flag=1; break;
} }
 if (flag==0)
 {
 printf("%d is a prime number ",n);
if (n>90)
printf(" is greater than 90 ");
 else
 printf(" is greater than 90 ");
 if(n%2!=0)
printf(" and is odd ");
else
 printf(" and is even ");
}
else printf("%d is not a prime number",n)
;
 return 0;
}




6. Write a function that converts the lowercase string into uppercase string.

#include<stdio.h>
#include<string.h>
char *convert(char a[])
{
return strupr(a);
}
void main()
{
char a[50];
printf("Enter a string(in lowercase): ");
gets(a);
printf("Given string in uppercase: %s",convert(a));
}
Output:
Enter a string(in lowercase): Hello
Given string in uppercase: HELLO

Group “C”

9. Write the output generated by the following program.

#include<stdio.h>
#include<conio.h>
void main(){
     int a,b,c;
     clrscr();
     for(a=1;a&lt;=3;a++){
         for(b=1;b&lt;=3;b++){
                for(c=1;c&lt;=3;c++){
                   if(a==b||b==c||a==c){
                          continue;
                      }
                     else{                     {
                           printf("%d%d%d\n",a,b,c);
                      }
                  }
                }
         }
     }
     getch();
}
Output:
123
132
213
231
312
321

 

Leave a Reply

Your email address will not be published.